让我们看看我有一个这样的字符串:

NSString *longStr = @"AAAAA\nBBBBB\nCCCCC";  

如何使UILabel像这样显示消息

五星级 BBBBB CCCCC

我不认为\n能被UILabel识别,我能在NSString中放入什么东西让UILabel知道它必须在那里创建换行符吗?


当前回答

textLabel.text = @"\nAAAAA\nBBBBB\nCCCCC";
textLabel.numberOfLines = 3; \\As you want - AAAAA\nBBBBB\nCCCCC
textLabel.lineBreakMode = UILineBreakModeWordWrap;
NSLog(@"The textLabel text is - %@",textLabel.text);

其他回答

在我的情况下,也\n是不工作的,我通过保持行数为0,复制和粘贴文本与新行本身例如,而不是Hello \n世界我粘贴

你好 世界

在接口构建器中。

//不要忘记将numberolines设置为0

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];

重要的是要注意它是\n(反斜杠)而不是/n。

NSCharacterSet *charSet = NSCharacterSet.newlineCharacterSet;
NSString *formatted = [[unformatted componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@"\n"];

在Xcode 6上,我可以在swift编程上使用\n没有问题