Dynamic expand UITextView on ios 7

后端 未结 9 1368
天命终不由人
天命终不由人 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:24

    Finally i did it.

    If anyone is having the same problem just had this code before

        [_moreDetailsTextView sizeToFit];
    
    0 讨论(0)
  • 2020-12-29 09:24

    To address this exact problem I made an auto-layout based light-weight UITextView subclass which automatically grows and shrinks based on the size of user input and can be constrained by maximal and minimal height - all without a single line of code.

    Made primarily for use in Interface builder and only works with Auto layout

    https://github.com/MatejBalantic/MBAutoGrowingTextView

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

    GrowingTextViewHandler is an NSObject subclass which resizes text view as user types text. Handling resize via NSObject subclass is more useful rather than subclassing UITextView, because it will work for any subclass of UITextView. Here is the sample usage.

    @interface ViewController ()<UITextViewDelegate>
    
     @property (weak, nonatomic) IBOutlet UITextView *textView;
     @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
     @property (strong, nonatomic) GrowingTextViewHandler *handler;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
      self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint];
      [self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8];
     }
    
    - (void)textViewDidChange:(UITextView *)textView {
      [self.handler resizeTextViewWithAnimation:YES];
    }
    @end
    
    0 讨论(0)
  • 2020-12-29 09:32

    Recently I have found this CSGrowingTextView that sizes while you type, maybe it can help: https://github.com/cloverstudio/CSGrowingTextView

    0 讨论(0)
  • Neither of answers worked for me. I have editable UITextView that I want to dynamically expand in height as the text grows in multiple lines.

    It worked fine for iOS6 but I had problems on iOS7 with scrolling the text view in order to keep the cursor visible.

    The solution for me was to put bottom inset for text view on iOS7. Like this:

    _textView.contentInset = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0") ? UIEdgeInsetsMake(0, 0, DEFAULT_FONT_SIZE, 0) : UIEdgeInsetsZero;
    

    Satisfiable results. Good luck.

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

    Noticed in IOS7 sizeToFit wasn't working also - perhaps the solution may help you too

    [UITEXTVIEW sizeToFit];
    [UITEXTVIEW layoutIfNeeded];
    
    0 讨论(0)
提交回复
热议问题