When do adjustsFontSizeToFitWidth or boundingRectWithSize change the context.actualScaleFactor?

笑着哭i 提交于 2020-01-10 05:03:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!