是否可以同时使用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];
这似乎行不通。如果我设置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];
然后你就会知道它们的长度率。例如,第一部分为 140 像素,其余部分为 260 像素,标签区域为 100 像素,然后,您可以创建 2 个不同的标签,分别为 35 像素和 65 像素。然后,您可以对两个标签使用 setAdjustsFontSizeToFitWidth。