14

に設定されたラベルがadjustsFontSizeToFitWidth = YESあり、実際に表示されるフォント サイズを取得する必要があります。

現在、iOS 7 では、以前は機能していたすべてのメソッドが廃止され、SO に関するすべての質問は、これらの廃止されたメソッドの使用を提案しています。

SOの許可が下り次第、この質問を報奨金にします。閉じないでください。

4

4 に答える 4

0

iOS 7でadjustsFontSizeToFitWidthを使用した場合、UILabelはfontSizeを表示しました

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
label.text = @" Your Text goes here into this label";
label.adjustsFontSizeToFitWidth = YES;

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
// Get the theoretical font-size
[attrStr setAttributes:@{NSFontAttributeName:label.font} range:NSMakeRange(0, attrStr.length)];

NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = label.minimumScaleFactor;
[attrStr boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin context:context];
CGFloat theoreticalFontSize = label.font.pointSize * context.actualScaleFactor;

NSLog(@"theoreticalFontSize: %f",theoreticalFontSize);
NSLog(@"AttributedString Width: %f", [attrStr size].width);
double scaleFactor=label.frame.size.width/([attrStr size].width);
double displayedFontSize=theoreticalFontSize*scaleFactor;
NSLog(@"Actual displayed Font Size: %f",displayedFontSize);

// Verification of Result
double verification=(displayedFontSize * [attrStr length]);
NSLog(@"Should be equal to %0.5f: %0.5f ", [attrStr size].width/17.0, label.frame.size.width/displayedFontSize);
于 2016-10-05T06:47:12.813 に答える
-2

それを可能にする読み取り専用プロパティがあります。こんな感じでアクセスできます

nameLabel.adjustsFontSizeToFitWidth = YES;

//Make sure to use the line below AFTER the line above

float fontSize = nameLabel.font.xHeight;

これにより、幅に合わせて調整された後のフォントサイズが得られます。

于 2013-10-06T21:45:08.277 に答える