【问题标题】:How to use multipleline in uilabel and automatically extend tableview row height depend upon label height in Swift?如何在 uilabel 中使用多行并根据 Swift 中的标签高度自动扩展 uitableview 行高?
【发布时间】:2016-05-19 05:52:48
【问题描述】:

我正在使用 swift 语言开发基于表格的 iOS 应用程序。我已将 UILabel 集成到 UITableview 单元格中。我想根据 UILabel 高度更改单元格高度。

【问题讨论】:

  • 你的 usinf Storyboard right

标签: ios swift uitableview uilabel


【解决方案1】:

您可以将numberoflines 属性设置为0 以获取多行标签。比如,

 yourLabel.numberOfLines = 0;

您可以根据字符串更改标签高度,例如,

 //Calculate the expected size based on the font and linebreak mode of your label
 // FLT_MAX here simply means no constraint in height
 CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

 //adjust the label the the new height.
 CGRect newFrame = yourLabel.frame;
 newFrame.size.height = expectedLabelSize.height;
 yourLabel.frame = newFrame;

您可以通过实现委托方法来增加tableview行的高度,

heightForRowAtIndexPath

希望这会有所帮助:)

【讨论】:

  • 如何根据 swift 2.0 中的标签大小自动扩展 tableview,但不是objective-c。请尽快发送给我...
【解决方案2】:

首先在 Storyboard 中将 UILabel 行设置为 0。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{

// need to calculte height of label text
 NSString *comment = [[self.objects objectAtIndex:[indexPath row]] objectForKey:@"Testo"];
    CGFloat whidt =  260;
    UIFont *FONT = [UIFont systemFontOfSize:15];
    NSAttributedString *attributedText =[[NSAttributedString alloc]  initWithString:comment  attributes:@  { NSFontAttributeName: FONT }];
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;
    return size.height +130;
}

whidt 是标签宽度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多