设置UILabel可变高度(根据文本内容自动适应高度)
2016-05-12 11:01
609 查看
@property(nonatomic)UILabel *showLabel;
// 计算文本所占高度,计算出来之后设置label的高度
// 第一个参数:字体大小,字体大小/样式影响计算字体的高度
// 第二个参数:CGSize结构体,结构体中第一个参数表示宽度,宽度的设置影响计算文本的高度,很明显越宽,高度越小;结构体中第二个参数表示最大能有多高,比如我们写为100,那么即使文本高度计算出来是200, 这个方法也会返回100, 所以一般情况下我们把它写为MAXFLOAT, 表示能有多高返回多高
if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 7.0) {
CGSize size = [str sizeWithFont:self.showLabel.font constrainedToSize:CGSizeMake(self.showLabel.frame.size.width, MAXFLOAT)];
// 计算出文本高度后,重新给label赋值
CGRect frame = self.showLabel.frame;
frame.size.height = size.height;
self.showLabel.frame = frame;
} else {
/*
// 计算text的高度,改变label的高,让label适应这些文字
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:18.0] constrainedToSize:CGSizeMake(self.targetLabel.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
// 第一个参数:字体
// 第二个参数:constraintedSize
// 第三个参数:换行模式
*/
// after iOS7: boundingRectWithSize:options:attributes:context:
CGSize textSize = [str boundingRectWithSize:CGSizeMake(self.showLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0]} context:nil].size;
// 选项options:
/**
* NSStringDrawingUsesLineFragmentOrigin:
绘制文本时使用 line fragement origin 而不是 baseline origin
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine
* NSStringDrawingUsesFontLeading:
计算行高时使用行距。(译者注:字体大小+行间距=行距)
* NSStringDrawingTruncatesLastVisibleLine:
如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。
* NSStringDrawingUsesDeviceMetrics:
计算布局时使用图元字形(而不是印刷字体)。
*/
CGRect frame = self.showLabel.frame;
frame.size.height = textSize.height;
self.showLabel.frame = frame;
}
self.showLabel.text = @"xxxxxxx";//你的文本内容
【拓】同理可以设计一个可变高度(根据内容自适应高度)的 UITableViewCell
1) 创建并添加一个 UILabel 作为单元格 cell 的子视图;
2) 在 UITableView 的委托方法:
(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:
(NSIndexPath *) indexPath 中计算高度
3) 在 UITableView 的委托方法:
(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexP
ath: (NSIndexPath *) indexPath 中计算 UILabel 的框大小。
// 计算文本所占高度,计算出来之后设置label的高度
// 第一个参数:字体大小,字体大小/样式影响计算字体的高度
// 第二个参数:CGSize结构体,结构体中第一个参数表示宽度,宽度的设置影响计算文本的高度,很明显越宽,高度越小;结构体中第二个参数表示最大能有多高,比如我们写为100,那么即使文本高度计算出来是200, 这个方法也会返回100, 所以一般情况下我们把它写为MAXFLOAT, 表示能有多高返回多高
if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 7.0) {
CGSize size = [str sizeWithFont:self.showLabel.font constrainedToSize:CGSizeMake(self.showLabel.frame.size.width, MAXFLOAT)];
// 计算出文本高度后,重新给label赋值
CGRect frame = self.showLabel.frame;
frame.size.height = size.height;
self.showLabel.frame = frame;
} else {
/*
// 计算text的高度,改变label的高,让label适应这些文字
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:18.0] constrainedToSize:CGSizeMake(self.targetLabel.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
// 第一个参数:字体
// 第二个参数:constraintedSize
// 第三个参数:换行模式
*/
// after iOS7: boundingRectWithSize:options:attributes:context:
CGSize textSize = [str boundingRectWithSize:CGSizeMake(self.showLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0]} context:nil].size;
// 选项options:
/**
* NSStringDrawingUsesLineFragmentOrigin:
绘制文本时使用 line fragement origin 而不是 baseline origin
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine
* NSStringDrawingUsesFontLeading:
计算行高时使用行距。(译者注:字体大小+行间距=行距)
* NSStringDrawingTruncatesLastVisibleLine:
如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。
* NSStringDrawingUsesDeviceMetrics:
计算布局时使用图元字形(而不是印刷字体)。
*/
CGRect frame = self.showLabel.frame;
frame.size.height = textSize.height;
self.showLabel.frame = frame;
}
self.showLabel.text = @"xxxxxxx";//你的文本内容
【拓】同理可以设计一个可变高度(根据内容自适应高度)的 UITableViewCell
1) 创建并添加一个 UILabel 作为单元格 cell 的子视图;
2) 在 UITableView 的委托方法:
(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:
(NSIndexPath *) indexPath 中计算高度
3) 在 UITableView 的委托方法:
(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexP
ath: (NSIndexPath *) indexPath 中计算 UILabel 的框大小。
相关文章推荐
- iOS UITleView 属性及方法详解
- iOS ViewController添加Segue
- findui环境搭建
- 关于UITextView的几种回收键盘的方法
- 设置UIButton的文字显示位置、字体的大小、字体的颜色
- LeetCode No347. Top K Frequent Elements
- 反编译dex2jar和jar-GUI的命令
- easyui combotree选项重复
- warning: suggest parentheses around assignment used as truth value
- UI3D特效动态加载
- UI特效层级管理
- Error Code: 1235. This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
- INSERT INTO t_inertupdate (a,c) VALUES(15,a*2);
- iOS经典讲解之UILabel居上居下显示
- RequestDispatcher提供两个方法:forward,include有什么区别
- UITextView 使用
- apue.h头文件错误和error错误处理
- [iOS]实现UITableView头部带有图片并且下拉图片放大效果
- Android recovery UI实现分析
- easyui combotree加载静态前台json问题