排列三个UILabels彼此相邻,每个标签可以是多行

我正在做一个项目,我需要将3 UILabels排列在一起。 让我用一个例子来解释一下。

我有例如下面的句子。 "Tim Moore commented on the little bear story"现在我的三个UILabels将包含:

 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 !" 

就像你在第二个例子中看到的那样,句子要长得多。

我现在的问题是如何将这三个UILabels排列在一起,以便我得到一个很好的句子。 我将它分成三个UILabel的原因是因为我需要在每个UILabel上设置UITapgestures

我正在使用autolayout的自定义UITableviewCell内工作。

任何帮助,将不胜感激 !

对您的单元格进行自定义布局,并将自定义单元格内的约束与UILabelsalignment。 既然你在一个自定义的布局,约束应该做的伎俩。

你可以使用这段代码来让你的标签排列起来。 设置numberOfLines:0让它们成为多行。

 -(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的另一个答案来解决它。