iOS 根据文字自适应高度
2016-04-28 10:40
302 查看
Objective-C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView == kindTableView)
{
NSInteger section = indexPath.section;
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:customCellView];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
cell = _customCell;
}
//下面将会用到item
FileListItem * item = [cuArray objectAtIndex:indexPath.row];
cell.textLabel.text = item.name;
//主要是这里
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cell.textLabel.text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = cell.textLabel.frame;
cell.textLabel.frame = CGRectMake(frame.origin.x, ((labelSize.height + 20)-frame.size.height)/2, frame.size.width, frame.size.height);
}
return cell;
}
//设置cell每行间隔的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//这里的item为什么设置cell时的内容
NSString *cellText = item.name;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 20;
}
swift
//计算文字高度
extension UILabel {
func calculateSize() -> CGFloat {
let options: NSStringDrawingOptions = .UsesLineFragmentOrigin
let string = self.text
let boundingRect = string!.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max), options: options, attributes: [NSFontAttributeName:self.font], context: nil)
return boundingRect.height
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView == kindTableView)
{
NSInteger section = indexPath.section;
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:customCellView];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
cell = _customCell;
}
//下面将会用到item
FileListItem * item = [cuArray objectAtIndex:indexPath.row];
cell.textLabel.text = item.name;
//主要是这里
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cell.textLabel.text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = cell.textLabel.frame;
cell.textLabel.frame = CGRectMake(frame.origin.x, ((labelSize.height + 20)-frame.size.height)/2, frame.size.width, frame.size.height);
}
return cell;
}
//设置cell每行间隔的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//这里的item为什么设置cell时的内容
NSString *cellText = item.name;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 20;
}
swift
//计算文字高度
extension UILabel {
func calculateSize() -> CGFloat {
let options: NSStringDrawingOptions = .UsesLineFragmentOrigin
let string = self.text
let boundingRect = string!.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max), options: options, attributes: [NSFontAttributeName:self.font], context: nil)
return boundingRect.height
}
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- Apple Swift学习教程
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 介绍 Fedora 上的 Swift
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift自定义iOS中的TabBarController并为其添加动画
- Swift编程中的泛型解析