问题
When does the actualScaleFactor of an NSStringDrawingContext change?
The documentation says:
"If you specified a custom value in the minimumScaleFactor property, when drawing is complete, this property contains the actual scale factor value that was used to draw the string."
My code:
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
Console log for both text that fits without being shrunk and longer text that is adjusted to fit the label's width are both the same:
context: 0.0
actual context after drawing: 1.0
Any idea what step I am missing to get a real scaleFactor from context after the text has been sized to fit the label?
回答1:
The same problem as @RanLearns.
After add myButton.titleLabel!.minimumScaleFactor = 0.1
it worked for me
回答2:
do this in viewDidAppear... That worked for me
来源:https://stackoverflow.com/questions/34622503/when-do-adjustsfontsizetofitwidth-or-boundingrectwithsize-change-the-context-act