xcode 我怎样才能让 UITextView 只在它充满文本时滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11333212/
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
How can I get the UITextView to scroll only when it's full of text
提问by sebi
I have this UITextView
that works great except, I can't get the text inside the UITextView
to start scrolling only after the UITextView
's size in nearly full, the UITextView is 4 lines tall, but as soon as I reach the 2nd line the 1st line is pushed up, I don't want the view to begin scrolling until I've reached the 5 line. scrollingEnabled = NO
keeps it from scrolling at all, so that didn't work.
我有这个UITextView
效果很好,除了,我无法让里面的文本UITextView
开始滚动,只有在UITextView
's 的大小几乎满后,UITextView 有 4 行高,但是一旦我到达第二行,第一行就是向上推,我不希望视图在到达第 5 行之前开始滚动。scrollingEnabled = NO
阻止它滚动,所以没有用。
UITextView *barf_ = [[UITextView alloc] initWithFrame:CGRectMake(20.0, 310.0, 155, 50)];
barf_.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//[barf_ scrollRangeToVisible:NSMakeRange([barf_.text length], 0)];
barf_.layer.cornerRadius = 3.0f;
barf_.layer.borderWidth = 0.5f;
barf_.font = [UIFont fontWithName:@"Helvetica" size:13];
回答by afrederick
When your UITextView is loaded set scrollEnabled to NO. Then set the text view's delegate to self
or some other object and implement the UITextViewDelegate method
加载 UITextView 时,将 scrollEnabled 设置为 NO。然后将文本视图的委托设置为self
或其他一些对象并实现 UITextViewDelegate 方法
- (void)textViewDidChange:(UITextView *)textView
This method will get called anytime the user makes a change to the text inside the view. Inside this method you need to figure out how big your text is and if it goes beyond the bounds of the text view. If so you enable scrolling. Use this method:
每当用户对视图内的文本进行更改时,都会调用此方法。在这个方法中,你需要弄清楚你的文本有多大以及它是否超出了文本视图的边界。如果是这样,您启用滚动。使用这个方法:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
This is a UIKit category method on NSString. It returns a CGSize that will tell you the height of whatever text string you call it on. In your case it would be something like
这是 NSString 上的 UIKit 类别方法。它返回一个 CGSize ,它会告诉你你调用它的任何文本字符串的高度。在你的情况下,它会是这样的
CGSize textSize = [textView.text sizeWithFont:textView.font
constrainedToSize:CGSizeMake(textView.frame.size.width, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
if (textSize.height > textView.frame.size.height) {
textView.scrollEnabled = YES;
} else {
textView.scrollEnabled = NO;
}
回答by sebi
I found the answer, as others with similar problems have mention, with a small textView, it automatically adds 32 padding to the bottom.
我找到了答案,正如其他有类似问题的人所提到的,使用一个小的 textView,它会自动在底部添加 32 填充。
A simple fix is to add YourTextView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
inside shouldChangeTextInRange
method, that fixed my problem!
一个简单的解决方法是添加YourTextView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
内部shouldChangeTextInRange
方法,解决了我的问题!
回答by Max MacLeod
Setting the contentInset
may help the text to appear more correctly within the UITextView
. However, it won't help solve the issue whereby the UITextView
has scrolling enabled despite not having more text to view.
设置contentInset
可能有助于文本在UITextView
. 但是,它无助于解决UITextView
尽管没有更多文本可查看但启用滚动的问题。
Similarly, methods such as sizeWithFont
have limitations. As explained in Mike Weller's excellent blog series iOS Development: You're Doing It Wrong, NSString
isn't a good object to ask regarding how large a UIView
should be. Many UIView
subclasses such as UILabel
, UIButton
, etc. have insets and other considerations that must be accounted for during sizing. UITextLabel
is no exception.
同样,诸如此类的方法也sizeWithFont
有局限性。正如迈克·韦勒的优秀博客系列解释iOS开发:你就错了,NSString
是不是一个好对象,以询问有关大一个如何UIView
应该是。很多UIView
子类,如UILabel
,UIButton
等有插图和必须上浆过程中考虑的其他因素。UITextLabel
也不例外。
Mike Weller's particular entry on this subject is:
Mike Weller 关于这个主题的特别条目是:
You're Doing It Wrong #2: Sizing labels with -[NSString sizeWithFont:...]
你做错了#2:用 -[NSString sizeWithFont:...] 调整标签大小
iOS 7 promises us more sophisticated text handling in UITextView
, with properties such as textContainerInset
. But what to do in the meantime?
iOS 7 向我们承诺了更复杂的文本处理UITextView
,具有诸如textContainerInset
. 但是在此期间该怎么办?
Well, first we know that UITextView
is a subclass of UIScrollView
. Therefore, the golden rule that if the contentSize
is larger than the view's bounds
property, the scroll view will scroll so we can see more content.
好吧,首先我们知道它UITextView
是 的子类UIScrollView
。因此,黄金法则是,如果contentSize
大于视图的bounds
属性,滚动视图将滚动以便我们可以看到更多内容。
Checking out contentSize
agains the bounds
won't work either because we know that UIScrollView
is already calculating whether it should scroll or not based on the text, and it's giving us the wrong answer.
检查出contentSize
agains的bounds
,因为我们知道这将不会工作UIScrollView
已经计算基于文本是否应该滚动或没有,它给了我们错误的答案。
This is where arbitrary adjustment values come to the rescue! For me this value was 17.f
. For you - depending on your fonts - it maybe different. We then take control and decide whether we should allow the scroll view to scroll:
这是任意调整值派上用场的地方!对我来说,这个值是17.f
. 对你来说——取决于你的字体——它可能会有所不同。然后我们控制并决定是否应该允许滚动视图滚动:
static const CGFloat kArbritaryHeight = 17.f;
CGFloat adjustedContentHeight = myTextView.contentSize.height - kArbritaryHeight;
CGFloat boundsHeight = CGRectGetHeight(myTextView.bounds);
BOOL tooMuchContent = adjustedContentHeight > boundsHeight;
if (tooMuchContent)
{
myTextView.scrollEnabled = YES;
}
else
{
myTextView.scrollEnabled = NO;
}
回答by Paul O.
You might use the sizeWithFont:constrainedToSize:lineBreakMode:
method to check whether your string will actually render larger than your text view and see if you need to enable scrolling. You will have to call it any time the text in your scrollview is set, however.
您可以使用该sizeWithFont:constrainedToSize:lineBreakMode:
方法来检查您的字符串是否实际渲染得比您的文本视图大,并查看您是否需要启用滚动。但是,只要设置了滚动视图中的文本,您就必须随时调用它。
ex:
前任:
CGSize barfStringSize = [barfString sizeWithFont:[barf_ font]
constrainedToSize:CGSizeMake(barf_.bounds.size.width, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap]
[barf_ setScrollEnabled:barfStringSize.height > barf_.bounds.size.height]