【问题标题】:iOS how calculate height of label?iOS如何计算标签的高度?
【发布时间】:2016-03-11 11:54:10
【问题描述】:

b4a 中,我们可以像这样通过测量文本高度和 StringUtils 库轻松计算标签的高度:

StringUtils.MeasureMultilineTextHeight

但是在B4i 中没有这样的库或选项来执行此操作,那么我如何在标签中(在滚动视图中)加载长文本?

我必须有标签高度(取决于 txt)才能添加其他按钮并查看底部并制作我的布局

【问题讨论】:

  • 什么是 b4a?没有人能理解你试图说的话......
  • @Zhi-WeiCai 可能是他在说这个b4x.com/b4a.html
  • ohhh 我很抱歉 b4a 表示 basic4Android 而 B4i 是 Basic4Ios 我不知道为什么它们没有标签(我发现 b4a 标签为 basic4android)

标签: ios iphone basic4android


【解决方案1】:

试试这个

- (CGFloat)getLabelHeight:(UILabel*)label
{
    CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
    CGSize size;

    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:context].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

    return size.height;
}

这样使用

CGFloat lblHeight = [self getLabelHeight:self.yourLable];

【讨论】:

【解决方案2】:

您可以调用 Label.SizeToFit。您还可以使用 String.MeasureWidth 或 String.MeasureHeight 测量宽度和高度。

虽然显示长文本的最佳解决方案是使用 CustomListView.AddTextItem。

【讨论】:

    【解决方案3】:

    而不是计算 UILabel 高度计算文本本身的高度并相应地调整标签。

    Objectiv-C

    // *** I have created a dynamic label and calculated its height ***
    UILabel *lblDynamicHeight = [[UILabel alloc] init];
    [lblDynamicHeight setText:@"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."];
    [lblDynamicHeight setFrame:CGRectMake(0, 0, 200, [self textHeight:lblDynamicHeight.text])];
    [lblDynamicHeight setFont:[UIFont fontWithName:@"Arial" size:15]];
    [self.view addSubview:lblDynamicHeight];
    
    - (CGFloat)textHeight:(NSString *)text
    {
        CGFloat maxWidth = 200; // set Max width for your control here. (i have used 200)
        CGRect rect = [text boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
                                                              options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                           attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15]}   // Set your label's font here
                                                              context:nil];
        CGFloat textHeight = rect.size.height;
        return textHeight;
    }
    

    斯威夫特

    func addLabel() {
        // *** I have created a dynamic label and calculated its height ***
        var lblDynamicHeight: UILabel = UILabel()
        lblDynamicHeight.text = "Lorem Ipsum."
        lblDynamicHeight.frame = CGRectMake(0, 0, 200, self.textHeight(lblDynamicHeight.text!))
        lblDynamicHeight.font = UIFont(name: "Arial", size: 15)
        self.view!.addSubview(lblDynamicHeight)
    }
    
    func textHeight(text: String) -> CGFloat {
        var maxWidth: CGFloat = 200
        // set Max width for your control here. (i have used 200)
        var rect: CGRect = text.boundingRectWithSize(CGSizeMake(maxWidth, CGFLOAT_MAX), options: ([.UsesLineFragmentOrigin, .UsesFontLeading]), attributes: [NSFontAttributeName: UIFont(name: "Arial", size: 15)], context: nil)
        var textHeight: CGFloat = rect.size.height
        return textHeight
    }
    

    使用此代码。快乐编码:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2011-09-28
      • 2016-01-14
      相关资源
      最近更新 更多