使用检索到的 NSString 的换行符
我从属性列表中检索 NSString
并将其显示在 UILabel
中。 NSString
已经包含 \n
,但是 UILabel
仅将它们显示为文本。如何告诉 UILabel
实际上使用 \n
作为换行符?
I retrieve an NSString
from a Property list and display it in a UILabel
. The NSString
already includes \n
s, however the UILabel
just displays them as text. How can I tell the UILabel
to actually use the \n
s as line breaks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在 plist 编辑器中输入到 plist 中的所有内容都将被解释为纯文本。尝试一下...将
'
放入字段中,然后右键单击 ->查看为“纯文本”,您会看到它替换了'
。因此,您不能将 \n 放入 plist 中,因为它认为您只是在键入文本并将其视为如此。不要将 \n 放入 plist 中,而是使用 Alt+Enter 来获取换行符。如果您现在将其视为文本文件,您将看到打印的 \ns 以及文本文件中实际显示的新行。现在,当您输出它时,它不会显示 \n 它只会给它一个新行。
另外,正如已经提到的,UITextField 无论如何都只有一行,您可能会从使用 UITextView 中受益。
Everything you type into a plist in the plist editor is interpreted as plain text. Try it... put a
'
into a field and right click -> view as "plain text" and you'll see it substitutes it for'
. Therefore you can't put \n into a plist because it thinks you're just typing text and will treat it as such. Instead of putting \n into your plist use Alt+Enter to get your newline. If you view this as a text file now you'll see \ns printed and new lines acctually shown in the text file.Now when you output it it won't display \n it will just give it a new line.
Plus, as has been mentioned UITextField is only one line anyway and you probably would benefit from using UITextView.
好吧,首先,您需要一个可以修改的字符串。要实现这一点,您可以简单地执行以下操作:
NSMutableString* CorrectedPath = [path mutableCopy];
此时,您可以使用 -insertString:atIndex: 插入您需要的任何字符。
Well, first, you are going to need a string that you can modify. To accomplish that, you can simply do:
NSMutableString* correctedPath = [path mutableCopy];
At that point, you can use -insertString:atIndex: to insert any characters you need.
您在这里使用了错误的类。
UITextField
不(据我所知)支持多行输入。为此,您将需要一个 UITextView(默认情况下启用编辑)。它应该可以毫无问题地解释\n
。如果您想使用它,它还有一个lineBreakMode
属性。You're using the wrong class here.
UITextField
doesn't (for all that I know) support multi-line input. For that, you will need aUITextView
(it has editing enabled by default). It should interpret\n
's without any problems. It also has alineBreakMode
property, if you want to make use of that.