NSAttributedString'\ n'被忽略 [英] NSAttributedString '\n' ignored
问题描述
我有一个视图,其唯一的UI元素是 UITextView
。在 viewDidLoad:
我创建一个带有Text \ n的属性字符串,并将文本视图的属性文本设置为:
I have a single view whose only UI element is a UITextView
. In viewDidLoad:
I create an attributed string with "Text\n" and set the text view's attributed text as such:
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Text\n"];
[self.textView setAttributedText:string];
我的问题是在运行应用程序时会忽略换行符。
My problem is that the line break is ignored when I run the app.
如果我使用 NSString
和 setText:
那个不会发生。
If I use an NSString
and setText:
that doesn't happen.
NSString *string = [[NSString alloc] initWithString:@"Text\n"];
[self.textView setText:string];
任何人都可以了解正在发生的事情吗?我似乎无法在文档或其他方面找到任何内容。
Can anyone shed some light on what is happening? I can't seem to find anything in documentation or otherwise.
推荐答案
你可以使用这个:
NSMutableAttributedString *cr = [[NSMutableAttributedString alloc] initWithString: @"\n"];
因此,如果你有两个NSMutableAttributedString(string1和string2),你可以这样做:
So if you have two NSMutableAttributedString (string1 and string2) you can do:
[string1 appendAttributedString: cr];
[string1 appendAttributedString: string2];
这太可怕而且不优雅....但它确实有效!!!
It's horrible and not elegant.... but it works!!!
这篇关于NSAttributedString'\ n'被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!