如何增加按钮宽度动态取决于iphone中的文字大小? [英] How can i increase the button width dynamically depends on the text size in iphone?

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

问题描述

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



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



这里我的示例代码是

  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]; //获取按钮标题

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.png
image-1 http: //www.freeimagehosting.net/uploads/6b3daab12f.png



所以这可以设置确切的按钮框架大小取决于文本?



谢谢

解决方案

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

  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]; //获取按钮标题

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

}


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

Here my sample code is,

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

}

see the example image,

image-2 http://www.freeimagehosting.net/uploads/b6e0f234dc.png image-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.

Thanks

解决方案

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中的文字大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆