Resizing UITextView

前端 未结 10 1393
隐瞒了意图╮
隐瞒了意图╮ 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;
    }
    
    0 讨论(0)
  • 2020-12-04 16:21

    There is an answer posted at How do I size a UITextView to its content?

    CGRect frame = _textView.frame;
    frame.size.height = _textView.contentSize.height;
    _textView.frame = frame;
    

    or better(taking into account contentInset thanks to kpower's comment)

    CGRect frame = _textView.frame;
    UIEdgeInsets inset = textView.contentInset;
    frame.size.height = _textView.contentSize.height + inset.top + inset.bottom;
    _textView.frame = frame;
    

    note: If you are going to reference a property of an object many times(e.g. frame or contentInset) it's better to assign it to a local variable so you don't trigger extra method calls(_textView.frame/[_textView frame] are method calls). If you are calling this code a lot(100000s of times) then this will be noticeably slower(a dozen or so method calls is insignificant).

    However... if you want to do this in one line without extra variables it would be

    _textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, _textView.frame.size.width, _textView.contentSize.height + _textView.contentInset.top + _textView.contentInset.bottom);
    

    at the expense of 5 extra method calls.

    0 讨论(0)
  • 2020-12-04 16:31

    Just set scrollEnabled to NO, or uncheck Scrolling Enabled in the Scroll View section in IB and the UITextView will self-size.

    0 讨论(0)
  • 2020-12-04 16:32

    After you add the UITextView to its parent if you set a Content Mode on it then it seems to resize itself automatically.

    This means you don't need to work out the height manually and apply a height contraint. It just seems to work!! Tested in iOS7 and iOS8 on iPad.

    e.g.

    --
    textView.contentMode = UIViewContentMode.Center;
    --
    

    If anyone can explain why this works it would be much appreciated.. I found it by accident when messing with options in interface builder.

    0 讨论(0)
  • 2020-12-04 16:36

    This works perfectly for me:

    #define MAX_HEIGHT 2000     
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14]
          constrainedToSize:CGSizeMake(100, MAX_HEIGHT)
              lineBreakMode:UILineBreakModeWordWrap];
    
    [textview setFont:[UIFont systemFontOfSize:14]];
    [textview setFrame:CGRectMake(45, 6, 100, size.height + 10)];
    textview.text = text;
    
    0 讨论(0)
  • 2020-12-04 16:40

    You can use setFrame: or sizeToFit.

    UPDATE:

    I use sizeToFit with UILabel, and it works just fine, but UITextView is a subclass of UIScrollView, so I can understand why sizeToFit doesn't produce the desired result.

    You can still calculate the text height and use setFrame, but you might want to take advantage of UITextView's scrollbars if the text is too long.

    Here's how you get the text height:

    #define MAX_HEIGHT 2000
    
    NSString *foo = @"Lorem ipsum dolor sit amet.";
    CGSize size = [foo sizeWithFont:[UIFont systemFontOfSize:14]
                  constrainedToSize:CGSizeMake(100, MAX_HEIGHT)
                      lineBreakMode:UILineBreakModeWordWrap];
    

    and then you can use this with your UITextView:

    [textView setFont:[UIFont systemFontOfSize:14]];
    [textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];
    

    or you can do the height calculation first and avoid the setFrame line:

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 100, size.height + 10)];
    
    0 讨论(0)
提交回复
热议问题