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

允我心安 提交于 2019-12-22 18:43:01

问题


How can you make a UITextView expand with the text that is inside of it?


回答1:


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);



回答2:


You can try this..

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



回答3:


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.



来源:https://stackoverflow.com/questions/2097850/how-to-make-a-uitextview-expand-with-the-text-like-the-notes-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!