什么时候adjustsFontSizeToFitWidth或boundingRectWithSize更改context.actualScaleFactor?

什么时候NSStringDrawingContext的actualScaleFactor改变?

该文件说:

“如果您在minimumScaleFactor属性中指定了一个自定义值,则绘图完成时,此属性将包含用于绘制string的实际缩放因子值。”

我的代码:

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 

控制台logging两个文本适合没有收缩和较长的文本,调整到适合标签的宽度都是相同的:

 context: 0.0 actual context after drawing: 1.0 

任何想法是什么步骤,我已经错过了从上下文得到一个真正的scaleFactor之后,文本的大小,以适应标签?

这样做viewDidAppear …这对我工作

与@RanLearns相同的问题。

添加myButton.titleLabel!.minimumScaleFactor = 0.1它为我工作