Tim*_*nnö 10 cocoa cocoa-touch objective-c uilabel
是否可以同时使用minimumScaleFactor和属性文本字符串?
[myLabel setFrame:CGRectMake(6, 0, 200, 25)];
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"];
[attStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, 20)];
myLabel.attributedText = attStr;
[myLabel setAdjustsFontSizeToFitWidth:YES];
[myLabel setAdjustsLetterSpacingToFitWidth:YES];
[myLabel setMinimumScaleFactor:0.3];
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.如果我设置myLabel.text,它会按预期进行缩放.我如何缩放才能正常工作?
您可以获取前 20 个字符部分并获取其宽度,然后可以使用相同的方式获取其余部分的宽度:
CGSize maximumLabelSize = CGSizeMake(500.0,20.0);//write a really long width
//this will turn the expected length of the labels..
CGSize expectedFirstLabelSize = [[[[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"] substringToIndex:21]
sizeWithFont:[UIFont boldSystemFontOfSize:17] constrainedToSize:maximumLabelSize lineBreakMode:nil];
CGSize expectedLastLabelSize = [[[[NSMutableAttributedString alloc] initWithString:@"A very long string and its first 20 characters should be bold"] substringFromIndex:20]
sizeWithFont:[UIFont normalSystemFontOfSize:15] constrainedToSize:maximumLabelSize lineBreakMode:nil];
Run Code Online (Sandbox Code Playgroud)
那么你就会知道它们的长度比率..例如。第一部分是 140 px,其余部分是 260 px,标签区域是 100 px,然后您可以创建 2 个不同的标签,分别为 35 px 和 65 px。然后您可以对这两个标签使用 setAdjustsFontSizeToFitWidth。