加粗;
[UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
加粗并且倾斜
[UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];
guessLikeLabel.font = UIFont(name: "Helvetica-Bold", size: 14)
//初始化label
UILabel *label = [[UILabel alloc] init];
NSString *text = @"这是一个测试!!!adsfsaf时发生发勿忘我勿忘我勿忘我勿忘我勿忘我阿阿阿阿阿阿阿阿阿阿阿阿阿啊00000000阿什顿。。。";
label.text = text;
[label setNumberOfLines:0];
UIFont *font = [UIFont fontWithName:@"Arial" size:14];
//设置字体
label.font = font;
CGSize constraint = CGSizeMake(300, 20000.0f);
CGSize size = [text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(10, 0, size.width, size.height)];
[self.view addSubview:label];
iOS7 UILabel 内容自适应 - OBJC
2014年08月29日
//iOS7放弃使用“ CGSize actualsize = [tstring sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping]; ”
//基本设置
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
NSString *text = @"测试:阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿啊阿asdafdddddaaaaaaaaajhkjkgghghghghghghghghghgghghhghghghghghghg啊阿啊阿啊阿啊阿啊阿啊";
label.text = text;
UIFont *textFont = [UIFont systemFontOfSize:14.f];
label.font = textFont;
//折行
label.lineBreakMode = NSLineBreakByWordWrapping;
//必须写,否则只显示一行
[label setNumberOfLines:0];
[self.view addSubview:label];
//最大尺寸
// MAXFLOAT 为可设置的最大高度
CGSize size = CGSizeMake(300, MAXFLOAT);
//获取当前那本属性
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:textFont,NSFontAttributeName, nil];
//实际尺寸
CGSize actualSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
label.frame = CGRectMake(10, 70, actualSize.width,actualSize.height);
//下面是自己在项目中写的, 当时出错了,因为在cellForItemAtIndexPath是先初使化cell,然后在赋值,所在初使化(cell的init中是拿不到_detailText,所以_detailTextLabel.text也是没有值的,不能计算frame)
UIFont *textFont = [UIFont systemFontOfSize:12.f];
_detailTextLabel.numberOfLines = 0;//设置无限换行
_detailTextLabel.lineBreakMode = NSLineBreakByCharWrapping; //自动折行
//CGSize size = [_detailTextLabel.text boundingRectWithSize:_detailTextLabel.frame options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:nil context:nil];
//设置最大尺寸 10000.0( MAXFLOAT )为可设置的最大高度
CGSize constraint = CGSizeMake([[UIScreen mainScreen] bounds].size.width-20, 10000.0);
//获取当前文本属性
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:textFont,NSFontAttributeName, nil];
//实际尺寸
CGSize actualSize = [_detailTextLabel.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
_detailTextLabel.frame = CGRectMake(10, _topicImageView.frame.size.height+60+1+15, actualSize.width, actualSize.height);
UILabel 内容自适应 - swift
var detailTextLabel = UILabel()
dtext = detailTextLabel.text
detailTextLabel.font= UIFont.systemFontOfSize(12)
//numberOfLines //标签最多显示行数,如果为0则表示多行 即设置无限换行。
detailTextLabel.numberOfLines = 0
detailTextLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping //自动折行
//根据detailText文字长度调节topView高度
let constraint = CGSize(width: topView.frame.size.width, height: 10000.0)
var size =dtext.boundingRectWithSize(constraint, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: NSDictionary(object:UIFont.systemFontOfSize(12), forKey: NSFontAttributeName) as [NSObject : AnyObject] ,context: nil)
detailTextLabel.frame = CGRectMake(10, topicImageView.frame.size.height+60+1+15, size.width, size.height)