文本隨著寬度而改變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];