【问题标题】:Custom button in iOS with rounded rect stroke, icon and labeliOS 中带有圆角矩形笔划、图标和标签的自定义按钮
【发布时间】:2012-09-17 18:03:18
【问题描述】:

我想使用代码重新创建此按钮,使其成为可重复使用的对象,并且您可以设置最小宽度、高度或拉伸以适合图标和标签。在整个应用程序中,我们将在多个区域重复使用该按钮,它将包括一个细圆角矩形描边、一个背景颜色、一个图标(trans PNG)和一个标签。我们想让背景颜色和笔触颜色可配置,以便我们可以打开/关闭按钮。


编辑:几乎可以工作的代码,但文本标签块是白色的,需要调整图像大小以适合框架并且两者都要居中。

自定义按钮代码:

#import "CustomButton.h"

@implementation CustomButton

- (id)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title background:(UIColor *)background border:(UIColor *)border 
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self = [UIButton buttonWithType:UIButtonTypeCustom];
        CALayer *layer = [self layer];

        self.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

        // background
        if (background) {
            layer.backgroundColor = (__bridge CGColorRef)(background);
        } else {
            layer.backgroundColor = [[UIColor darkGrayColor] CGColor];
        }

        // border
        if (border) {
            layer.borderColor = (__bridge CGColorRef) (border);
        } else {
            layer.borderColor = [[UIColor lightGrayColor] CGColor];
        }
        layer.cornerRadius = 2.0f;
        layer.borderWidth = 0.5f;

        // icon
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
                                  [self addSubview:imageView];

        // text label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 55, 15)];
        titleLabel.font = [[UIFont alloc] fontWithSize:7.00];
                                titleLabel.text = title;
                                [self addSubview:titleLabel];

        [self setFrame:frame];
    }
    return self;
}

@end

编辑:更新了上面的代码块,并使用以下代码在 viewController 的相应视图中显示按钮:

CGRect buttonFrame = CGRectMake(125, 3, 52, 37);
CustomButton *btnNearby = [[CustomButton alloc] initWithFrame:buttonFrame image:@"map.png" title:@"NEARBY" background:nil border:nil];
[myCustomView addSubview:btnNearby];

自定义按钮出现,但格式仍然不正确。


这是一个应出现在按钮中心的示例图标(白色 PNG,带反式)。


所需功能的总结:

1) 可重复使用的按钮 2)可以具有最小宽度/高度或覆盖以匹配标签的宽度和图像+标签的高度 3) 具有可配置的描边颜色 4) 将上面的按钮图标与描边+图标+标签+背景颜色匹配 5) 可以改变边框颜色来开启/关闭

【问题讨论】:

  • 更新了我的代码块,以显示更多我试图通过 cmets 中的问题完成的工作。请参阅上面的代码。

标签: ios button


【解决方案1】:

你可以试试这样的:

#import <QuartzCore/QuartzCore.h>

@implementation CustomButton

- (id)initWithFrame:(CGRect)frame andImageName:(NSString*)filename ofType:(NSString*)type
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self = [UIButton buttonWithType:UIButtonTypeCustom];
        CALayer *layer = [self layer];
        layer.borderColor = [[UIColor blueColor] CGColor];
        layer.cornerRadius = 4.0f;
        layer.borderWidth = 2.0f;

        UIImage* img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:type]];
        UIImageView* imgView = [[UIImageView alloc] initWithImage:img];
        [imgView setFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
        [self addSubview:imgView];

        [self setFrame:frame];
    }
    return self;
}

- (void)switchColor
{
    CALayer *layer = [self layer];

    if(buttonIsOn)
        layer.borderColor = [[UIColor blueColor] CGColor];
    else
        layer.borderColor = [[UIColor grayColor] CGColor];
}

@end

而每次你使用这个按钮时,只需使用:

CustomButton* cusButton = [[CustomButton alloc] initWithFrame:someFrame];

为了交替描边颜色,只需在cusButton的目标方法的第一行调用switchColor就可以了。

【讨论】:

  • 当我想启动按钮时,我可以像 initWithImage:@"map.png" 那样在初始化期间声明图像吗?什么是 someFrame ?以及在哪里添加图标下方的标签文本?
  • isButtonOn 也没有在任何地方声明。只接受 switchColor 中的颜色并将其设置为该颜色而不是固定值可能更好吗?
  • 好的,我更新了答案以更改初始化程序以包含图像名称和类型。我假设您使用的是保存在包中的图像。 someFrame 可以是任何框架——这只是我在那里使用的一个例子。它是您希望 cusButton 所在的框架。 isButtonOn 是您可以添加到 CustomButton 类的接口的属性。它可以是BOOL 类型,并且可以根据您的要求进行初始化。
  • 在示例图像中查看图像下方的文本标签。看来您的解决方案不包含文本标签?另外,在实例化按钮时,我看不到声明按钮颜色或笔触颜色的方法。最后,一种设置大小的方法。鉴于我可能想要一个标准的宽度/高度,但在某些情况下,我只是让它由标签的宽度和图像+标签的高度来设置。
  • 我给了你一个很好的指引,让你从这里继续前进。您在这里提出的问题似乎更像是一个直接的答案,而不是一个有用的方向,所以我建议您遵循我在回答中给出的相同概念。重新编写指定的初始化程序并使用一些逻辑到您想要的位置。时间不应超过 30 分钟!谢谢!我看到你已经使用了我的部分答案来更新你的问题,所以如果你觉得答案有帮助,请接受并投票。
【解决方案2】:

我能够解决这个问题,并且我确信它可以进一步改进,但现在按钮会按需要显示。请参阅最终结果的快照,以及下面的工作代码,希望能对其他人有所帮助。

工作截图:

工作代码:

自定义按钮.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface CustomButton : UIButton
- (id)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title background:(UIColor *)background border:(UIColor *)border;
@end

自定义按钮.m

#import "CustomButton.h"

@implementation CustomButton

- (id)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title background:(UIColor *)background border:(UIColor *)border 
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self = [UIButton buttonWithType:UIButtonTypeCustom];
        CALayer *layer = [self layer];

        // background
        if (background) {
            layer.backgroundColor = (__bridge CGColorRef)(background);
        } else {
            layer.backgroundColor = [[UIColor darkGrayColor] CGColor];
        }

        // border
        if (border) {
            layer.borderColor = (__bridge CGColorRef)(border);
        } else {
            layer.borderColor = [[UIColor lightGrayColor] CGColor];
        }
        layer.cornerRadius = 2.0f;
        layer.borderWidth = 0.5f;

        // icon
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,3,20,19)];
        imageView.image = [UIImage imageNamed:image];
        imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:imageView];

        // text label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 50, 14)];
        titleLabel.opaque = NO;
        titleLabel.numberOfLines = 1;
        titleLabel.textAlignment = UITextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:7.00];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.text = title;
        [self addSubview:titleLabel];

        [self setFrame:frame];
    }
    return self;
}

@end

在 UIImage 层上实例化了与 View Controller 有 segue 的按钮:

// Add custom button to image view background layer
CGRect buttonFrame = CGRectMake(125, 3, 50, 35);
CustomButton *btnNearby = [[CustomButton alloc] initWithFrame:buttonFrame image:@"map.png" title:@"NEARBY" background:nil border:nil];
[myCustomView addSubview:btnNearby];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多