带有包装标签的静态表格单元格的动态高度? [英] Dynamic height for static table cells with wrapping labels?
本文介绍了带有包装标签的静态表格单元格的动态高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文字在纵向模式下有两行长.当我切换到横向模式时,它适合一行.我正在通过故事板使用静态 tableview 单元格;我怎样才能调整行的大小以适应它?
My text is two lines long in portrait mode. When I switch to landscape mode, it fits into a single line. I'm using static tableview cells via a storyboard; how can I resize the row to fit snugly?
屏幕是登录屏幕.
- 第一个单元格包含一些说明文本
- 第二个是用于输入帐户名称的文本字段
- 第三个是用于输入密码的安全文本字段
- 第四个(也是最后一个)单元格包含登录按钮.键盘上的返回键提交表单或适当切换焦点
推荐答案
使用 UITableView 的 heightForRowAtIndexPath
:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int topPadding = 10;
int bottomPadding = 10;
float landscapeWidth = 400;
float portraitWidth = 300;
UIFont *font = [UIFont fontWithName:@"Arial" size:22];
//This is for first cell only if you want for all then remove below condition
if (indexPath.row == 0) // for cell with dynamic height
{
NSString *strText = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label
if(landscape)//depends on orientation
{
CGSize maximumSize = CGSizeMake(landscapeWidth, MAXFLOAT); // change width and height to your requirement
}
else //protrait
{
CGSize maximumSize = CGSizeMake(portraitWidth, MAXFLOAT); // change width and height to your requirement
}
//dynamic height of string depending on given width to fit
CGSize textSize = CGSizeZero;
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
{
NSMutableParagraphStyle *pstyle = [NSMutableParagraphStyle new];
pstyle.lineBreakMode = NSLineBreakByWordWrapping;
textSize = [[strText boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :font,NSParagraphStyleAttributeName:[pstyle copy]} context:nil] size];
}
else // < (iOS 7.0)
{
textSize = [strText sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping]
}
return (topPadding+textSize.height+bottomPadding) // caculate on your bases as u have string height
}
else
{
// return height from the storyboard
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
EDIT :为 > 的
和 as support
添加和 <ios7sizeWithFont
方法在 iOS 7.0 中已弃用
EDIT : Added for support
for > and < ios7
and as sizeWithFont
method is deprecated in iOS 7.0
这篇关于带有包装标签的静态表格单元格的动态高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文