UILabel 获取当 adjustsFontSizeToFitWidth 为 YES 时显示的字体大小
UILabel get the displayed font size when adjustsFontSizeToFitWidth is YES
我有一个 UILabel
,adjustsFontSizeToFitWidth
设置为 YES
,正如预期的那样,当文本太大而无法按font
属性.
我已经查询了 font
属性 但这仍然和最初设置在标签上时一样,而且 UILabel
似乎没有任何其他属性可能会有用。
有没有办法知道标签绘制的缩小文字的大小是多少?
编辑:不像建议的那样重复,因为 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
在 iOS7
中被弃用
事实证明它有点复杂,因为 iOS 7 弃用了有用的 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
, as used in the suggested duplicate。
我们现在必须改用 NSAttributedString
。
从要在标签上设置的字符串创建一个NSAttributedString
。将整个属性字符串的字体设置为标签的字体。
NSDictionary *attributes = @{NSFontAttributeName : self.label.font};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text
attributes:attributes];
然后,创建一个NSStringDrawingContext
对象,配置为使用标签的最小比例因子。这将用于辅助计算实际字体大小。
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
context.minimumScaleFactor = self.label.minimumScaleFactor;
然后我们可以使用NSAttributedString
方法计算bounding rect,进一步配置NSStringDrawingContext
:
[attributedString boundingRectWithSize:self.label.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
context:context];
这将使绘图通过并且上下文将被更新以包括用于在 space 可用(标签的大小)中绘制文本的比例因子。
获取实际字体大小就这么简单:
CGFloat actualFontSize = self.label.font.pointSize * context.actualScaleFactor;
我已经在 link 中回答了您关于 Swift 5 的问题,类似于你的问题
但是,我们可以通过如下方式得到actualFontSize:
对于单行 UILabel
extension UILabel {
var actualFontSize: CGFloat {
//initial label
let fullSizeLabel = UILabel()
fullSizeLabel.font = self.font
fullSizeLabel.text = self.text
fullSizeLabel.sizeToFit()
var actualFontSize: CGFloat = self.font.pointSize * (self.bounds.size.width / fullSizeLabel.bounds.size.width);
//correct, if new font size bigger than initial
actualFontSize = actualFontSize < self.font.pointSize ? actualFontSize : self.font.pointSize;
return actualFontSize
}
}
获取实际字体大小就这么简单:
let currentLabelFontSize = myLabel.actualFontSize
我有一个 UILabel
,adjustsFontSizeToFitWidth
设置为 YES
,正如预期的那样,当文本太大而无法按font
属性.
我已经查询了 font
属性 但这仍然和最初设置在标签上时一样,而且 UILabel
似乎没有任何其他属性可能会有用。
有没有办法知道标签绘制的缩小文字的大小是多少?
编辑:不像建议的那样重复,因为 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
在 iOS7
事实证明它有点复杂,因为 iOS 7 弃用了有用的 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
, as used in the suggested duplicate。
我们现在必须改用 NSAttributedString
。
从要在标签上设置的字符串创建一个NSAttributedString
。将整个属性字符串的字体设置为标签的字体。
NSDictionary *attributes = @{NSFontAttributeName : self.label.font};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text
attributes:attributes];
然后,创建一个NSStringDrawingContext
对象,配置为使用标签的最小比例因子。这将用于辅助计算实际字体大小。
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
context.minimumScaleFactor = self.label.minimumScaleFactor;
然后我们可以使用NSAttributedString
方法计算bounding rect,进一步配置NSStringDrawingContext
:
[attributedString boundingRectWithSize:self.label.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
context:context];
这将使绘图通过并且上下文将被更新以包括用于在 space 可用(标签的大小)中绘制文本的比例因子。
获取实际字体大小就这么简单:
CGFloat actualFontSize = self.label.font.pointSize * context.actualScaleFactor;
我已经在 link 中回答了您关于 Swift 5 的问题,类似于你的问题
但是,我们可以通过如下方式得到actualFontSize: 对于单行 UILabel
extension UILabel {
var actualFontSize: CGFloat {
//initial label
let fullSizeLabel = UILabel()
fullSizeLabel.font = self.font
fullSizeLabel.text = self.text
fullSizeLabel.sizeToFit()
var actualFontSize: CGFloat = self.font.pointSize * (self.bounds.size.width / fullSizeLabel.bounds.size.width);
//correct, if new font size bigger than initial
actualFontSize = actualFontSize < self.font.pointSize ? actualFontSize : self.font.pointSize;
return actualFontSize
}
}
获取实际字体大小就这么简单:
let currentLabelFontSize = myLabel.actualFontSize