Objective C - IB - 垂直居中3个多线UILabel

时间:2014-10-29 19:43:24

标签: ios xcode storyboard interface-builder

我有3个UILabel叠在一起,如下所示:

顶部标签可以是2行,但不是所有时间。我试图在图片的顶部和底部线之间垂直居中所有3个标签。我只是不确定如何去做。我已经尝试将它们之间的空间设置为相等,并在顶部和底部给出≥边距,但它似乎不起作用。

尝试在没有代码的情况下执行此操作,如果可能,请在界面构建器中完成。

2 个答案:

答案 0 :(得分:2)

您可以尝试将三个标签放在UIView内,然后指定UIView以对齐UIImageView的中心。

您可以通过选择所有三个UILabel并转到Editor->Embed In->UIView轻松完成此操作。在图像上选择新视图并 Shift单击,然后选择Editor->Align->Vertical Centers

答案 1 :(得分:0)

忘了为此使用不同的标签,这是噩梦。将NSAttributedStringUILabel一起使用,它会自动将文字居中。

NSString *name = @"John\nMultiline\nSmith"; // your multiline string
NSString *title = @"Salesguy"; // some more strings
NSString *phone = @"555-55-55";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@\n%@", name, title, phone] attributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:17.0]}]; // construct the label with default parameters
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:17.0] range:[attributedString.string rangeOfString:name]]; // add bold selection to the name part
myLabel.attributedText = attributedString;
相关问题