ios UITextView 内容插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18560412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITextView content inset
提问by H Bellamy
I have encountered something a bit strange with contentInsets
我在 contentInsets 中遇到了一些奇怪的事情
I have a UITextView in my storyboard with a contentInset of 50 left, as I'm trying to add some padding to my uitextview
我的故事板中有一个 UITextView,其 contentInset 为 50,因为我正在尝试向我的 uitextview 添加一些填充
However, a scrollbar appears on the bottom of the uitextview, as shown below in this test:
但是,在uitextview的底部出现了一个滚动条,如下图所示:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar, so how can I remove the need for the horizontal scrollbar and make everything--the inset AND all the text in the uitextview--visible without the need for this scrollbar.
我的印象是 contentInset 在不导致此水平滚动条的情况下挤压了 uitextview,那么我如何消除对水平滚动条的需要并使所有内容——uitextview 中的插图和所有文本——可见而无需此滚动条。
N.B: I'm not asking about preventing the scrolling horizontally or not displaying the scrollbar(thus cutting of the text)
注意:我不是在问防止水平滚动或不显示滚动条(从而剪切文本)
Thanks a lot!
非常感谢!
For atomk(UITextView is called ss)
对于 atomk(UITextView 称为 ss)
NSLog(@"Content Size Before %f",self.ss.contentSize.width); Logs: 280
CGSize size=self.ss.contentSize; size.width=size.width-50;
[self.ss setContentSize:size];
NSLog(@"Content Size After %f",self.ss.contentSize.width); Logs: 230
There is novisible difference between the view with the code added than before it was added, so something's going wrong! (Thanks)
有没有比加入前添加的代码视图之间明显的区别,因此,一些地方出了错!(谢谢)
回答by Florian
In iOS 7 UITextView
is based on TextKit and has a new property textContainerInset
. It behaves just as you would expect:
在 iOS 7 中UITextView
是基于 TextKit 并且有一个新的属性textContainerInset
。它的行为正如您所期望的:
UITextView *textView = ...;
// Left inset of 50 points
textView.textContainerInset = UIEdgeInsetsMake(0.0, 50.0, 0.0, 0.0);
Swift 4.2
斯威夫特 4.2
textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
回答by Steph Sharp
UPDATE: This solution is out of date as of iOS 7.
更新:此解决方案自 iOS 7 起已过时。
See this answer below. In iOS 7.0, the textContainerInset
property on UITextView
was introduced.
请参阅下面的这个答案。在 iOS 7.0 中,引入了textContainerInset
属性 on UITextView
。
Objective-C:
目标-C:
textView.textContainerInset = UIEdgeInsetsMake(0, 50, 0, 0);
Swift:
迅速:
textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
Pre-iOS 7 solution:
iOS 7 之前的解决方案:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar...
我的印象是 contentInset 会挤压 uitextview 而不会导致此水平滚动条...
I'm afraid this is not how contentInset
works with a UITextView
. See Apple's documentationfor contentInset
where it states:
这恐怕不是如何contentInset
与工作UITextView
。请参阅Apple 的文档,了解contentInset
其中的说明:
The distance that the content view is inset from the enclosing scroll view... Use this property to add to the scrolling area around the content.
内容视图与封闭滚动视图之间的距离...使用此属性添加到内容周围的滚动区域。
The contentInset
is addedaround the content.
将contentInset
被添加内容周围。
You can change the contentSize
in viewDidLayoutSubviews
using the code you have included above:
您可以使用上面包含的代码更改contentSize
in viewDidLayoutSubviews
:
- (void)viewDidLayoutSubviews
{
self.textView.contentInset = UIEdgeInsetsMake(0, 50, 0, 0);
NSLog(@"Content Size Before %f",self.textView.contentSize.width); //Logs: 280
CGSize size=self.textView.contentSize;
size.width=size.width-50;
[self.textView setContentSize:size];
NSLog(@"Content Size After %f",self.textView.contentSize.width); //Logs: 230
}
However, this causes the text to be cut off on the right side:
但是,这会导致文本在右侧被截断:
The best way I have been able to achieve the appearance of horizontal padding in a UITextView
is to position it inside a container UIView
. In your case, simply create a UIView
the same size as your current text view, and add a text view that is 50px narrower inside the container view.
我能够在 a 中实现水平填充外观的最佳方法UITextView
是将其放置在容器内UIView
。在您的情况下,只需创建UIView
与当前文本视图相同大小的文本视图,并在容器视图内添加一个窄 50 像素的文本视图。
This workaround can cause problems if you have a background for your text view, but from your screenshot above it doesn't look like that's an issue for you.
如果您的文本视图有背景,则此解决方法可能会导致问题,但从上面的屏幕截图来看,这对您来说似乎不是问题。
UITextView
(frame in red) inside UIView
container:
UITextView
(红色框)在UIView
容器内:
If your UITextView
does have a background, see:
如果您UITextView
确实有背景,请参阅:
- How to set UITextView's content inset like Notes App
- Stuff you learn from reverse-engineering Notes.app(see "iPadding" section)
- 如何像 Notes App 一样设置 UITextView 的内容插入
- 你从逆向工程 Notes.app 中学到的东西(参见“iPadding”部分)
Hope that helps, and if anyone can find a better solution I'd love to hear about it!
希望有帮助,如果有人能找到更好的解决方案,我很想听听!