• iOS 自定义一对UI表现相反的按钮


    假如有一对按钮【重置】【提交】,要让他们的默认UI和点击的UI表现刚好相反
    【提交】按钮,默认橙色,点击边框是橙色,字体是橙色,背景变白色
    【重置】按钮,默认白色橙色,边框是橙色,点击字体是白色,背景变橙色

    @implementation SubmmitButton
    
    - (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
        self = [super initWithFrame:frame];
        if (self) {
            UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
    //        UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
            self = [SubmmitButton new];
            [self setTitle:title forState:UIControlStateNormal];
            [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self setBackgroundImage:image forState:UIControlStateNormal];
            [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
            [self.layer setMasksToBounds:YES];
            [self.layer setCornerRadius:6];
        }
        return self;
    }
    
    - (void)setSelected:(BOOL)selected {
        [super setSelected:selected];
    }
    
    - (void)setHighlighted:(BOOL)highlighted {
        [super setHighlighted:highlighted];
        UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
        UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
        if (highlighted) {
            [self setBackgroundImage:selectedImage forState:UIControlStateHighlighted];
            [self setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
            self.layer.borderWidth = 1;
            self.layer.borderColor = [UIColor orangeColor].CGColor;
        }else {
            [self setBackgroundImage:image forState:UIControlStateNormal];
            [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        }
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    @end
    
    
    @implementation RestButton
    - (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
        self = [super initWithFrame:frame];
        if (self) {
    //        UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
            UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
            self = [RestButton new];
            [self setTitle:title forState:UIControlStateNormal];
            [self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [self setBackgroundImage:selectedImage forState:UIControlStateNormal];
            [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
            [self.layer setMasksToBounds:YES];
            [self.layer setCornerRadius:6];
            self.layer.borderWidth = 1;
            self.layer.borderColor = [UIColor orangeColor].CGColor;
        }
        return self;
    }
    
    - (void)setSelected:(BOOL)selected {
        [super setSelected:selected];
    }
    
    - (void)setHighlighted:(BOOL)highlighted {
        [super setHighlighted:highlighted];
        UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
        UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
        if (highlighted) {
            [self setBackgroundImage:image forState:UIControlStateHighlighted];
            [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        }else {
            [self setBackgroundImage:selectedImage forState:UIControlStateNormal];
            [self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            self.layer.borderWidth = 1;
            self.layer.borderColor = [UIColor orangeColor].CGColor;
        }
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    @end
    

    UIImage的一个分类方法

    /**
     根据颜色生图片
    
     @param color 生成图片的颜色
     @return 返回纯色图片
     */
    + (UIImage*)createImageWithColor:(UIColor*)color {
        CGRect rect = CGRectMake(0.0f,0.0f,10.0f,10.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context=UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
        UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
        return theImage;
    }
    
  • 相关阅读:
    用django2.1开发公司官网(上)
    vue常用手册
    xadmin+django2.0删除用户报错,get_deleted_objects() takes 3 positional arguments but 5 were given
    Vue+koa2开发一款全栈小程序(9.图书详情页)
    Vue+koa2开发一款全栈小程序(8.图书列表页)
    Vue+koa2开发一款全栈小程序(7.图书录入功能)
    Vue+koa2开发一款全栈小程序(6.个人中心)
    Vue+koa2开发一款全栈小程序(5.服务端环境搭建和项目初始化)
    java中基本类型double和对象类型Double
    vb中的除法
  • 原文地址:https://www.cnblogs.com/wjw-blog/p/11172362.html
Copyright © 2020-2023  润新知