zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    112th LeetCode Weekly Contest Validate Stack Sequences
    112th LeetCode Weekly Contest Minimum Increment to Make Array Unique
    C# 有关系统音量的操作
    C# 关于时区的操作
    WordPress 设置GeoIP数据库目录权限时错误解决方案
    AtCoder Beginner Contest 113 C
    AtCoder Beginner Contest 113 B
    AtCoder Beginner Contest 113 A
    将Tomcat注册为Windows服务
    常用的调试方法
  • 原文地址:https://www.cnblogs.com/wjw-blog/p/11172362.html
Copyright © 2011-2022 走看看