在iOS7发布时,不推荐使用以下函数:
drawAtPoint:forWidth:withFont:minFontSize:actualFontSize:lineBreakMode:baselineAdjustment:
在Apple的文档中,它建议使用
drawInRect:withAttributes:
我使用这个功能的原因是因为 minFontSize
参数,它让我在rect中绘制一个字符串。如果文本不适合,它将首先缩小文本大小 minFontSize
如果它不合适,它将截断它。到目前为止,我无法完成此任务 drawInRect:withAttributes:
任何人都可以帮我确定哪个键可以用来确定 minFontSize
当量?
谢谢!
-抢
它比以前复杂一点,你不能使用最小字体大小,但必须使用最小字体比例因子。 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
。
它比以前复杂一点,你不能使用最小字体大小,但必须使用最小字体比例因子。 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
。
谷歌搜索了很长一段时间后,我找不到在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];
只需创建一个具有该键和属性的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];
我使用以下方法来解决我的问题。
使用下面的代码,您可以在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 ]}];