iOS 计算文字宽度
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 计算宽度!!
static int count = 0;
NSString *str = @"";
if (count % 2 == 0) {
str = @"剩余18460天12小时20分79秒";
}else if (count % 3 == 0) {
str = @"已过去60天12小时20分79秒";
}else if (count % 5 == 0) {
str = @"已过去60天12小时20分79秒已过去60天12小时20分79秒";
}else if (count % 7 == 0) {
str = @"活动已结束!";
}else {
str = @"1122";
}
_testLable.text = str;
CGFloat width1=[(NSString *)str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 21) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size.width;
_lableWidth_Constraint.constant = width1 + 5;
count++;
}
网友评论