How to make UILabel / UITableViewCell to have a dynamic height
来源:互联网 发布:天天动听mac版下载 编辑:程序博客网 时间:2024/06/18 10:28
I recently found out, that even though according to the documentation sizeToFit on a UILabel will take into account the numberOfLines property, it simply doesn't.
You have seen variable heights of UITableViewCell in iPhone applications like App Store, Address Book, SMS and so on. These applications have a UITableView as main part, but each cell in the table has a variable height according to text amount it holds.
With UITableView it is quite different if you're coming from a HTML world,as UITableView cannot generate a dynamic height automatically, you'll need to calculate the height yourself before you set the delegate.
How to make UILabel / UITableViewCell to have a dynamic height
After you created a UITableView, you will need to set a delegate and a datasource. There is a UITableViewDelegate method to tell UITableView how tall a cell would be:
- - (CGFloat)tableView:(UITableView *)tableView
- heightForRowAtIndexPath:(NSIndexPath *)indexPath;
If you want UITableViewCell to have a dynmic height,you'll need to calculate the height of text block and return the resultin the method I mentioned above.
There are four NSString methods that can do the calculations:
- - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size;
- - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
- lineBreakMode:(UILineBreakMode)lineBreakMode;
- - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width
- lineBreakMode:(UILineBreakMode)lineBreakMode;
- - (CGSize)sizeWithFont:(UIFont *)font
- minFontSize:(CGFloat)minFontSize
- actualFontSize:(CGFloat *)actualFontSize
- forWidth:(CGFloat)width
- lineBreakMode:(UILineBreakMode)lineBreakMode;
To calculate the exact height of a text block, you'll need tospecify a large CGSize and those methods will return you exact size,here's an example:
- CGSize aSize;
- aSize = [aString sizeWithFont:[UIFont systemFontOfSize:14]
- constrainedToSize:CGSizeMake(300.0, 1000.0)
- lineBreakMode:UILineBreakModeTailTruncation];
Then you can get the height by accessing aSize.height.
What I'm doing most of the time is to call one of those methods inthe model class when setting the string and storing the height in themodel class, so you can use the correct height right away.
Now to size the UILabel and/or UITableViewCell you just need to set the frame property of UILabel and/or UITableViewCell.
- How to make UILabel / UITableViewCell to have a dynamic height
- How to make UITableViewCell have variable height
- How To Make A Keygen
- How to make a patch
- how-to-make-a-rpg
- How to make a ipcamera
- how to register a cell for UITableViewCell
- Android - How to have a tiled background
- How to Have a Healthy Relationship
- How to Make Dynamic Hyperlinks Using C# in ASP.NET
- Dynamic Enterprise Architecture: How to Make It Work
- How to make your Web Reference proxy URL dynamic
- How To Make a Tag Cloud
- how to make a JAR file
- how to make a driver pagable
- HOW TO - Make a Roomba Serial Interface
- How to Make Exercise a Daily Habit
- How To Make A Cookie Stealer
- 屏蔽掉phpinfo()的 方法
- GIS图片缓存工具GeoWebCache的使用说明
- Loadrunner关于页面检查的几个函数详解
- 存储过程编写经验和优化措施
- Pocket PC 2003 PC网卡上网设置
- How to make UILabel / UITableViewCell to have a dynamic height
- 谈谈软件开发人员基本要求
- JDBC对数据库的事务操作
- 卸载mysql
- web.xml文件的作用及基本配置
- linux下top命令参数解释
- 整车下线检测系统 EOL
- 常用正则表达式
- 为什么不报培训班