等待UITextView完成更新文本(iOS 7.1) [英] Waiting for UITextView to Finish Updating Text (iOS 7.1)
问题描述
我正在编程更新 UITextView
的 attributedText
。更新后,我想调用 setContentOffset
,以便滚动到新文本的位置。
使用以下代码,调试日志输出相同的值:
- (void)appendToTextView:(NSString *)text
{
CGFloat bottomOfCurrentContent = _pastedText.contentSize.height;
NSLog(@Bottom of content,before =%f,bottomOfCurrentContent);
NSString * textViewText = text;
BOOL以前的HadText = _pastedText.text.length> 0;
if(previouslyHadText){
textViewText = [NSString stringWithFormat:@%@ \\\
%@,_pastedText.text,textViewText];
}
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = _lineSpacing;
NSDictionary * attribute = @ {NSParagraphStyleAttributeName:paragraphStyle,
NSFontAttributeName:[UIFont systemFontOfSize:18.0]};
_pastedText.attributedText = [[NSAttributedString alloc] initWithString:textViewText attributes:attribute];
if(previouslyHadText){
CGFloat bottomOfNewContent = _pastedText.contentSize.height;
NSLog(@Bottom of content,after =%f,bottomOfNewContent);
//设置内容偏移...
}
}
例如,输出:
2014-05-03 21:52:57.181 thisApp [7011: 60b]内容底部,之前= 244.000000
2014-05-03 21:52:57.182 thisApp [7011:60b]内容底部,之后= 244.000000
如果我人为地等待文本视图完成更新,则内容将按预期报告...
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(5.0 * NSEC_PER_SEC)),dispatch_get_main_queue(),^ {
CGFloat bottomOfNewContent = _pastedText.contentSize.height;
NSLog (@Bottom of content,after =%f,bottomOfNewContent);
//设置内容偏移...
});
输出:
2014-05-03 23:12:39.694 thisApp [7050:60b]底部的内容,before = 244.000000
2014-05-03 23:12:44.698 thisApp [7050:60b]内容的底部,之后= 451.000000
UITextView
的更新已完成?
更新: b
根据 indyfromoz 的建议,我可以通过代理方法 textViewDidChangeSelection
获得回调到文本视图。
如果我把日志语句放在那里:
- (void)textViewDidChangeSelection:(UITextView *)textView
{
CGFloat newContentHeight = textView.contentSize 。高度;
NSLog(@新内容的底部:%f,newContentHeight);
}
内容高度报告与更新前相同...
我把这个问题放在 UITextView
的错误。
插入新文本后,在 UITextView
中,您可以使用 scrollRangeToVisible
(通常通过 textViewDidChangeSelection
delegate方法)滚动文本视图的内容。 UITextView
在iOS 7中有一些错误,并有一个彼得讨论此文章的好文章和 Brent Simmons有一个近乎完美的解决方案用于在UITextView中滚动内容。
I'm programatically updating the attributedText
of a UITextView
. Following the update I want to call setContentOffset
so that I can scroll to position the new text.
The problem I'm having is that there's a delay in the text view updating its text.
With the following code the debug log outputs the same value:
- (void)appendToTextView:(NSString*)text
{
CGFloat bottomOfCurrentContent = _pastedText.contentSize.height;
NSLog(@"Bottom of content, before = %f", bottomOfCurrentContent);
NSString* textViewText = text;
BOOL previouslyHadText = _pastedText.text.length > 0;
if (previouslyHadText) {
textViewText = [NSString stringWithFormat:@"%@\n%@", _pastedText.text, textViewText];
}
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = _lineSpacing;
NSDictionary *attribute = @{ NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : [UIFont systemFontOfSize:18.0] };
_pastedText.attributedText = [[ NSAttributedString alloc] initWithString:textViewText attributes:attribute];
if (previouslyHadText) {
CGFloat bottomOfNewContent = _pastedText.contentSize.height;
NSLog(@"Bottom of content, after = %f", bottomOfNewContent);
// Set content offset...
}
}
E.g., output:
2014-05-03 21:52:57.181 thisApp[7011:60b] Bottom of content, before = 244.000000
2014-05-03 21:52:57.182 thisApp[7011:60b] Bottom of content, after = 244.000000
If I artificially wait for the text view to complete its update the content is reported as expected...
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CGFloat bottomOfNewContent = _pastedText.contentSize.height;
NSLog(@"Bottom of content, after = %f", bottomOfNewContent);
// Set content offset...
});
Output:
2014-05-03 23:12:39.694 thisApp[7050:60b] Bottom of content, before = 244.000000
2014-05-03 23:12:44.698 thisApp[7050:60b] Bottom of content, after = 451.000000
So, how do I get updated when the UITextView
s update has finished?
Update:
Based on the suggestion from indyfromoz, I can get a callback from the delegate method textViewDidChangeSelection
, following a programatic update to the text view. However, when that delegate method is called, the text view still hasn't finished updating.
If I drop the log statement in there:
- (void)textViewDidChangeSelection:(UITextView *)textView
{
CGFloat newContentHeight = textView.contentSize.height;
NSLog(@"bottom of new content: %f", newContentHeight);
}
The content height is reported the same as it was before the update...
I'm putting this down to a bug with UITextView
. I'll work around it for now and look at the code again in the future.
After the insertion of the new text in your UITextView
, you can use scrollRangeToVisible
(Typically through the textViewDidChangeSelection
delegate method) to scroll the text view's content. UITextView
in iOS 7 has some bugs and there is a good article by Peter discussing this and Brent Simmons has a near perfect solution for scrolling content in UITextView.
这篇关于等待UITextView完成更新文本(iOS 7.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!