UILabel
* testlable = [[UILabel alloc]initWithFrame:CGRectMake(
10
,
20
,
200
,
20
)];
NSString
* tstring =@
"UILabel
ios7 与ios7之前实现自适应撑高的方法,文本的内容长度不一,我们需要根据内容的多少来自动换行处理。在IOS7下要求font,与breakmode与之前设置的完全一致sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping"
;
testlable.numberOfLines
=
2
;
UIFont
* tfont = [UIFont systemFontOfSize:
14
];
testlable.font
= tfont;
testlable.lineBreakMode
=NSLineBreakByTruncatingTail ;
testlable.text
= tstring ;
[testlable
setBackgroundColor:[UIColor redColor]];
[self.view
addSubview:testlable];
CGSize
size =CGSizeMake(
300
,
60
);
NSDictionary
* tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
CGSize
actualsize =[tstring boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:tdic context:nil].size;
testlable.frame
=CGRectMake(
10
,
20
,
actualsize.width, actualsize.height);