UILabel 中的多行文本 (Multiple lines of text in UILabel)


問題描述

UILabel 中的多行文本 (Multiple lines of text in UILabel)

Is there a way to have multiple lines of text in UILabel like in the UITextView or should I use the second one instead?


參考解法

方法 1:

Set the line break mode to word-wrapping and the number of lines to 0:

// Swift
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0

// Objective-C
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

// C# (Xamarin.iOS)
textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;  

Restored old answer (for reference and devs willing to support iOS below 6.0):

textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;

On the side: both enum values yield to 0 anyway.

方法 2:

In IB, set number of lines to 0 (allows unlimited lines)

When typing within the text field using IB, use "alt-return" to insert a return and go to the next line (or you can copy in text already separated out by lines).

方法 3:

The best solution I have found (to an otherwise frustrating problem that should have been solved in the framework) is similar to vaychick's.

Just set number of lines to 0 in either IB or code

myLabel.numberOfLines = 0;

This will display the lines needed but will reposition the label so its centered horizontally (so that a 1 line and 3 line label are aligned in their horizontal position). To fix that add:

CGRect currentFrame = myLabel.frame;
CGSize max = CGSizeMake(myLabel.frame.size.width, 500);
CGSize expected = [myString sizeWithFont:myLabel.font constrainedToSize:max lineBreakMode:myLabel.lineBreakMode]; 
currentFrame.size.height = expected.height;
myLabel.frame = currentFrame;

方法 4:

Use this to have multiple lines of text in UILabel:

textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

Swift:

textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0

方法 5:

myUILabel.numberOfLines = 0;
myUILabel.text = @"your long string here";
[myUILabel sizeToFit];

(by Ilya SuzdalnitskiIlya SuzdalnitskiKendall Helmstetter GelnerMichael MichailidisAbiGurumoorthy Arumugam)

參考文件

  1. Multiple lines of text in UILabel (CC BY-SA 3.0/4.0)

#uilabel #uitextview #iOS #line-breaks #multiline






相關問題

標籤邊框內的間距 (spacing inside label's border)

awakeFromNib 無法從 UILabel 獲取值,也無法禁用 UIButton (awakeFromNib can't get value from UILabel and can't disable UIButton)

如何在 UILabel 中製作陰影 (How to make shadow in UILabel)

如何計算 UILabel 的字體大小 (How can I calculate font size of UILabel)

如何在情節提要中為uilabel設置大於300的字體大小? (How to set font size greater than 300 for uilabel in storyboard?)

隨著 UILabel 尺寸的增加,在我的 UILabel 周圍獲得額外的空間 (Getting extra space around my UILabel as it increases in size)

UILabel 不顯示所有內容 (UILabel not displaying everything)

在 UIView 中將標籤居中 (Centering a label in a UIView)

UITableViewCell 未在其中顯示 UILabel (UITableViewCell not showing UILabel within)

帶有 UIMenucontroller 的 UILabel 不會通過外部觸摸退出第一響應者 (UILabel with UIMenucontroller not resigning first responder with touch outside)

UILabel 動態高度問題 (UILabel Dynamic height problem)

是否可以將標籤內的文本與“大”框架垂直對齊 (Is it possible to vertically align text inside labels with a "large" frame)







留言討論