Resizing UITextView

前端 未结 10 1445
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 15:52

I have a UITextView added on my UIView. The textview added is not editable, it is just to display some data. The data displayed in the textview is

10条回答
  •  心在旅途
    2020-12-04 16:18

    The answer given by @Gabe doesn't work in iOS7.1 seemingly until after viewDidAppear. See my tests below.

    UPDATE: Actually, the situation is even more complicated. If you assign textView.text in the resizeTheTextView method, in iOS7, the resizing amounts to allowing for only a single line of text. Seriously odd.

    UPDATE2: See also UITextView content size different in iOS7

    UPDATE3: See my code at the very bottom for what I'm using now. Seems to do the job.

    #import "ViewController.h"
    
    @interface ViewController ()
    {
        UITextView *textView;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 1)];
        [self.view addSubview:textView];
        CALayer *layer = textView.layer;
        layer.borderColor = [UIColor blackColor].CGColor;
        layer.borderWidth = 1;
    
        textView.text = @"hello world\n\n";
    
        // Calling the method directly, after the view is rendered, i.e., after viewDidAppear, works on both iOS6.1 and iOS7.1
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setTitle:@"Change size" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(resizeTheTextView) forControlEvents:UIControlEventTouchUpInside];
        [button sizeToFit];
        CGRect frame = button.frame;
        frame.origin.y = 400;
        button.frame = frame;
        [self.view addSubview:button];
    
        // Works on iOS6.1, but does not work on iOS7.1
        //[self resizeTheTextView];
    }
    
    - (void) viewWillAppear:(BOOL)animated
    {
        // Does not work on iOS7.1, but does work on iOS6.1
        //[self resizeTheTextView];
    }
    
    - (void) viewDidAppear:(BOOL)animated
    {
        // Does work on iOS6.1 and iOS7.1
        //[self resizeTheTextView];
    }
    
    - (void) resizeTheTextView
    {
        NSLog(@"textView.frame.size.height: %f", textView.frame.size.height);
    
        NSLog(@"textView.contentSize.height: %f", textView.contentSize.height);
    
        // 5) From https://stackoverflow.com/questions/728704/resizing-uitextview
        CGRect frame = textView.frame;
        UIEdgeInsets inset = textView.contentInset;
        frame.size.height = textView.contentSize.height + inset.top + inset.bottom;
        textView.frame = frame;
    
        NSLog(@"inset.top: %f, inset.bottom: %f",  inset.top,  inset.bottom);
    
        NSLog(@"textView.frame.size.height: %f", textView.frame.size.height);
    
        NSLog(@"textView.contentSize.height: %f", textView.contentSize.height);
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    UPDATE3:

    if ([[UIDevice currentDevice] majorVersionNumber] < 7.0) {
        CGRect frame = _abstractTextView.frame;
        UIEdgeInsets inset = _abstractTextView.contentInset;
        frame.size.height = _abstractTextView.contentSize.height + inset.top + inset.bottom;
        _abstractTextView.frame = frame;
    }
    else {
        CGSize textViewSize = [_abstractTextView sizeThatFits:CGSizeMake(_abstractTextView.frame.size.width, FLT_MAX)];
        _abstractTextView.frameHeight = textViewSize.height;
    }
    

提交回复
热议问题