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五星级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知道它必须在那里创建一个换行符?
19 个解决方案
#1
290
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;
标签。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:
Reference, Replacement for deprecated sizeWithFont: in iOS 7?
引用,替换过时的sizeWithFont:在ios7中?
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);
#2
222
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.
在接口构建器中的小框中输入一个行提要(\n)时,使用选项返回。在Interface Builder的标签属性中,设置# 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.
选择标签,然后将行属性改为0,就像上面的图像一样,然后在字符串中使用\n来换行。
#3
126
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文件中读取一个字符串,那么在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部分替换为在硬编码的NSString中解析的\n。
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?
不知道为什么苹果选择不解析从XML读取的字符串?
Hope this helps.
希望这个有帮助。
#4
72
In the interface builder, you can use Ctrl + Enter to insert /n
to the position you want. This way could implement the following situation
在接口构建器中,您可以使用Ctrl + Enter来将/n插入到您想要的位置。这种方法可以实现以下情况。
aaa
aaaaaaa
aaa aaaaaaa
#5
24
You have to set the numberOfLines
property on the UILabel
. The default is 1, if you set it to 0 it will remove all limits.
你必须在UILabel上设置numberOfLines属性。默认值是1,如果你把它设为0,它会移除所有的限制。
#6
17
Important to note it's \n
(backslash) rather than /n
.
需要注意的是,它是\n(反斜杠)而不是/n。
#7
7
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:
我在故事板上设置了Lines = 0,在属性检查器下,并将引用的outlet链接到标签。然后在这样的控制器中使用:
@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"
}
#8
6
// 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];
#9
3
If your using a UILabel
you have to remember that the default setting is 1 line, so it does not matter how many breaks you add (\n
or \r
), you need to make sure it is set to more than one line so it could be allowed to append more lines.
如果你使用UILabel你必须记住,默认设置为1,所以不管有多少优惠您添加(\ n或\ r),您需要确保它将超过一行可以被允许添加更多的线。
One alternative is to use UITextView
which is really meant for multilines.
另一种选择是使用UITextView,它实际上是用于多行的。
You can easily achieve this in XCode attribute section of the UILabel, see screenshot:
您可以在UILabel的XCode属性部分轻松实现这一点,请参见屏幕截图:
#10
3
On Xcode 6, you can just use \n even inside a string when using word wrap. It will work. So for example:
在Xcode 6中,当使用单词wrap时,可以在字符串中使用\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;
#11
3
Just use 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;
#12
2
Just using label.numberOfLines = 0;
只使用标签。numberOfLines = 0;
#13
1
textLabel.text = @"\nAAAAA\nBBBBB\nCCCCC";
textLabel.numberOfLines = 3; \\As you want - AAAAA\nBBBBB\nCCCCC
textLabel.lineBreakMode = UILineBreakModeWordWrap;
NSLog(@"The textLabel text is - %@",textLabel.text);
#14
0
For anyone else that might have trouble with sizeWithFont:constrainedToSize:lineBreakMode:
or anyone switching to ios8 (the method is deprecated as of ios7), I adjusted my height by using sizeToFit
instead.
对于任何其他可能遇到sizeWithFont的问题:限制大小:lineBreakMode:或者任何人切换到ios8(该方法不赞成ios7),我使用sizeToFit来调整我的高度。
UILabel *label;
label.numberOfLines = 0;
// Setup label with desired settings
...
[label sizeToFit];
label.frame = CGRectMake(label.frame.origin.x, // Or use any desired origin
label.frame.origin.y,
label.frame.size.width, // Or use any desired width
label.frame.size.height);
#15
0
NSCharacterSet *charSet = NSCharacterSet.newlineCharacterSet;
NSString *formatted = [[unformatted componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@"\n"];
#16
0
It seems wrong to me to change the label frame sizes especially when using autolayout. Using the appendFormat method seems more appropriate. Here is my example:
我觉得改变标签框的尺寸是不对的,尤其是在使用autolayout的时候。使用appendFormat方法似乎更合适。这是我的例子:
NSMutableString *list = [[NSMutableString alloc] init];
NSArray *textArray = @[@"AAAA", @"BBBB"];
for (NSString *string in textArray) {
[list appendFormat:@"%@\n", string.mutableCopy];
}
self.label.text = list;
self.label.numberOfLines = 0;
#17
0
If you set your UILable properties from Plain to Attributed...the UILabel will hold multiline text no matter how many paragraphs for along as your UILabel height and width are set to fit the screen area you want to display the text in.
如果你将你的可UILable属性从普通属性设置为属性…UILabel将保留多行文本,不管您的UILabel高度和宽度设置为适合屏幕区域,您希望显示文本。
#18
0
In my case also \n was not working, I fixed issue by keeping number of lines to 0 and copied and pasted the text with new line itself for example instead of Hello \n World i pasted
在我的例子中,也没有工作,我通过将行数保持为0,并以新的行本身来复制和粘贴文本,而不是我粘贴的Hello \n世界。
Hello
你好
World
世界
in the interface builder.
在界面构建器。
#19
0
I have faced same problem, and here is, how i solved the problem. Hope this will be helpful for someone.
我也遇到过同样的问题,这就是我如何解决这个问题的。希望这对某人有帮助。
// Swift 2
/ /斯威夫特2
lblMultiline.lineBreakMode = .ByWordWrapping // or use NSLineBreakMode.ByWordWrapping
lblMultiline.numberOfLines = 0
// Objective-C
/ / objective - c
lblMultiline.lineBreakMode = NSLineBreakByWordWrapping;
lblMultiline.numberOfLines = 0;
// C# (Xamarin.iOS)
/ / c#(Xamarin.iOS)
lblMultiline.LineBreakMode = UILineBreakMode.WordWrap;
lblMultiline.Lines = 0;
#1
290
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;
标签。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:
Reference, Replacement for deprecated sizeWithFont: in iOS 7?
引用,替换过时的sizeWithFont:在ios7中?
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);
#2
222
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.
在接口构建器中的小框中输入一个行提要(\n)时,使用选项返回。在Interface Builder的标签属性中,设置# 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.
选择标签,然后将行属性改为0,就像上面的图像一样,然后在字符串中使用\n来换行。
#3
126
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文件中读取一个字符串,那么在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部分替换为在硬编码的NSString中解析的\n。
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?
不知道为什么苹果选择不解析从XML读取的字符串?
Hope this helps.
希望这个有帮助。
#4
72
In the interface builder, you can use Ctrl + Enter to insert /n
to the position you want. This way could implement the following situation
在接口构建器中,您可以使用Ctrl + Enter来将/n插入到您想要的位置。这种方法可以实现以下情况。
aaa
aaaaaaa
aaa aaaaaaa
#5
24
You have to set the numberOfLines
property on the UILabel
. The default is 1, if you set it to 0 it will remove all limits.
你必须在UILabel上设置numberOfLines属性。默认值是1,如果你把它设为0,它会移除所有的限制。
#6
17
Important to note it's \n
(backslash) rather than /n
.
需要注意的是,它是\n(反斜杠)而不是/n。
#7
7
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:
我在故事板上设置了Lines = 0,在属性检查器下,并将引用的outlet链接到标签。然后在这样的控制器中使用:
@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"
}
#8
6
// 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];
#9
3
If your using a UILabel
you have to remember that the default setting is 1 line, so it does not matter how many breaks you add (\n
or \r
), you need to make sure it is set to more than one line so it could be allowed to append more lines.
如果你使用UILabel你必须记住,默认设置为1,所以不管有多少优惠您添加(\ n或\ r),您需要确保它将超过一行可以被允许添加更多的线。
One alternative is to use UITextView
which is really meant for multilines.
另一种选择是使用UITextView,它实际上是用于多行的。
You can easily achieve this in XCode attribute section of the UILabel, see screenshot:
您可以在UILabel的XCode属性部分轻松实现这一点,请参见屏幕截图:
#10
3
On Xcode 6, you can just use \n even inside a string when using word wrap. It will work. So for example:
在Xcode 6中,当使用单词wrap时,可以在字符串中使用\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;
#11
3
Just use 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;
#12
2
Just using label.numberOfLines = 0;
只使用标签。numberOfLines = 0;
#13
1
textLabel.text = @"\nAAAAA\nBBBBB\nCCCCC";
textLabel.numberOfLines = 3; \\As you want - AAAAA\nBBBBB\nCCCCC
textLabel.lineBreakMode = UILineBreakModeWordWrap;
NSLog(@"The textLabel text is - %@",textLabel.text);
#14
0
For anyone else that might have trouble with sizeWithFont:constrainedToSize:lineBreakMode:
or anyone switching to ios8 (the method is deprecated as of ios7), I adjusted my height by using sizeToFit
instead.
对于任何其他可能遇到sizeWithFont的问题:限制大小:lineBreakMode:或者任何人切换到ios8(该方法不赞成ios7),我使用sizeToFit来调整我的高度。
UILabel *label;
label.numberOfLines = 0;
// Setup label with desired settings
...
[label sizeToFit];
label.frame = CGRectMake(label.frame.origin.x, // Or use any desired origin
label.frame.origin.y,
label.frame.size.width, // Or use any desired width
label.frame.size.height);
#15
0
NSCharacterSet *charSet = NSCharacterSet.newlineCharacterSet;
NSString *formatted = [[unformatted componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@"\n"];
#16
0
It seems wrong to me to change the label frame sizes especially when using autolayout. Using the appendFormat method seems more appropriate. Here is my example:
我觉得改变标签框的尺寸是不对的,尤其是在使用autolayout的时候。使用appendFormat方法似乎更合适。这是我的例子:
NSMutableString *list = [[NSMutableString alloc] init];
NSArray *textArray = @[@"AAAA", @"BBBB"];
for (NSString *string in textArray) {
[list appendFormat:@"%@\n", string.mutableCopy];
}
self.label.text = list;
self.label.numberOfLines = 0;
#17
0
If you set your UILable properties from Plain to Attributed...the UILabel will hold multiline text no matter how many paragraphs for along as your UILabel height and width are set to fit the screen area you want to display the text in.
如果你将你的可UILable属性从普通属性设置为属性…UILabel将保留多行文本,不管您的UILabel高度和宽度设置为适合屏幕区域,您希望显示文本。
#18
0
In my case also \n was not working, I fixed issue by keeping number of lines to 0 and copied and pasted the text with new line itself for example instead of Hello \n World i pasted
在我的例子中,也没有工作,我通过将行数保持为0,并以新的行本身来复制和粘贴文本,而不是我粘贴的Hello \n世界。
Hello
你好
World
世界
in the interface builder.
在界面构建器。
#19
0
I have faced same problem, and here is, how i solved the problem. Hope this will be helpful for someone.
我也遇到过同样的问题,这就是我如何解决这个问题的。希望这对某人有帮助。
// Swift 2
/ /斯威夫特2
lblMultiline.lineBreakMode = .ByWordWrapping // or use NSLineBreakMode.ByWordWrapping
lblMultiline.numberOfLines = 0
// Objective-C
/ / objective - c
lblMultiline.lineBreakMode = NSLineBreakByWordWrapping;
lblMultiline.numberOfLines = 0;
// C# (Xamarin.iOS)
/ / c#(Xamarin.iOS)
lblMultiline.LineBreakMode = UILineBreakMode.WordWrap;
lblMultiline.Lines = 0;