8

NSStringDrawingContext の actualScaleFactor はいつ変更されますか?

ドキュメントには次のように記載されています。

「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

@RanLearns と同じ問題。

追加した後、myButton.titleLabel!.minimumScaleFactor = 0.1それは私のために働いた

于 2016-07-20T07:26:22.880 に答える