是否可以同时使用 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。
关于objective-c - UILabel attributedText 和 minimumScaleFactor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433481/