iOS7已经不再支持NSString的drawAtPoint:forWidth:withFont:minFontSize:actualFontSize:lineBreakMode:baselineAdjustment:方法。

12

iOS7发布后,以下函数已被弃用:

drawAtPoint:forWidth:withFont:minFontSize:actualFontSize:lineBreakMode:baselineAdjustment:

在苹果的文档中建议使用

drawInRect:withAttributes:

我使用这个函数的原因是因为其中的<code>minFontSize</code>参数,它允许我在矩形内绘制字符串。

如果文本不适合,它会先缩小文本大小到<code>minFontSize</code>,然后如果仍然不适合,就会截断它。

我无法使用<code>drawInRect:withAttributes:</code>来实现这一点。

哪个键可以用来确定等效于<code>minFontSize</code>的值?

4个回答

13

现在比以前复杂了一点,你不能使用最小字体大小,而必须使用最小字体缩放因子。此外,iOS SDK存在一个漏洞,它破坏了大多数用例(请参见底部的注释)。以下是您需要做的内容:

// Create text attributes
NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:18.0]};

// Create string drawing context
NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
drawingContext.minimumScaleFactor = 0.5; // Half the font size

CGRect drawRect = CGRectMake(0.0, 0.0, 200.0, 100.0);
[string drawWithRect:drawRect
             options:NSStringDrawingUsesLineFragmentOrigin
          attributes:textAttributes
             context:drawingContext];

注意:

  • iOS 7 SDK至少到版本7.0.3似乎存在一个bug: 如果在属性中指定自定义字体,则最小比例因子将被忽略。如果将属性设置为nil,则文本会正确缩放。

  • NSStringDrawingUsesLineFragmentOrigin选项很重要。它告诉文本绘制系统,绘制矩形的起点应该位于左上角。

  • 无法使用新方法设置基线调整。您需要首先调用boundingRectWithSize:options:attributes:context:进行调整,并在将其传递给drawWithRect:options:attributes:context之前调整矩形。


根据文档,您的答案是正确的。然而,正如您所指出的,似乎存在一个错误,但这被忽略了。我没有尝试您留下属性为空的建议,因为我确实需要传入一些属性,否则返回的大小将不正确。我将此答案标记为正确。谢谢! - Rob

1

在长时间的谷歌搜索之后,我没有找到在iOS7上运行的解决方案。

现在我使用以下权宜之计,知道它非常丑陋。

我在内存中渲染一个UILabel,然后截屏并绘制出来。

UILabel能够正确地缩小文本。

也许有人会觉得这很有用。

UILabel *myLabel = [[UILabel alloc] initWithFrame:myLabelFrame];
myLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];
myLabel.text = @"Some text that is too long";
myLabel.minimumScaleFactor = 0.5;
myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.backgroundColor = [UIColor clearColor];

UIGraphicsBeginImageContextWithOptions(myLabelFrame.size, NO, 0.0f);
[[myLabel layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[screenshot drawInRect:myLabel.frame];

0
只需使用该键和属性创建一个NS字典即可,代码如下:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: @"15", @"minFontSize", @"value2", @"key2", nil];
//in key2 and value2 you could set any other of the attributes included in the first method
[yourString drawInRect:rect withAttributes:attributes];

2
将minFontSize添加为键实际上并不会改变任何内容。 - Rob
是的,抱歉,我没有正确阅读你的问题,我在文档中找不到任何与minFontSize完全相同的键,但也许尝试使用“NSStrokeWidthAttributeName”这个键会有所帮助,希望能对你有所帮助。 - heczaco

0
我使用以下代码解决了我的问题。 使用下面的代码,您可以在X,Y位置绘制文本,设置字体、字号和字体颜色。
[String drawAtPoint:CGPointMake(X, Y) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:18], NSForegroundColorAttributeName:[UIColor colorWithRed:199.0f/255.0f green:0.0f/255.0f blue:54.0f/255.0f alpha:1.0f] }];

1
在回答中添加一些附加上下文,以帮助人们理解为什么这是一个好的回答,而不仅仅发布原始代码。 - Aaron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接