ios 如何为 UILabel 添加换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2312899/
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
How to add line break for UILabel?
提问by Thang Pham
Let see that I have a string look like this:
让我们看看我有一个像这样的字符串:
NSString *longStr = @"AAAAA\nBBBBB\nCCCCC";
How do I make it so that the UILabel display the message like this
如何让 UILabel 显示这样的消息
AAAAA
BBBBB
CCCCC
AAAAA
BBBBB
CCCCC
I don't think \n
is recognized by UILabel, so is there anything that I can put inside NSString so that UILabel knows that it has to create a line break there?
我认为\n
UILabel无法识别,所以我可以在 NSString 中放入任何内容,以便 UILabel 知道它必须在那里创建换行符?
回答by Gerry Shaw
Use \n
as you are using in your string.
使用\n
如您使用的是您的字符串。
Set numberOfLines to 0 to allow for any number of lines.
将 numberOfLines 设置为 0 以允许任意数量的行。
label.numberOfLines = 0;
label.numberOfLines = 0;
Update the label frame to match the size of the text using sizeWithFont:
. If you don't do this your text will be vertically centered or cut off.
使用 更新标签框架以匹配文本的大小sizeWithFont:
。如果您不这样做,您的文本将垂直居中或被切断。
UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.frame.origin.x, label.frame.origin.y,
label.frame.size.width, labelSize.height);
Update : Replacement for deprecated
更新:替换已弃用
sizeWithFont:constrainedToSize:lineBreakMode:
sizeWithFont:constrainedToSize:lineBreakMode:
Reference, Replacement for deprecated sizeWithFont: in iOS 7?
参考,替换已弃用的 sizeWithFont:在 iOS 7 中?
CGSize labelSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
label.frame = CGRectMake(
label.frame.origin.x, label.frame.origin.y,
label.frame.size.width, labelSize.height);
回答by Marco
Use option-return when typing in the little box in Interface Builder to insert a line feed (\n). In Interface Builder's Label attributes, set # Lines = 0.
在 Interface Builder 的小框中键入以插入换行符 (\n) 时使用 option-return。在 Interface Builder 的 Label 属性中,设置 # Lines = 0。
Select the label and then change Lines property to 0 like in the above image, and then use \n in your string for line break.
选择标签,然后像上图一样将 Lines 属性更改为 0,然后在字符串中使用 \n 进行换行。
回答by Al-Noor Ladhani
If you read a string from an XML file, the line break \n
in this string will not work in UILabel
text. The \n
is not parsed to a line break.
如果您从 XML 文件中读取字符串,则\n
该字符串中的换行符在UILabel
文本中不起作用。该\n
不会被解析到一个换行符。
Here is a little trick to solve this issue:
这里有一个小技巧可以解决这个问题:
// correct next line \n in string from XML file
NSString *myNewLineStr = @"\n";
myLabelText = [myLabelText stringByReplacingOccurrencesOfString:@"\n" withString:myNewLineStr];
myLabel.text = myLabelText;
So you have to replace the unparsed \n
part in your string by a parsed \n
in a hardcoded NSString
.
因此,您必须将\n
字符串中未解析的部分替换\n
为硬编码NSString
.
Here are my other label settings:
这是我的其他标签设置:
myLabel.numberOfLines = 0;
myLabel.backgroundColor = [UIColor lightGrayColor];
myLabel.textColor = [UIColor redColor];
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];
myLabel.textAlignment = UITextAlignmentCenter;
Most important is to set numberOfLines
to 0
(= unlimited number of lines in label).
最重要的是设置numberOfLines
为0
(= 标签中的行数不受限制)。
No idea why Apple has chosen to not parse \n
in strings read from XML?
不知道为什么 Apple 选择不解析\n
从 XML 读取的字符串?
Hope this helps.
希望这可以帮助。
回答by kuokuo321
In the interface builder, you can use Ctrl+ Enterto insert /n
to the position you want.
This way could implement the following situation
在界面构建器中,您可以使用Ctrl+Enter插入/n
到您想要的位置。这种方式可以实现以下情况
aaa
aaaaaaa
AAA
AAAAAAA
回答by Dan
You have to set the numberOfLines
property on the UILabel
. The default is 1, if you set it to 0it will remove all limits.
您必须numberOfLines
在UILabel
. 默认值为1,如果将其设置为0将删除所有限制。
回答by AndyOng
Important to note it's \n
(backslash) rather than /n
.
重要的是要注意它是\n
(反斜杠)而不是/n
.
回答by CodeOverRide
In Swift 2.2, > iOS 8
在 Swift 2.2 中,> iOS 8
I've set Lines = 0 on Storyboard, under Attribute Inspector and linked a referencing outlet to the label. Then use in controller like this:
我在 Storyboard 上的 Attribute Inspector 下设置了 Lines = 0,并将引用出口链接到标签。然后像这样在控制器中使用:
@IBOutlet weak var listLabel: UILabel!
override func viewDidLoad() {
...
listLabel.text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8"
}
回答by Sandip Patel - SM
Just do it like this
就这样做
NSString * strCheck = @"A\nB";
strCheck = [strCheck stringByReplacingOccurrencesOfString:@"\n" withString:@"\n"]; //This is to prevent for fetching string from plist or data structure
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = strCheck;
回答by Pankaj Chauhan
// DO not forget to set numberOfLines to zero
// 不要忘记将 numberOfLines 设置为零
UILabel* locationTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 30, 230, 40)];
locationTitle.font = [UIFont systemFontOfSize:13.0];
locationTitle.numberOfLines = 0;
locationTitle.text = [NSString stringWithFormat:@"Eaton industries pvt. Ltd \nUK Apr 12"];
[cell addSubview:locationTitle];
回答by Roi
On Xcode 6, you can just use \n even inside a string when using word wrap. It will work. So for example:
在 Xcode 6 上,使用自动换行时,您甚至可以在字符串内使用 \n。它会起作用。例如:
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, screenRect.size.width, 50)];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"This will be on the first line\nfollowed by a line under it.";
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;