【发布时间】:2015-09-11 20:34:46
【问题描述】:
我在我的应用程序中动态创建 UILabel 以显示食谱的方向列表。标签正确填充,一个接一个地显示所有项目。
问题是当文本在标签的下一行出现时,它与下一个标签重叠。
我已将 numberOfLines 设置为 0,并将 lineBreakMode 设置为 NSLineBreakByWordWrapping。这有助于标签在多行上显示文本。
我已尝试调整标签的高度,正如您将在代码中看到的那样,但它不起作用。
如何防止标签多行导致标签重叠?
这是用多行填充标签的代码:
//add all the directions to the uiview
for (int i = 0; i < self.recipe.directions.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,(i+1)*25,280,25)];
label.lineBreakMode = NSLineBreakByWordWrapping; //multiple lines in a label
label.numberOfLines = 0;
label.text =[NSString stringWithFormat: @"%@", self.recipe.directions[i]];
[label sizeToFit]; // resize the width and height to fit the text
NSLog(@"Actual height is: %f", label.frame.size.height); // Use this for spacing any further elements
CGSize expectedLabelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:NSLineBreakByWordWrapping];
//adjust the label the the new height.
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
[self.directionsView addSubview:label];
}
【问题讨论】:
标签: ios objective-c uilabel