Dynamic expand UITextView on ios 7

后端 未结 9 1369
天命终不由人
天命终不由人 2020-12-29 09:10

I am using this code

CGRect frame = self.mytext.frame;
frame.size.height = self.mytext.contentSize.height;
self.mytext.frame = frame;

But i

相关标签:
9条回答
  • 2020-12-29 09:41
    [UITextView sizeToFit];
    

    Using sizeToFit resizes the textview but cuts some text from the bottom. Any idea how to resolve this?

    Solution:

    Finally the method described at the following thread worked. https://stackoverflow.com/a/18818036/1869452

    0 讨论(0)
  • 2020-12-29 09:42

    I've worked on a complete solution to solve this on iOS 9.3.1. Find it here.

    0 讨论(0)
  • 2020-12-29 09:43

    Get the new size of the textView using this method

    CGSize sz = [_textView.text sizeWithFont:_textView.font]
    

    in case that didn't work very well with the height,get the width you just got from the preivous method and use in the next method to get the appropriate height you need

    - (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
    {
        UITextView *textView = [[UITextView alloc] init];
    
        [textView performSelectorOnMainThread:@selector(setAttributedText:)
                                        withObject:text
                                     waitUntilDone:YES];
    
        CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
        return size.height;
    }
    

    Note : if there was any animation in the view,all the changes made in the view.frame will be discarded as the view will be redrawn again. So it would be better to deal with the constraints than dealing with the frame itself as the changes you will make in the constraint will be permanent even if the view got refreshed.

    create IBoutlet of a constraint and connect it to your class like that

    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *your_constraint;
    

    change the value of the contraint just like the way you change the view.frame parameters like that

                _constraintHeaderTitle.constant = THE_NEW_VALUE;
    
    0 讨论(0)
提交回复
热议问题