标签:uilabel 自适应 sizetofit boundingrectwithsize sizewithfont
示例代码如下:
#define IOS7_OR_LATER ( [[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending )
CGSize size;
NSString *strTail = @"allColorDictallColorDictallColorDtallColorDictallC";
UIFont *font = [UIFont systemFontOfSize:15.0f];
NSDictionary *attribute = @{NSFontAttributeName: font};
if (IOS7_OR_LATER) {
size = [strTail boundingRectWithSize:self.view.frame.size
options:
NSStringDrawingTruncatesLastVisibleLine|
NSStringDrawingUsesLineFragmentOrigin|
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
}else{
size = [strTail sizeWithFont:font constrainedToSize:CGSizeMake(300, 30)];
}利用UIView的sizeToFit方法,设置好字体大小,文字内容之后调用即可,size自动就会变化,我们自然也就知道size是多少了,比较方便。
示例代码如下:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; label.text = @"tallColorDictallColorDictallColorDictallColorDictallColorDtallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorDictallColorD"; [label sizeToFit]; [self.view addSubview:label];
UILabel的高度(height)自适应文字内容(text)
标签:uilabel 自适应 sizetofit boundingrectwithsize sizewithfont
原文地址:http://blog.csdn.net/u011439689/article/details/42642781