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

发布于 2024-09-11 11:58:20 字数 1743 浏览 4 评论 0原文

我以编程方式创建了 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];  // 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。网/uploads/b6e0f234dc.png image-1 http://www.freeimagehosting.net/uploads/6b3daab12f.png

所以是否可以根据文本设置确切的按钮框架大小?请指导我。

谢谢

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

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

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

发布评论

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

评论(4

雨巷深深 2024-09-18 11:58:20
[btn sizeToFit]
[btn sizeToFit]
渔村楼浪 2024-09-18 11:58:20

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

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

}

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

}
预谋 2024-09-18 11:58:20

非常简单的解决方案:

TestButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);
[TestButton sizeToFit];
float width  =   TestButton.frame.size.width + 20;
TestButton.frame = CGRectMake(TestButton.frame.origin.x, TestButton.frame.origin.y, width, 40);

Very simple solution:

TestButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);
[TestButton sizeToFit];
float width  =   TestButton.frame.size.width + 20;
TestButton.frame = CGRectMake(TestButton.frame.origin.x, TestButton.frame.origin.y, width, 40);
梦途 2024-09-18 11:58:20

复制并粘贴 Apple 的 Bubble Level 源代码。

UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];

这是您想要完整代码的

- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image {
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    [button setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    [button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenDisabled = YES;
    button.adjustsImageWhenHighlighted = YES;
    [button setBackgroundColor:[UIColor clearColor]];   // in case the parent view draws with a custom color or gradient, use a transparent color
    [button autorelease];
    return button;
}

Copy an pasted from Apples Bubble Level source code.

This is the line you want

UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];

full code here

- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image {
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    [button setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    [button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenDisabled = YES;
    button.adjustsImageWhenHighlighted = YES;
    [button setBackgroundColor:[UIColor clearColor]];   // in case the parent view draws with a custom color or gradient, use a transparent color
    [button autorelease];
    return button;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文