<tfoot id='IFpIK'></tfoot>
    <bdo id='IFpIK'></bdo><ul id='IFpIK'></ul>

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

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

      <legend id='IFpIK'><style id='IFpIK'><dir id='IFpIK'><q id='IFpIK'></q></dir></style></legend>

      如何调整 UIButton 的大小以适应文本而不使其比屏幕更宽?

      时间:2023-07-08

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

          <tbody id='0Ra0s'></tbody>
        <tfoot id='0Ra0s'></tfoot>
      • <small id='0Ra0s'></small><noframes id='0Ra0s'>

                本文介绍了如何调整 UIButton 的大小以适应文本而不使其比屏幕更宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 UIButton,我想调整它的大小以适应其中的任何文本.它的宽度设置为 280,但文本应换行到下一行并根据需要扩展按钮的高度.

                I have a UIButton that I want to resize to fit whatever text is inside it. It has a set width of 280, but the text should wrap to the next line(s) and extend the height of the button as needed.

                我已将换行设置为自动换行并尝试了 sizeToFit,但这只会使按钮变宽,而不是垂直.

                I've set line breaks to Word Wrap and tried sizeToFit but that only makes the button go wider, not vertical.

                推荐答案

                我设法让它在 UIButton 的类别中工作:

                I managed to get it working in a category of UIButton:

                @interface UIButton (ExpandsVertically)
                
                - (CGSize)sizeThatFits:(CGSize)size;
                
                @end
                
                @implementation UIButton (Expandable)
                
                - (CGSize)sizeThatFits:(CGSize)size {
                
                    // for the width, I subtract 24 for the border
                    // for the height, I use a large value that will be reduced when the size is returned from sizeWithFont
                    CGSize tempSize = CGSizeMake(size.width - 24, 1000);
                
                    CGSize stringSize = [self.titleLabel.text
                                         sizeWithFont:self.titleLabel.font
                                         constrainedToSize:tempSize
                                         lineBreakMode:UILineBreakModeWordWrap];
                
                    return CGSizeMake(size.width - 24, stringSize.height);
                }
                
                @end
                

                如果有人使用它,请确保设置:

                If anyone uses this make sure to set:

                myButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
                

                这篇关于如何调整 UIButton 的大小以适应文本而不使其比屏幕更宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:无法在 UITableview 中的单元格内单击 UIButton 下一篇:在两个 UIButton 图像之间淡入淡出

                相关文章

              1. <small id='0tzm7'></small><noframes id='0tzm7'>

                    <bdo id='0tzm7'></bdo><ul id='0tzm7'></ul>

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