ios UILabel 中的多行文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/990221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple lines of text in UILabel
提问by Ilya Suzdalnitski
Is there a way to have multiple lines of text in UILabel
like in the UITextView
or should I use the second one instead?
有没有办法让多行文本中UILabel
像UITextView
或者我应该使用第二个呢?
回答by Ilya Suzdalnitski
I found a solution.
我找到了解决方案。
One just has to add the following code:
只需添加以下代码:
// Swift
textLabel.lineBreakMode = .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
textLabel.numberOfLines = 0
// For Swift >= 3
textLabel.lineBreakMode = .byWordWrapping // notice the 'b' instead of 'B'
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):
恢复旧答案(供参考和愿意支持 iOS 6.0 以下的开发者):
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
On the side: both enum values yield to 0
anyway.
另一方面:0
无论如何,两个枚举值都会屈服。
回答by Kendall Helmstetter Gelner
In IB, set number of lines to 0 (allows unlimited lines)
在 IB 中,将行数设置为 0(允许无限行)
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).
使用 IB 在文本字段中键入时,使用“alt-return”插入回车并转到下一行(或者您可以复制已由行分隔的文本)。
回答by Michael Michailidis
The best solution I have found (to an otherwise frustrating problem that should have been solved in the framework) is similar to vaychick's.
我发现的最佳解决方案(针对一个本应在框架中解决的令人沮丧的问题)类似于 vaychick 的。
Just set number of lines to 0 in either IB or code
只需在 IB 或代码中将行数设置为 0
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:
这将显示所需的行,但会重新定位标签,使其水平居中(以便 1 行和 3 行标签在其水平位置对齐)。要解决该问题,请添加:
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;
回答by Abi
Use this to have multiple lines of text in UILabel
:
使用它在 中包含多行文本UILabel
:
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
Swift:
迅速:
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0
回答by Gurumoorthy Arumugam
myUILabel.numberOfLines = 0;
myUILabel.text = @"your long string here";
[myUILabel sizeToFit];
回答by Tim Maher-De Troyer
If you have to use the:
如果您必须使用:
myLabel.numberOfLines = 0;
property you can also use a standard line break ("\n")
, in code, to force a new line.
属性,您还可以("\n")
在代码中使用标准换行符来强制换行。
回答by prajul
You can use \r
to go to next line while filling up the UILabel
using NSString
.
您可以\r
在填写UILabel
using时使用转到下一行NSString
。
UILabel * label;
label.text = [NSString stringWithFormat:@"%@ \r %@",@"first line",@"seconcd line"];
回答by user40910
lets try this
让我们试试这个
textLabel.lineBreakMode = NSLineBreakModeWordWrap; // UILineBreakModeWordWrap deprecated
textLabel.numberOfLines = 0;
回答by bartolo-otrit
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
The solution above does't work in my case. I'm doing like this:
上面的解决方案在我的情况下不起作用。我这样做:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
// ...
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Georgia-Bold" size:18.0];
}
// ...
UILabel *textLabel = [cell textLabel];
CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0]
constrainedToSize:CGSizeMake(240.0, 480.0)
lineBreakMode:UILineBreakModeWordWrap];
cell.textLabel.frame = CGRectMake(0, 0, size.width + 20, size.height + 20);
//...
}
回答by Vijay Sharma
Use story borad : select the label to set number of lines to zero......Or Refer this
使用 story borad :选择标签将行数设置为零......或者参考这个