8

NSStringDrawingContext 的实际ScaleFactor 什么时候改变?

文档说:

“如果您在 minimumScaleFactor 属性中指定了自定义值,则绘制完成后,此属性包含用于绘制字符串的实际比例因子值。”

我的代码:

myButton.titleLabel!.font = UIFont(name: "AmericanTypewriter-Bold", size: 40)
myButton.titleLabel?.adjustsFontSizeToFitWidth = true
myButton.setTitle("\(textString)", forState: .Normal)

let attributes = [NSFontAttributeName : myButton.titleLabel!.font]
let attributedString = NSMutableAttributedString(string:textString, attributes:attributes)

let context = NSStringDrawingContext()
context.minimumScaleFactor = myButton.titleLabel!.minimumScaleFactor

print("context: \(context.actualScaleFactor)")

let resultingRect = attributedString.boundingRectWithSize(myButton.titleLabel!.bounds.size, options: .UsesLineFragmentOrigin, context: context)

print("actual context after drawing: \(context.actualScaleFactor)")

//want to get the font size after adjustsFontSizeToFitWidth has done its magic:      
//let actualFontSize = myButton.titleLabel!.font.pointSize * context.actualScaleFactor

适合而不收缩的文本和调整以适合标签宽度的较长文本的控制台日志都是相同的:

context: 0.0
actual context after drawing: 1.0

知道在调整文本大小以适合标签后,我缺少什么步骤来从上下文中获取真正的 scaleFactor 吗?

4

2 回答 2

0

在 viewDidAppear 中执行此操作...这对我有用

于 2016-04-27T07:15:03.813 回答
0

与@RanLearns 相同的问题。

添加后myButton.titleLabel!.minimumScaleFactor = 0.1它对我有用

于 2016-07-20T07:26:22.880 回答