导读
1.设置label高度为文字的高度,宽度固定
[Objective-C] 纯文本查看 复制代码 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 50)];
label.numberOfLines = 0;
label.font = kFont(14);
label.text = @"苏飞论坛,欢迎你!终身VIP 限时特惠";
label.frameHeight = getTextHeight(label.text, label.font, label.frameWidth);
2.设置label宽度为文字的宽度,高度一定
[Objective-C] 纯文本查看 复制代码 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 50)];
label.numberOfLines = 0;
label.font = kFont(14);
label.text = @"苏飞论坛,欢迎你!终身VIP 限时特惠";
label.frameWidth = tWidthByFont(14, label.text);
还有另一个方法,
[Objective-C] 纯文本查看 复制代码 label.frameWidth = getLabelTextWidth(label);
3.获取单行文本的高度
[Objective-C] 纯文本查看 复制代码 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 50)];
label.numberOfLines = 0;
label.font = kFont(14);
label.text = @"苏飞论坛,欢迎你!终身VIP 限时特惠";
label.frameHeight = tHeightByFont(label.font);
4.获取单行文本的宽度
[Objective-C] 纯文本查看 复制代码 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 50)];
label.numberOfLines = 0;
label.font = kFont(14);
NSString *string = @"苏飞论坛,欢迎你!终身VIP 限时特惠 苏飞论坛,欢迎你!终身VIP 限时特惠 苏飞论坛,欢迎你!终身VIP 限时特惠";
label.text = string;
label.frameWidth = tWidthByFont(14, string);
5.获取带行高的文本的高度
[Objective-C] 纯文本查看 复制代码 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 50)];
label.numberOfLines = 0;
label.font = kFont(14);
NSString *string = @"苏飞论坛,欢迎你!终身VIP 限时特惠 苏飞论坛,欢迎你!终身VIP 限时特惠 苏飞论坛,欢迎你!终身VIP 限时特惠";
label.attributedText = getMutableAttributeStringWithLineHeight(string, @"", nil, 14, 8);
label.frameHeight = getTextHeightWithLineHeight(string, label.font, label.frameWidth, 8);
框架源码下载
|