iOS版:动态添加多行的UILabel [英] iOS: Dynamically adding a multiline UILabel

查看:429
本文介绍了iOS版:动态添加多行的UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法动态地添加一个多行的UILabel。下面是我得到的结果:

I am having trouble dynamically adding a multiline UILabel. Here is the result I'm getting:

我想在摘要盒上的标签自然换行。这里是code我使用添加标签:

I would like the label in the "Summary" box to wrap naturally. Here is the code I'm using to add the label:

-(void)setSummary{
    summaryContentLabel = [[UILabel alloc] init];
    summaryContentLabel.numberOfLines=0;
    summaryContentLabel.lineBreakMode=NSLineBreakByWordWrapping;
    summaryContentLabel.text=self.summary;
    summaryContentLabel.font=[UIFont fontWithName:@"Helvetica" size:13.0];

    [summaryView addSubview:summaryContentLabel];
    [summaryContentLabel  setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSDictionary *views = NSDictionaryOfVariableBindings(summaryView,summaryContentLabel ,summaryLabel);
    [summaryView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[summaryLabel]-5-[summaryContentLabel(==100)]-5-|" options:nil metrics:nil views:views]];

}

您会发现这本书的标题包装了我想没有问题的方式。那是在IB完成。不幸的是,我必须添加在code此标签。自动布局也是有要求的。

You'll notice the title of the book wraps the way I want with no problem. That was done in IB. Unfortunately, I have to add this label in code. Auto Layout is also a requirement.

推荐答案

想通了。我需要添加额外的限制条件为标签的宽度,这样的:

Figured it out. I needed to add an additional constraint for the width of the label, like this:

[summaryView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[summaryContentLabel]-|" options:nil metrics:nil views:views]];

这篇关于iOS版:动态添加多行的UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆