ios - iOS 中的自定义按钮,带有圆角矩形笔画、图标和标签

标签 ios button

Button example

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

Example layout over a view area with background pattern


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

自定义按钮代码:

#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 中相应 View 中的以下代码显示按钮:

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

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

enter image description here


这是一个示例图标(白色 PNG w/trans),应该出现在按钮的中心。

Example map pin icon to appear in button


所需功能摘要:

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

每次使用此按钮时,只需使用:

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

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

关于ios - iOS 中的自定义按钮,带有圆角矩形笔画、图标和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464806/

相关文章:

ios - 像 objective-C 一样,如何在 swift 中写入 "valueForKey"?

android - WP REST API 和 React Native : how to show content not in HTML

ios - 如何使用重叠的透明 View Controller 重新创建 FaceTime 的导航 View Controller 转换动画?

java - 发送网格 : How to set dynamic link value of a button

c# - 在 WPF 中关闭没有代码隐藏的窗口

iOS apple-touch-icon 仅适用于一个 URL

ios - 在应用中使用 Revmob 时,我的应用因广告支持框架而被拒绝

html - 如何重新设计或删除按钮(聚焦)的内边框

ios - 按钮约束 Swift

android - 我怎样才能给一系列带有背景颜色的按钮一个焦点?