UIlabel字段部分字体高亮突出显示,如下图:
NSString *str = @"芝麻信用高于650的用户,租赁商品将享受部分免押金特权";
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:str];
[attributedStr addAttribute:NSForegroundColorAttributeName value:kColor(@"#2196F3") range:NSMakeRange(6, 3)];
[attributedStr addAttribute:NSForegroundColorAttributeName value:kColor(@"#2196F3") range:NSMakeRange(22, 5)];
_detalTextLabel.attributedText = attributedStr;
[self.View addSubview:_detalTextLabel];
计算文字宽度
- (CGFloat)calculateRowWidth:(NSString *)string Height:(CGFloat)textHeight fontSize:(NSInteger) fontSize{
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize: fontSize]}; //指定字号
CGRect rect = [string boundingRectWithSize:CGSizeMake(0, textHeight)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:nil];
return rect.size.width;
}
计算文字高度
- (CGFloat)calculateRowHeight:(NSString *)string Width:(CGFloat)textWidth fontSize:(NSInteger)fontSize{
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};//指定字号
CGRect rect = [string boundingRectWithSize:CGSizeMake(textWidth, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:nil];
return rect.size.height;
}