iOS中带有圆形矩形笔划、图标和标签的自定义按钮

iOS中带有圆形矩形笔划、图标和标签的自定义按钮,ios,button,Ios,Button,我想用代码重新创建这个按钮,使它成为一个可重用的对象,您可以设置最小宽度、高度或它的延伸以适合图标和标签。在整个应用程序中,我们将在几个区域重复使用该按钮,它将包括一个细细的圆形矩形笔划、一个背景色、一个图标(trans-PNG)和一个标签。我们希望使背景色和笔划颜色可配置,以便可以打开/关闭按钮 编辑:几乎可以工作的代码,但文本标签块是白色的,需要调整图像大小以适应框架,并使两者居中 自定义按钮代码: #import "CustomButton.h" @implementation Cu

我想用代码重新创建这个按钮,使它成为一个可重用的对象,您可以设置最小宽度、高度或它的延伸以适合图标和标签。在整个应用程序中,我们将在几个区域重复使用该按钮,它将包括一个细细的圆形矩形笔划、一个背景色、一个图标(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];
// 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];
“自定义”按钮出现,但格式仍然不正确


下面是一个示例图标(白色PNG w/trans),应该显示在按钮的中心


所需功能的摘要:

1) 可重复使用按钮 2) 可以具有最小宽度/高度或替代,以匹配标签的宽度和图像+标签的高度 3) 具有可配置的笔划颜色 4) 将上面的按钮图标与笔划+图标+标签+背景色匹配
5) 可以更改边框颜色以打开/关闭

您可以尝试以下操作:

#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
#导入
@实现自定义按钮
-(id)initWithFrame:(CGRect)frame和图像名:(NSString*)类型的文件名:(NSString*)类型
{
self=[super initWithFrame:frame];
如果(自我)
{
self=[UIButton按钮类型:UIButtonTypeCustom];
CALayer*层=[自层];
layer.borderColor=[[UIColor blueColor]CGColor];
层角半径=4.0f;
layer.borderWidth=2.0f;
UIImage*img=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]路径资源:类型的文件名:type]];
UIImageView*imgView=[[UIImageView alloc]initWithImage:img];
[imgView设置框:CGRectMake(0,0,img.size.width,img.size.height)];
[自添加子视图:imgView];
[自设置帧:帧];
}
回归自我;
}
-(无效)切换颜色
{
CALayer*层=[自层];
如果(按钮松)
layer.borderColor=[[UIColor blueColor]CGColor];
其他的
layer.borderColor=[[UIColor grayColor]CGColor];
}
@结束
每次使用此按钮时,只需使用:

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


为了改变笔划颜色,只需在
cusButton
的目标方法的第一行调用
switchColor
,您应该会很好。

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

工作屏幕截图:

工作代码:

自定义按钮

#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
实例化了UIImage层上与视图控制器相连的按钮:

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

当我想启动按钮时,我是否可以像initWithImage:@“map.png”那样在初始化期间声明图像?什么是相框?我应该在图标下面的什么地方添加标签文本?同时,isButtonOn也不会在任何地方声明。在switchColor中接受一种颜色并将其设置为该颜色而不是固定值是否更好?好的,我更新了答案以更改初始值设定项,以包括图像名称和类型。我假设您使用的是保存在捆绑包中的图像
someFrame
可以是任何帧-这只是我在那里使用的一个示例。它是您希望cusButton位于的框架
isButtonOn
是可以添加到
CustomButton
类界面的属性。它可以是
BOOL
类型,并且可以根据您的要求进行初始化。请参见示例图像中图像下方的文本标签。您的解决方案似乎不包含文本标签?此外,我没有看到在实例化按钮时声明按钮颜色或笔划颜色的方法。最后,一种设置大小的方法。考虑到大多数情况,我可能需要一个标准的宽度/高度,但在某些情况下,我只是让它由标签的宽度和图像+标签的高度来设置。我给了你一个很好的线索,让你从这里开始。你在这里所问的似乎是一个直接的答案,而不是一个有用的方向,因此我建议你遵循我在回答中给出的相同概念。重新编写指定的初始值设定项,并使用一些逻辑将其返回到您想要的位置。这不应该超过30分钟!谢谢我发现您已经使用了我的部分答案来更新您的问题,因此如果您发现答案有帮助,请接受它并进行投票。更新了我的代码块,以显示我试图通过注释中的问题实现的更多功能。请看上面的代码。