文本随着宽度而改变frame
UILabel *_nickname = [[UILabel alloc]init];
//_accountHome 是model 对象
NSString *nickStr = _accountHome.user_name ? _accountHome.user_name :@"去玩吗";
_nickname.text = [NSString stringWithFormat:@" %@ ",nickStr];
// 文本的宽度(随着文本字数而改变)
NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:14]};
CGSize size=[_nickname.text sizeWithAttributes:attrs];
float nickWidth = size.width;//LTWPercentage(150);
if (nickWidth > LTWidth - LTWPercentage(80)) {// 给宽度一个最大限定值
nickWidth = LTWidth - LTWPercentage(80);
}
_nickname.frame = CGRectMake((LTWidth - nickWidth)/2 ,CGRectGetMaxY(photoImg.frame) + LTPercentage(10), nickWidth, LTPercentage(30));
_nickname.layer.borderWidth = 1;
[self.homeHeaderView addSubview:_nickname];