<legend id='zbb5I'><style id='zbb5I'><dir id='zbb5I'><q id='zbb5I'></q></dir></style></legend>
  • <i id='zbb5I'><tr id='zbb5I'><dt id='zbb5I'><q id='zbb5I'><span id='zbb5I'><b id='zbb5I'><form id='zbb5I'><ins id='zbb5I'></ins><ul id='zbb5I'></ul><sub id='zbb5I'></sub></form><legend id='zbb5I'></legend><bdo id='zbb5I'><pre id='zbb5I'><center id='zbb5I'></center></pre></bdo></b><th id='zbb5I'></th></span></q></dt></tr></i><div id='zbb5I'><tfoot id='zbb5I'></tfoot><dl id='zbb5I'><fieldset id='zbb5I'></fieldset></dl></div>

    <small id='zbb5I'></small><noframes id='zbb5I'>

  • <tfoot id='zbb5I'></tfoot>

        <bdo id='zbb5I'></bdo><ul id='zbb5I'></ul>
      1. 如何根据内容调整 UITextView 的大小?

        时间:2023-09-13

                <small id='ZPDss'></small><noframes id='ZPDss'>

              • <tfoot id='ZPDss'></tfoot>

                  <tbody id='ZPDss'></tbody>
              • <legend id='ZPDss'><style id='ZPDss'><dir id='ZPDss'><q id='ZPDss'></q></dir></style></legend>
                <i id='ZPDss'><tr id='ZPDss'><dt id='ZPDss'><q id='ZPDss'><span id='ZPDss'><b id='ZPDss'><form id='ZPDss'><ins id='ZPDss'></ins><ul id='ZPDss'></ul><sub id='ZPDss'></sub></form><legend id='ZPDss'></legend><bdo id='ZPDss'><pre id='ZPDss'><center id='ZPDss'></center></pre></bdo></b><th id='ZPDss'></th></span></q></dt></tr></i><div id='ZPDss'><tfoot id='ZPDss'></tfoot><dl id='ZPDss'><fieldset id='ZPDss'></fieldset></dl></div>

                  <bdo id='ZPDss'></bdo><ul id='ZPDss'></ul>

                  本文介绍了如何根据内容调整 UITextView 的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有一种好方法可以调整 UITextView 的大小以符合其内容?比如说我有一个 UITextView 包含一行文本:

                  Is there a good way to adjust the size of a UITextView to conform to its content? Say for instance I have a UITextView that contains one line of text:

                  "Hello world"
                  

                  然后我添加另一行文本:

                  I then add another line of text:

                  "Goodbye world"
                  

                  在 Cocoa Touch 中是否有一个好方法可以获取 rect 来保存文本视图中的所有行,以便我可以相应地调整父视图?

                  Is there a good way in Cocoa Touch to get the rect that will hold all of the lines in the text view so that I can adjust the parent view accordingly?

                  作为另一个示例,查看日历应用程序中事件的注释字段 - 请注意单元格(以及它包含的 UITextView)如何扩展以容纳注释字符串中的所有文本行.

                  As another example, look at the notes' field for events in the Calendar application - note how the cell (and the UITextView it contains) expands to hold all lines of text in the notes' string.

                  推荐答案

                  这适用于 iOS 6.1 和 iOS 7:

                  This works for both iOS 6.1 and iOS 7:

                  - (void)textViewDidChange:(UITextView *)textView
                  {
                      CGFloat fixedWidth = textView.frame.size.width;
                      CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
                      CGRect newFrame = textView.frame;
                      newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
                      textView.frame = newFrame;
                  }
                  

                  或在 Swift 中(适用于 iOS 11 中的 Swift 4.1)

                  Or in Swift (Works with Swift 4.1 in iOS 11)

                  let fixedWidth = textView.frame.size.width
                  let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
                  textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
                  

                  如果您想要支持 iOS 6.1,那么您还应该:

                  If you want support for iOS 6.1 then you should also:

                  textview.scrollEnabled = NO;
                  

                  这篇关于如何根据内容调整 UITextView 的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法开始工作 CocoaPods 和 Yosemite 下一篇:如何为约束更改设置动画?

                  相关文章

                  <small id='IeLSy'></small><noframes id='IeLSy'>

                • <tfoot id='IeLSy'></tfoot>
                    <bdo id='IeLSy'></bdo><ul id='IeLSy'></ul>
                  <legend id='IeLSy'><style id='IeLSy'><dir id='IeLSy'><q id='IeLSy'></q></dir></style></legend>

                  1. <i id='IeLSy'><tr id='IeLSy'><dt id='IeLSy'><q id='IeLSy'><span id='IeLSy'><b id='IeLSy'><form id='IeLSy'><ins id='IeLSy'></ins><ul id='IeLSy'></ul><sub id='IeLSy'></sub></form><legend id='IeLSy'></legend><bdo id='IeLSy'><pre id='IeLSy'><center id='IeLSy'></center></pre></bdo></b><th id='IeLSy'></th></span></q></dt></tr></i><div id='IeLSy'><tfoot id='IeLSy'></tfoot><dl id='IeLSy'><fieldset id='IeLSy'></fieldset></dl></div>