开发者

How to make a UITextView Expand with the text like the Notes app

How can you make a UITextView expand with th开发者_开发百科e text that is inside of it?


you could try this...

UITextView *textView; // your UITextView
NSString *text; // the text that you want to place in the UITextView
UIFont *textViewFont; // the font that you are using for your UITextView

CGSize size = {255,2000.0f};  //The default width, and max height you want to use

CGSize newSize = [text sizeWithFont:textViewFont
                 constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

textView.frame = CGRectMake(0,0,newSize.width, newSize.height);


You can try this..

CGRect frame = textView.frame;
frame.size.height = [textView contentSize].height;
textView.frame = frame;


Are you using UITextView inside UITableView?

Let's suppose you are using UITextView inside UITableView. In order to make UITextView height dynamic(As per the content inside it) we need to take care of following things:-

  1. Make sure UITextView constraints are properly given. e.g: I had used XIB for tableview cell and inside cell I had a UITextView. So, I will give constraint to UITextView 0 to all four sides e.g: Top, bottom, leading, trailing. (Depends on your requirements).

  2. Make sure UITextView attribute, autoscroll is disabled. It should be off.

  3. Make sure TableView cell height is UITableView.automaticDimension

  4. Now inside your 'textViewDidChange' delegate method, Just add this below mentioned code:

            UIView.setAnimationsEnabled(false)
            tableView?.beginUpdates()
            tableView?.endUpdates()
            UIView.setAnimationsEnabled(true)
    

Now your textView will auto expand, while you are typing init.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜