问题 iOS7弃用了NSString的drawAtPoint:forWidth:withFont:minFontSize:actualFontSize:lineBreakMode:baselineAdjustment:


在iOS7发布时,不推荐使用以下函数:

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

在Apple的文档中,它建议使用

drawInRect:withAttributes:

我使用这个功能的原因是因为 minFontSize 参数,它让我在rect中绘制一个字符串。如果文本不适合,它将首先缩小文本大小 minFontSize 如果它不合适,它将截断它。到目前为止,我无法完成此任务 drawInRect:withAttributes:

任何人都可以帮我确定哪个键可以用来确定 minFontSize 当量?

谢谢!

-抢


5925
2017-09-20 18:20


起源



答案:


它比以前复杂一点,你不能使用最小字体大小,但必须使用最小字体比例因子。 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的错误:如果在属性中指定自定义字体,则忽略miniumScaleFactor。如果为属性传递nil,则正确缩放文本。

  • NSStringDrawingUsesLineFragmentOrigin 选项很重要。它告诉文本绘图系统,绘图rect的原点应该在左上角。

  • 无法使用新方法设置baselineAdjustment。你必须自己打电话来做 boundingRectWithSize:options:attributes:context: 首先,然后在传递之前调整矩形 drawWithRect:options:attributes:context


12
2017-11-05 11:47



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


答案:


它比以前复杂一点,你不能使用最小字体大小,但必须使用最小字体比例因子。 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的错误:如果在属性中指定自定义字体,则忽略miniumScaleFactor。如果为属性传递nil,则正确缩放文本。

  • NSStringDrawingUsesLineFragmentOrigin 选项很重要。它告诉文本绘图系统,绘图rect的原点应该在左上角。

  • 无法使用新方法设置baselineAdjustment。你必须自己打电话来做 boundingRectWithSize:options:attributes:context: 首先,然后在传递之前调整矩形 drawWithRect:options:attributes:context


12
2017-11-05 11:47



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


谷歌搜索了很长一段时间后,我找不到在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];

1
2018-02-09 22:11



这种解决方法实际上有效,即使它有点脏。谢谢! - myell0w


只需创建一个具有该键和属性的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];

0
2017-09-25 11:30



添加minFontSize作为键并没有真正改变任何东西。 - Rob
是的,对不起,我没有正确阅读你的问题,我在文档中找不到任何与minFontSize完全相同的密钥,但也许使用这个密钥可以工作,“NSStrokeWidthAttributeName”,希望它有帮助 - heczaco


我使用以下方法来解决我的问题。 使用下面的代码,您可以在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 ]}];


0
2017-07-18 12:14



为此答案添加一些额外的上下文,以帮助人们理解为什么这是一个很好的答案,而不仅仅是发布原始代码。 - Aaron