如何使 UITextView 像 Notes 应用程序一样使用文本展开

发布于 2024-08-18 14:42:44 字数 32 浏览 3 评论 0原文

如何使 UITextView 随其内部的文本展开?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流殇 2024-08-25 14:42:44

你可以试试这个...

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 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);
高速公鹿 2024-08-25 14:42:44

你可以试试这个..

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

You can try this..

CGRect frame = textView.frame;
frame.size.height = [textView contentSize].height;
textView.frame = frame;
骷髅 2024-08-25 14:42:44

你在 UITableView 中使用 UITextView 吗?

假设您在 UITableView 中使用 UITextView。为了使 UITextView 高度动态化(根据其中的内容),我们需要注意以下事项:-

  1. 确保正确给出 UITextView 约束。例如:我使用 XIB 作为 tableview 单元格,并且在单元格内部我有一个 UITextView。因此,我将对 UITextView 0 的所有四个边进行约束,例如:顶部、底部、前导、尾随。 (取决于您的要求)。

  2. 确保 UITextView 属性自动滚动已禁用。它应该关闭。

  3. 确保 TableView 单元格高度为 UITableView.automaticDimension

  4. 现在在“textViewDidChange”委托方法中,只需添加下面提到的代码:

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

现在,当您键入 init 时,您的 textView 将自动展开。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文