UILabel * label = [[UILabel alloc] init]; web
NSString * text = @"hellohellohellohellohellohellohellohellohellohellohellohello "; app
label.text = text; 字体
[label setNumberOfLines:0]; spa
UIFont *font = [UIFont fontWithName:@"Arial" size:14]; orm
//设置字体 ip
label.font = font; it
CGSize size = CGSizeMake(300, 20000.0f); io
size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping]; import
[label setFrame:CGRectMake(10, 0, size.width, size.height)]; webkit
[self.view addSubview:label];
lineBreakMode:设置标签文字过长时的显示方式。
label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内容长度,后半部分被删除。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,显示尾部文字内容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容以……方式省略,显示头尾的文字内容。
label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容以……方式省略,显示头的文字内容。
label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显示,后面部分省略不显示。
UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0f]};
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height);