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

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

      <tfoot id='iGusE'></tfoot>

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

        如何根据 iphone 中的文本大小动态增加按钮宽度?

        时间:2023-07-30
      2. <tfoot id='9vdkh'></tfoot>

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

              <small id='9vdkh'></small><noframes id='9vdkh'>

                  本文介绍了如何根据 iphone 中的文本大小动态增加按钮宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我以编程方式创建了 10 个按钮并在按钮中设置了标题.现在我想动态增加按钮框架的大小,它取决于文本.

                  I have created 10 buttons programmatically and set the titles in the button. Now i want to increase the button frame size dynamically,its depends on the text.

                  我给出了一些条件并设置了帧大小.但是我如何设置确切的帧大小取决于文本(动态获取文本).

                  I given some conditions and set the frame size. but how can i set the exact frame size depends on the text(get the text dynamically).

                  这是我的示例代码,

                       float x=0, y=0, w, h=20;
                  
                      for(int i = 100; i < 110; i++)
                       {
                           btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
                  
                           UIImage * img = [UIImage imageNamed:@"Round_Rect4.png"];
                  
                          [btn setBackgroundImage:img forState:UIControlStateSelected];
                  
                          titleString = [titleArray objectAtIndex:i-100];  // get button title
                  
                          if([titleString length] <= 5)
                          {
                              w = 50;
                              btn.frame = CGRectMake(x,y,w,h); 
                  
                              x = x + 70;
                          }
                  
                          if (([titleString length] >= 6) && ([titleString length] <=10))
                         {
                               w = 70;
                  
                               btn.frame = CGRectMake(x,y,w,h); 
                  
                               x = x + 90;
                         } 
                  
                         if(([titleString length] >= 11) && ([titleString length] <=15))
                         {
                               w = 105;
                               btn.frame = CGRectMake(x,y,w,h); 
                  
                               x = x + 120;
                  
                         }
                  
                         if([titleString length] >= 16)
                         {
                               w = 120;
                               btn.frame = CGRectMake(x,y,w,h); 
                  
                               x = x + 140;
                  
                         }
                  
                          [btn setTitle:titleString forState: UIControlStateNormal];
                  
                          [btn setTag:i];
                  
                          [self.view addSubview:btn];
                  

                  }

                  查看示例图片,

                  image-2 http://www.freeimagehosting.net/uploads/b6e0f234dc.pngimage-1 http://www.freeimagehosting.net/uploads/6b3daab12f.png

                  那么这是否可以设置取决于文本的确切按钮框架大小?请指导我.

                  So is this possible to set the exact button frame size which depends on the text?, plz guide me.

                  谢谢

                  推荐答案

                  我得到了答案,我的工作代码是,

                  I got the answer and my working code is,

                          float x=0;
                  
                          for(int i = 100; i < 110; i++)
                           {
                               btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
                  
                               UIImage * img = [UIImage imageNamed:@"Round_Rect4.png"];
                  
                              [btn setBackgroundImage:img forState:UIControlStateSelected];
                  
                              titleString = [titleArray objectAtIndex:i-100];  // get button title
                  
                             CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:12.0]];
                  
                              CGRect currentFrame = btn.frame;
                  
                              CGRect buttonFrame = CGRectMake(x, currentFrame.origin.y, fontSize.width + 22.0, fontSize.height + 12.0);
                  
                             [btn setFrame:buttonFrame];
                  
                              x = x + fontSize.width + 35.0;
                  
                              [btn setTitle:titleString forState: UIControlStateNormal];
                  
                              [btn setTag:i];
                  
                              [self.view addSubview:btn];
                  
                  }
                  

                  这篇关于如何根据 iphone 中的文本大小动态增加按钮宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将字符串属性添加到 Swift 中的 UIButton 下一篇:单击按钮滚动 UIScrollView

                  相关文章

                  1. <legend id='Edj3o'><style id='Edj3o'><dir id='Edj3o'><q id='Edj3o'></q></dir></style></legend>

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

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