【发布时间】:2017-06-01 21:59:08
【问题描述】:
我建立的这种方法在英语中非常有效。
现在我也在尝试支持希伯来语,而boundingRectWithSize 错过了高度(给我的东西太短了)
不知道为什么……
+ (void) setTextAndFitLabel:(UILabel *)label text:(NSString *)text
{
[label setNumberOfLines:0];
text = [text stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
[label setText:text];
NSDictionary *attributes = @{NSFontAttributeName: label.font};
CGRect rect = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX )
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil];
if ([text isEqualToString:@""])
{
rect = CGRectMake(0,0,0,0);
}
CGRect labelFrame = label.frame;
labelFrame.size.height = ceil(rect.size.height);
NSArray *constraints = [label constraints];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", NSLayoutAttributeHeight];
NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];
if(filteredArray.count == 0){
label.frame = labelFrame;
}
else
{
NSLayoutConstraint *heightConstraint = [filteredArray objectAtIndex:0];
heightConstraint.constant = ceil(rect.size.height);
}
}
【问题讨论】:
-
嗨,波阿兹!你想通了吗?这让我疯了啊
-
3 年过去了...不记得了...抱歉...
-
无论如何,谢谢,我决定将我的设计更改为固定大小的字体。大声笑