Scroll to bottom of UITextView erratic in iOS 7
This is obviously an iOS 7 bug. Here is a workaround until apple fixes it. The workaround is basically instantiates a UITextView
by creating an NSTextStorage
and NSLayoutManager
from scratch. Apple must have forgotten to initialize something in UITextView
initialization method. I filed a bug report and I hope you do too.
// ios7 bug fix
// check if the device is running iOS 7.0 or later
NSString *reqSysVer = @"7.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
if (osVersionSupported) {
NSTextStorage* textStorage = [[NSTextStorage alloc] init];
NSLayoutManager* layoutManager = [NSLayoutManager new];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
[layoutManager addTextContainer:textContainer];
yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView
textContainer:textContainer];
// if using ARC, remove these 3 lines
[textContainer release];
[layoutManager release];
[textStorage release];
}
else {
yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView];
}
Scroll UITextView To Bottom
You can use the following code if you are talking about UITextView:
-(void)scrollTextViewToBottom:(UITextView *)textView {
if(textView.text.length > 0 ) {
NSRange bottom = NSMakeRange(textView.text.length -1, 1);
[textView scrollRangeToVisible:bottom];
}
}
SWIFT 4:
func scrollTextViewToBottom(textView: UITextView) {
if textView.text.count > 0 {
let location = textView.text.count - 1
let bottom = NSMakeRange(location, 1)
textView.scrollRangeToVisible(bottom)
}
}
UITextView setText should not jump to top in ios8
Try just to add text to UITextView (without scrollRangeToVisible/scrollEnabled). It seams that hack with scroll enabled/disabled is no more needed in iOS8 SDK. UITextView scrolls automatically.
iOS 7 - UITextView size font to fit all text into view (no scroll)
Solution 1
Your problem can be solved by simply replacing sizeWithFont: constrainedToSize:
with :
boundingRectWithSize:CGSizeMake(newView.frame.size.width, FLT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Interstate" size:fontSize]}
context:nil];
Solution 2
The sizeThatFits
method can be used to address this problem like this:
while (fontSize > minSize && [newView sizeThatFits:(CGSizeMake(newView.frame.size.width, FLT_MAX))].height >= newView.frame.size.height ) {
fontSize -= 1.0;
newView.font = [tv.font fontWithSize:fontSize];
}
I hope one of these solutions solve your problem. Cheers!
UITextView cursor not positioning properly when editing in iOS 7. Why?
this bug is in iOS 7.0 you can solve this by modifying textView delegate method.
try below code
- (void)textViewDidChange:(UITextView *)textView {
CGRect line = [textView caretRectForPosition:
textView.selectedTextRange.start];
CGFloat overflow = line.origin.y + line.size.height
- ( textView.contentOffset.y + textView.bounds.size.height
- textView.contentInset.bottom - textView.contentInset.top );
if ( overflow > 0 ) {
// We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
// Scroll caret to visible area
CGPoint offset = textView.contentOffset;
offset.y += overflow + 7; // leave 7 pixels margin
// Cannot animate with setContentOffset:animated: or caret will not appear
[UIView animateWithDuration:.2 animations:^{
[textView setContentOffset:offset];
}];
}
}
your problem will solved.
How to make a UITextView scroll while typing/editing
Problems with other answers:
- when only scanning for "\n", if you type a line of text that exceeds the width of the text view, then scrolling will not occur.
- when always setting contentOffset in textViewDidChange:, if you edit the middle of the text you do not want to scroll to the bottom.
The solution is to add this to the text view delegate:
- (void)textViewDidChange:(UITextView *)textView {
CGRect line = [textView caretRectForPosition:
textView.selectedTextRange.start];
CGFloat overflow = line.origin.y + line.size.height
- ( textView.contentOffset.y + textView.bounds.size.height
- textView.contentInset.bottom - textView.contentInset.top );
if ( overflow > 0 ) {
// We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
// Scroll caret to visible area
CGPoint offset = textView.contentOffset;
offset.y += overflow + 7; // leave 7 pixels margin
// Cannot animate with setContentOffset:animated: or caret will not appear
[UIView animateWithDuration:.2 animations:^{
[textView setContentOffset:offset];
}];
}
}
Related Topics
Add Swipe to Delete Uitableviewcell
Paging Uiscrollview in Increments Smaller Than Frame Size
Rotating a View in Layoutsubviews
How to Set Image in Tabbar Not Tint Color in iOS
Uiview Backgroundcolor Disappears When Uitableviewcell Is Selected
Atos Cannot Get Symbols from Dsym of Archived Application
iOS Autolayout: Two Buttons of Equal Width, Side by Side
Uiview Vertical Flip Animation
Downloading Uiimage via Alamofireimage
Which Is the Best Way to Estimate Measure of Photographed Things
Xcode 8.3/Xcode 9.0 Refresh Provisioning Profile Devices
Is There a Public Way to Force Mpnowplayinginfocenter to Show Podcast Controls
Xcode Process Launch Failed: Security
How to Create Auto Incremented Key in Firebase
Swift Unrecognized Selector Sent to Instance Error
How to Use Navigation Controller Inside of Uitabbarcontroller with Storyboard on Swift