问题描述
我在 contentInsets 上遇到了一些奇怪的事情
我的故事板中有一个 UITextView,其 contentInset 为 50,因为我正在尝试向我的 uitextview 添加一些填充
但是,在uitextview的底部出现了一个滚动条,如下图所示:
我的印象是 contentInset 挤压了 uitextview 而不会导致这个水平滚动条,所以我怎样才能消除对水平滚动条的需求并使一切——插图和 uitextview 中的所有文本——在没有需要这个滚动条.
注意:我不是在问防止水平滚动或不显示滚动条(因此剪切文本)
非常感谢!
对于atomk(UITextView被称为ss)
NSLog(@"%f 之前的内容大小",self.ss.contentSize.width);日志:280CGSize 大小=self.ss.contentSize;size.width=size.width-50;[self.ss setContentSize:size];NSLog(@"%f 后的内容大小",self.ss.contentSize.width);日志:230
添加代码的视图与添加代码之前的视图相比没有可见差异,所以出了点问题!(谢谢)
更新:从 iOS 7 开始,此解决方案已过时.
请参阅(参见iPadding"部分)
希望对您有所帮助,如果有人能找到更好的解决方案,我很想听听!
I have encountered something a bit strange with 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
However, a scrollbar appears on the bottom of the uitextview, as shown below in this test:
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.
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)
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 no visible difference between the view with the code added than before it was added, so something's going wrong! (Thanks)
UPDATE: This solution is out of date as of iOS 7.
See this answer below. In iOS 7.0, the textContainerInset
property on UITextView
was introduced.
Objective-C:
textView.textContainerInset = UIEdgeInsetsMake(0, 50, 0, 0);
Swift:
textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
Or as Zeev Vax suggested, in Swift 5.0:
textView.textContainerInset.left = 50
Pre-iOS 7 solution:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar...
I'm afraid this is not how contentInset
works with a UITextView
. See Apple's documentation for contentInset
where it states:
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 added around the content.
You can change the contentSize
in viewDidLayoutSubviews
using the code you have included above:
- (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.
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:
If your UITextView
does have a background, see:
- How to set UITextView's content inset like Notes App
- Stuff you learn from reverse-engineering Notes.app (see "iPadding" section)
Hope that helps, and if anyone can find a better solution I'd love to hear about it!
这篇关于UITextView 内容插图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!