我有一个UILabel与adjustsFontSizeToFitWidth设置为YES如预期它缩小屏幕上显示的字体当文本太大的规模要绘制font财产.
我已经查询了该font属性,但这仍然与最初设置在标签上时相同,并且UILabel似乎没有任何其他可用的属性.
有没有办法找出标签绘制的缩小文字的大小是多少?
编辑:不是建议的重复,因为sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:在iOS7中已弃用
Ste*_*ord 11
事实证明它有点复杂,因为iOS 7弃用了有用的sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:,如建议的副本中所使用的那样.
我们现在必须NSAttributedString改用.
NSAttributedString从字符串创建一个在标签上设置的/.将整个属性字符串的字体设置为标签的字体.
NSDictionary *attributes = @{NSFontAttributeName : self.label.font};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text
attributes:attributes];
Run Code Online (Sandbox Code Playgroud)
然后,创建一个NSStringDrawingContext对象,配置为使用标签的最小比例因子.这将用于帮助计算实际字体大小.
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
context.minimumScaleFactor = self.label.minimumScaleFactor;
Run Code Online (Sandbox Code Playgroud)
然后我们可以使用该NSAttributedString方法计算边界矩形,并进一步配置NSStringDrawingContext:
[attributedString boundingRectWithSize:self.label.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
context:context];
Run Code Online (Sandbox Code Playgroud)
这将进行绘图传递,并且将更新上下文以包括用于在可用空间(标签的大小)中绘制文本的比例因子.
获得实际的字体大小就像这样简单:
CGFloat actualFontSize = self.label.font.pointSize * context.actualScaleFactor;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2717 次 |
| 最近记录: |