是否可以同时使用minimumScaleFactor和归因文本字符串?
[myLabel setFrame:CGRectMake(6,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像素,那么你可以创建两个不同的标签,35像素和65像素.然后可以对这两个标签使用setAdjustsFontSizeToFitWidth.