行了三个UILabels彼此相邻,每个标记可以是多行 [英] Line up three UILabels next to each other and each label can be multiline

查看:134
本文介绍了行了三个UILabels彼此相邻,每个标记可以是多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我'工作的一个项目,我需要排队 3 UILabels 彼此相邻。让我举个例子来说吧。

I'am working on a project where I need to line up 3 UILabels next to each other. Let me explain it with an example.

我有例如下面的句子。 蒂姆·摩尔谈到了小熊的故事
现在我的三个 UILabels 要包含以下内容:

I have for example the following sentence. "Tim Moore commented on the little bear story" Now my three UILabels going to contain:

LABEL 1 --> Tim Moore
LABEL 2 --> commented on
LABEL 3 --> the little bear story

另外一个例子:

Dave Smits likes the status "We and only we can me a knew future and only we nobody else can make changes, so don't stop now !"

LABEL 1 --> Dave Smits
LABEL 2 --> likes the status
LABEL 3 --> "We and only we can me a knew future and only we nobody else can make changes, so don't stop now !"

就像你可以在第二个例子中看到的一句话是更长的时间。

Like you can see in the second example the sentence is much longer.

我的问题是,现在我该怎么行这三个 UILabels 后对方排队,让我得到一个不错的句子。为什么我在三个是分开的原因 UILabels 是因为我需要设置 UITapgestures 每个的UILabel

My question is now how can I line these three UILabels line up after each other so that I get a nice sentence. The reason why I separate it in three UILabels is because I need to set UITapgestures on each UILabel.

我是一个自定义的的UITableViewCell 这是使用自动布局

I'm working inside a custom UITableviewCell which is using autolayout.

任何帮助将是AP preciated!

Any help would be appreciated !

推荐答案

您可以使用那块code,以获得您的标签一字排开。设置numberOfLines:0让他们多行

You can use that piece of code to get your labels lined up. Set numberOfLines:0 to let them be multi-line.

-(void)adjustLabels
{
    [self.label1 setText:@"Dave Smits"];
    [self.label2 setText:@"likes the status"];
    [self.label3 setText:@"We and only we can me a knew future and only we nobody else can make changes, so don't stop now !"];

    [self.label1 setNumberOfLines:0];
    [self.label1 sizeToFit];
    CGRect frameFor2 = self.label2.frame;
    frameFor2.origin.x = self.label1.frame.origin.x + 3.f; //add some space between labels
    [self.label2 setFrame:frameFor2];

    [self.label2 setNumberOfLines:0];
    [self.label2 sizeToFit];
    CGRect frameFor3 = self.label3.frame;
    frameFor3.origin.x = self.label2.frame.origin.x + 3.f;
    [self.label3 setFrame:frameFor3];

    [self.label3 setNumberOfLines:0];
    [self.label3 sizeToFit];
}

sizeToFit 方法不具有自动排版工作,你可以看看其他的从SO 的答案来解决它。

sizeToFit method does not work with auto layout, you can have a look at another answer from SO to fix it.

这篇关于行了三个UILabels彼此相邻,每个标记可以是多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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