如何在 UITextView 中设置边距(padding)?
我有一个用于文本编辑的 UITextView。默认情况下,文本周围有一个小边距。我想将该边距增加几个像素。
contentInset 属性为我提供了边距,但它不会更改文本“换行宽度”。文本以相同的宽度换行,额外的“边距”只会导致视图水平滚动。
有没有办法让一定宽度的 UITextView 显示具有较窄“换行宽度”的文本?
I have a UITextView for text editing. By default, it has a small margin around the text. I want to increase that margin by a few pixels.
The contentInset property gives me margins, but it does not change the text "wrap width". The text is wrapped at the same width, and the extra "margin" just causes the view to scroll horizontally.
Is there a way to make a UITextView of a certain width display the text with a narrower "wrap width"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从 iOS 7 开始,您可以使用
textContainerInset
属性:Objective-C
Swift
Starting from iOS 7 you can use
textContainerInset
property:Objective-C
Swift
摆弄了一段时间后,如果您只为 iOS 6 进行开发,我找到了另一个解决方案。使用 contentInset 设置顶部和底部边距:
对于左侧和右侧边距,不要添加普通的立即文本,但使用 NSAttributedString 代替,并使用 NSMutableParagraphStyle 正确设置左右缩进:
这为您提供了一个带有文本的 UITextView (在我的例子中来自变量 myText),具有 20 像素的填充,可以正确滚动。
After fiddling around with this for a while I found another solution if you're only developing for iOS 6. Set the top and bottom margins with contentInset:
For the left and right margins don't add your plain text right away but use an NSAttributedString instead with properly set left and right indents with an NSMutableParagraphStyle:
This gives you a UITextView with your text (in my case from the variable myText) with 20 pixels padding that properly scrolls.
试试这个
Try this
您可以使用较小的 UITextView,并将 UIView 放置在背景中来模拟填充。
You could just use a smaller UITextView, and place a UIView in the background to simulate the padding.
如果你想从一侧设置填充,你可以使用下面的代码:
if you want to set padding from one side, you can use below code:
这对我有用。更改 textView contentInset 和框架插入/位置以获得正确的填充和自动换行。然后更改 textView 边界以防止水平滚动。每次选择和更改文本时,您还需要重置填充。
This is working for me. Changing the textView contentInset and frame inset/position to get the correct padding and word wrap. Then changing the textView bounds to prevent horizontal scrolling. You also need to reset the padding each time the text is selected and changed.
感谢您的建议。
我在开发 macOS / OSX 应用程序时遇到了这个问题,最终得到以下结果:
Thanks for the suggestions.
I had this problem when developing a macOS / OSX app and ended up with this:
尝试下面的代码
对于 iOS7 使用 textContainerInset
textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 20);
检查下面的链接,它可能对您有用
https://stackoverflow.com/a/22935404/5184217
Try below code
For iOS7 use textContainerInset
textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 20);
Check below link it might be useful to you
https://stackoverflow.com/a/22935404/5184217
Swift + iOS 12:另一种方法
将填充设置为
UITextView
的左侧 + 右侧确实让文本在输入超过 9 时消失文本行。使用@rajesh的解决方案有助于解决这个问题:确保每当文本发生变化+设备旋转时调用该方法。
Swift + iOS 12: A different approach
Setting padding to the left + right side of our
UITextView
did let the text disappear when entered more that 9 lines of text. Using the solution of @rajesh helped to solve this issue:Make sure to invoke the method whenever text did change + the device rotates.