ios开发中往往需要动态的获取UILabel的高度,因为文字长度是随机的,方法有很多,虽然也可以用AutoLayout实现,但是如果涉及到UITableViewCell的高度计算时还是需要动态计算。
1、利用NSString的扩展方法一(不推荐)
//方法:- (CGSize)sizeWithAttributes:(NSDictionary *)attrs NS_AVAILABLE_IOS(7_0);
NSString *string = @"Hello world, Hello world, Hello world, Hello world, Hello world.";
CGSize size = [string sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}];
2、利用NSString的扩展方法二(3星推荐)
//方法:- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
NSString *string = @"Hello world, Hello world, Hello world, Hello world, Hello world.";
CGSize size = [string boundingRectWithSize:CGSizeMake(300, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size;
根据实际测试结果,单行情况两种方法获取的高度是相等的,但是后一种方法明显更好,尤其是高度是动态的时候。
3、利用NSAttributedString的扩展方法(5星推荐)
NSDictionary *attrDict = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Hello world, Hello world, Hello world, Hello world, Hello world." attributes:attrDict];
CGSize size = [attrString boundingRectWithSize:CGSizeMake(300, 0) options:(NSStringDrawingUsesLineFragmentOrigin) context:nil].size;
4、使用UIView的sizeThatFit或者sizeToFit。
5、关于AutoLayout的使用
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.labelMsg = [[UILabel alloc] initWithFrame:CGRectZero];
[self.labelMsg setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.labelMsg setLineBreakMode:NSLineBreakByWordWrapping];
[self.labelMsg setNumberOfLines:0];
UIFont *fontMsg = [UIFont fontWithName:@"Avenir-Book" size:17.0f];
[self.labelMsg setFont:fontMsg];
[self.contentView addSubview:self.labelMsg];
self.labelName = [[UILabel alloc] initWithFrame:CGRectZero];
[self.labelName setTranslatesAutoresizingMaskIntoConstraints:NO];
UIFont *fontName = [UIFont fontWithName:@"Avenir-Black" size:18.0f];
[self.labelName setFont:fontName];
[self.contentView addSubview:self.labelName];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.labelMsg, @"labelMsg", self.labelName, @"labelName", nil];
NSString *vflA = @"|-10-[labelMsg]-10-|";
NSString *vflAA = @"V:|-5-[labelMsg]";
NSString *vflC = @"|-10-[labelName]-10-|";
NSString *vflCC = @"V:[labelName]-5-|";
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflA options:NSLayoutFormatAlignAllLeft metrics:nil views:dic]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflAA options:NSLayoutFormatAlignAllLeft metrics:nil views:dic]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflC options:NSLayoutFormatAlignAllLeft metrics:nil views:dic]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vflCC options:NSLayoutFormatAlignAllLeft metrics:nil views:dic]];
}
return self;
}
开源的AutoLayout的框架有Masonry,git上面有,使用比较方便。
4、自动布局QNYGKit框架(本人实现)
基于Yoga实现的iOS动态布局框架。
- 基于Yoga实现,遵循FlexBox协议,性能高,对项目侵入性较低;
- QNLayout布局方便,支持链式操作,虚拟视图Div,异步计算size,多种方式计算size,布局缓存与失效
- 完全使用Div计算view的frame体系,无需创建真实view,把view的布局计算完全独立开来
- 基于协议实现兼容UITableView的使用,将数据、布局、view三者逻辑上独立
- 性能和Native基本一致
- 相对完善的单元测试
网友评论