UILabel的一些小方法
程序员文章站
2022-05-19 18:57:11
...
1.调整UILabel的行间距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:building];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:6];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [building length])];
2.自适应UILabel的高度
第一种方法
CGSize maxSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width-25, MAXFLOAT);
CGRect rect1 =
[building boundingRectWithSize:maxSize
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10]}
context:nil];
第二种方法
[self.introduceLabel sizeToFit];
CGFloat labelHeight = [self.introduceLabel sizeThatFits:CGSizeMake(self.introduceLabel.frame.size.width, MAXFLOAT)].height;
3.改变UILabel的字体大小和颜色
NSString * string = [NSString stringWithFormat:@"%@ %@",dict[@"UserNickName"],shop];
NSRange range = [string rangeOfString:@"["];
NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:string];
CGFloat font = 9;
if (IPHONE_HEIGHT == 667) {
font = 10;
}else if(IPHONE_HEIGHT == 736){
font = 11;
}
[mAttStri addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:font] range:NSMakeRange(range.location, string.length - range.location)];
[mAttStri addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:196/255.0 green:196/255.0 blue:196/255.0 alpha:1] range:NSMakeRange(range.location, string.length - range.location)];
self.titleCityLabel.attributedText =mAttStri;
转载于:https://www.jianshu.com/p/9a709cc1dcda
上一篇: 婆婆最不喜欢哪种媳妇?
下一篇: 自定义文件选择器