Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

ios - When do adjustsFontSizeToFitWidth or boundingRectWithSize change the context.actualScaleFactor?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

do this in viewDidAppear... That worked for me


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...