UITextview is getting randomly clipped when using Autolayout and or preferredFontForTextStyle
Asked Answered
L

4

7

Autolayout will randomly clip the text in my UITextView. It changes the size of the UITextContainerView when you rotate back and forth from landscape to portrait. It will work correctly several times but will randomly change the container and clip the static text.

If I disable Autolayout then the issue goes away. It also seems to go away if I eliminate the preferredFontForTextStyle code and keep the Autolayout.

The thing is I wanted to allow the user to set the text size and be able to use auto layout. The text is all static so this should be a simple thing.

Has anyone seen this? Is this a bug or have to do something incorrectly?

I have reduced the code to as small as I could in order to try and isolate the problem.

Here is the code

//  BugTest_ViewController.h
//
//
//
//

#import <UIKit/UIKit.h>

@interface BugTest_ViewController : UIViewController



@property (weak, nonatomic) IBOutlet UIImageView *image1;

@property (weak, nonatomic) IBOutlet UITextView *text1;



@end

//  BugTest_ViewController.m
//
//
//  
//

#import "BugTest_ViewController.h"

@interface BugTest_ViewController ()


@end

@implementation BugTest_ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
}

-(void)viewWillAppear:(BOOL)animated
{

    self.text1.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    [self.text1 setContentOffset:CGPointZero animated:YES];
    
}

Here is the container when the view looks as it should Good Container

Here is the container when the view is being clipped. Bad Container clipping my text

Lewan answered 25/1, 2016 at 0:16 Comment(4)
Check your constraints of text view.Christman
I did. There were no conflicts or warnings.Lewan
There a 4 constraints present. Leading Space to : Superview = -7.0 Bottom Space to: Superview = 0 Align Center x to: Image 1 Top space to :Image 1 = default.Lewan
Re-Tested with the latest Xcode release (Version 7.2.1 (7C1002)) and the problem persists. No response from Apple either.Lewan
C
3

The solution from this question helps me. I write something like this:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    _textView.scrollEnabled = NO;
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        _textView.scrollEnabled = YES;
    }];
}
Cymbiform answered 6/6, 2016 at 10:51 Comment(0)
S
1

The reason seems to be that a transformation applied to a UITextView can sometimes corrupt the view's dynamic layout. A screen rotation effectively applies rotation and translation transformations to the views during the transition.

The same issue happens when CGAffineTransform is applied to a UITextView. Here is a visual example where the _UITextContainerView (blue frame) is smaller than the UITextView (green background) after a CGAffineTransform, causing the text to be appear cropped.

enter image description here

One solution that worked in this case was to set the content inset after the transformation has finished:

textView.contentInset = .zero
view.layoutIfNeeded()
Shipmate answered 13/9, 2018 at 3:32 Comment(0)
S
0

Looks like this happens because of call setContentOffset: which confuses scrollView in UITextView. My solution for scroll down of UITextView:

NSRange range = NSMakeRange(self.outputTextView.text.length - 1, 1);
[self.outputTextView scrollRangeToVisible:range];
self.outputTextView.scrollEnabled = NO;
self.outputTextView.scrollEnabled = YES;

This re enabling of scroll fixes it.

Subak answered 3/4, 2018 at 13:34 Comment(0)
M
-1

Use

[text1 setNeedsLayout];
[text1 layoutIfNeeded]; 

when you change the frame of the text view. That should fix the problem.

Matriarch answered 2/4, 2018 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.