iOS UIButton 常用 Category(类目)分享

西门桃桃 2020-08-01 PM 137℃ 0条

1、UIButton 设置各种状态的图片

/** 设置图片和选择后的图片 **/
+ (instancetype)buttonWithImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName tag:(NSInteger)tag target:(id)target sel:(SEL)sel{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:CCLiveAssetsImage(imageName) forState:UIControlStateNormal];
    [btn setImage:CCLiveAssetsImage(selectedImageName) forState:UIControlStateSelected];
    btn.tag = tag;
    [btn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
/** 普通标题和背景颜色 */
+ (instancetype) buttonWithTitle:(NSString*) title backGroudColor:(UIColor*) color
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5;
    [btn.layer masksToBounds];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithWhite:0.8 alpha:1] forState:UIControlStateHighlighted];
    [btn setBackgroundColor:color];
    return btn;
}


/**标题,文字颜色,文字大小 */
+ (instancetype) buttonWithTitle:(NSString*) title TitleColor:(UIColor*) color TitleSize:(CGFloat) size{
    UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [customBtn setTitle:title forState:UIControlStateNormal];
    [customBtn setTitleColor:color forState:UIControlStateNormal];
    customBtn.titleLabel.font = [UIFont systemFontOfSize:size];
    return customBtn;
}


/**标题,文字颜色,文字大小,icon图标 */
+ (instancetype) buttonWithTitle:(NSString*) title TitleColor:(UIColor*) color TitleSize:(CGFloat) size IconImage:(UIImage*) icon{
    UIButton *customBtn = [self buttonWithTitle:title TitleColor:color TitleSize:size];
    [customBtn setImage:icon forState:UIControlStateNormal];
    return customBtn;
}


/**标题,颜色,文字大小,普通背景图片,高亮背景图 */
+ (instancetype) buttonWithTitle:(NSString*) title TitleColor:(UIColor*) color TitleSize:(CGFloat) size BackgroundNormalImage:(UIImage*) backgroundNormalImage BackgroundHighlightedImage:(UIImage*) backgroundHighlightedImage{
    UIButton *customBtn = [self buttonWithTitle:title TitleColor:color TitleSize:size];
    [customBtn setBackgroundImage:backgroundNormalImage forState:UIControlStateNormal];
    [customBtn setBackgroundImage:backgroundHighlightedImage forState:UIControlStateHighlighted];
    return customBtn;
}
/**标题,颜色,文字大小,普通背景图片,高亮背景图 ,圆角半径,边框颜色,边框尺寸*/
+ (instancetype) buttonWithTitle:(NSString*) title TitleColor:(UIColor*) color TitleSize:(CGFloat) size BackgroundNormalImage:(UIImage*) backgroundNormalImage BackgroundHighlightedImage:(UIImage*) backgroundHighlightedImage CornerRadius:(CGFloat) cornerRadius BorderColor:(UIColor*) borderColor BorderWidth:(CGFloat) borderWidth{
    UIButton *customBtn = [self buttonWithTitle:title TitleColor:color TitleSize:size BackgroundNormalImage:backgroundNormalImage BackgroundHighlightedImage:backgroundHighlightedImage];
    if (cornerRadius !=0) {
        [customBtn.layer setCornerRadius:cornerRadius];
        [customBtn.layer setMasksToBounds:YES];
    }
    if (borderColor) {
        customBtn.layer.borderColor = [borderColor CGColor];
    }
    if (borderWidth != 0) {
        customBtn.layer.borderWidth = borderWidth;
    }
    return customBtn;
}

-(void)setBackgroundWithStretchImageName:(NSString*)imageName{
    UIImage*bgImage=CCLiveAssetsImage(imageName);
    bgImage=[bgImage resizableImageWithCapInsets:UIEdgeInsetsMake(bgImage.size.height/2-1, bgImage.size.width/2-1, bgImage.size.height/2-1, bgImage.size.width/2-1) resizingMode:UIImageResizingModeStretch];
    [self setBackgroundImage:bgImage forState:UIControlStateNormal];
}

2、判断点击位置是否在按钮内

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect rect = self.bounds;

    // top
    rect.origin.y += _touchInsets.top;
    rect.size.height -= _touchInsets.top;
    // left
    rect.origin.x += _touchInsets.left;
    rect.size.width -= _touchInsets.left;
    // bottom
    rect.size.height -= _touchInsets.bottom;
    // right
    rect.size.width -= _touchInsets.right;

    if (CGRectContainsPoint(rect, point)) {
        return YES;
    }

    return [super pointInside:point withEvent:event];
}

非特殊说明,本博所有文章均为博主原创。

评论啦~