2024-06-07 13:38:31 +08:00

266 lines
9.4 KiB
Objective-C

//
// RSButton.m
// Serendipity
//
// Created by HI_LOSER on 2022/10/18.
//
#import "RSButton.h"
NSString * RSButtonStringFromControlState(UIControlState status) {
switch (status) {
case UIControlStateNormal:
return @"Normal";
case UIControlStateSelected:
return @"Selected";
case UIControlStateDisabled:
return @"Disabled";
case UIControlStateHighlighted:
return @"Highlighted";
case UIControlStateFocused:
return @"Focused";
case UIControlStateApplication:
return @"Application";
case UIControlStateReserved:
return @"Reserved";
default:
return @"Other";
}
}
@interface RSButton ()
@property (assign, nonatomic) RSButtonType type;
@property (strong, nonatomic) NSMutableDictionary * titleDcit;
@property (strong, nonatomic) NSMutableDictionary * titleColorDcit;
@property (strong, nonatomic) NSMutableDictionary * imageDcit;
@property (strong, nonatomic) NSMutableDictionary * backgroundImageDcit;
@end
@implementation RSButton
-(NSMutableDictionary *)titleColorDcit{
if (_titleColorDcit) return _titleColorDcit;
return _titleColorDcit = [NSMutableDictionary dictionary];
}
-(NSMutableDictionary *)backgroundImageDcit{
if (_backgroundImageDcit) return _backgroundImageDcit;
return _backgroundImageDcit = [NSMutableDictionary dictionary];
}
-(NSMutableDictionary *)imageDcit{
if (_imageDcit) return _imageDcit;
return _imageDcit = [NSMutableDictionary dictionary];
}
-(NSMutableDictionary *)titleDcit{
if (_titleDcit) return _titleDcit;
return _titleDcit = [NSMutableDictionary dictionary];
}
- (instancetype)initWithType:(RSButtonType)type {
self = [super init];
if (self) {
self.insets = UIEdgeInsetsZero;
self.type = type;
self.margin = 10;
[self loadSubViews];
}
return self;
}
- (void)loadSubViews {
UIImageView * backgroundImageView = [[UIImageView alloc]init];
[self addSubview:backgroundImageView];
_backgroundImageView = backgroundImageView;
UIImageView * imageView = [[UIImageView alloc]init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
_imageView = imageView;
UILabel * titleLabel = [UILabel new];
titleLabel.font = [UIFont boldSystemFontOfSize:13.0];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.numberOfLines = 0;
[self addSubview:titleLabel];
_titleLabel = titleLabel;
}
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat margin = (_imageView.image && ![self empty: _titleLabel.text]) ? self.margin : 0.0;
// [_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.top.right.bottom.equalTo(self);
// }];
if (self.type == RSButtonTypeAbove) {
[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(self.insets.top);
make.left.equalTo(self).offset(self.insets.left);
make.right.equalTo(self).offset(-self.insets.right);
}];
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_imageView.mas_bottom).offset(margin);
make.left.equalTo(self).offset(self.insets.left);
make.right.equalTo(self).offset(-self.insets.right);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
} else if (self.type == RSButtonTypeBelow) {
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(self.insets.top);
make.left.equalTo(self).offset(self.insets.left);
make.right.equalTo(self).offset(-self.insets.right);
}];
[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_titleLabel.mas_bottom).offset(margin);
make.left.equalTo(self).offset(self.insets.left);
make.right.equalTo(self).offset(-self.insets.right);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
} else if (self.type == RSButtonTypeLeft) {
[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(self.insets.left);
make.top.equalTo(self).offset(self.insets.top);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_imageView.mas_right).offset(margin);
make.right.equalTo(self).offset(-self.insets.right);
make.top.equalTo(self).offset(self.insets.top);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
} else if (self.type == RSButtonTypeRight) {
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(self.insets.left);
make.top.equalTo(self).offset(self.insets.top);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_titleLabel.mas_right).offset(margin);
make.right.equalTo(self).offset(-self.insets.right);
make.top.equalTo(self).offset(self.insets.top);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
} else if (self.type == RSButtonTypeOnlyImage) {
[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(self.insets.left);
make.right.equalTo(self).offset(-self.insets.right);
make.top.equalTo(self).offset(self.insets.top);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
_titleLabel.hidden = YES;
} else if (self.type == RSButtonTypeOnlyTitle) {
_imageView.hidden = YES;
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(self.insets.left);
make.right.equalTo(self).offset(-self.insets.right);
make.top.equalTo(self).offset(self.insets.top);
make.bottom.equalTo(self).offset(self.insets.bottom);
}];
}
}
- (void)setTitleFont:(UIFont *)titleFont{
_titleLabel.font = titleFont;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
[self setSubViewsValue];
}
- (void)setEnabled:(BOOL)enabled{
[super setEnabled:enabled];
[self setSubViewsValue];
}
- (void)setTitle:(NSString *) title forState:(UIControlState) state {
[self.titleDcit setObject:title forKey:RSButtonStringFromControlState(state)];
[self setSubViewsValue];
}
- (void)setTitleColor:(UIColor *) color forState:(UIControlState) state {
[self.titleColorDcit setObject:color forKey:RSButtonStringFromControlState(state)];
[self setSubViewsValue];
}
- (void)setImage:(UIImage *) image forState:(UIControlState) state {
if (image) {
[self.imageDcit setObject:image forKey:RSButtonStringFromControlState(state)];
[self setSubViewsValue];
}
}
-(void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state {
if (image) {
[self.backgroundImageDcit setObject:image forKey:RSButtonStringFromControlState(state)];
[self setSubViewsValue];
}
}
- (void)setSubViewsValue{
UIColor * textColor = self.titleColorDcit[RSButtonStringFromControlState(UIControlStateNormal)];
if (textColor) _titleLabel.textColor = textColor;
_imageView.image = self.imageDcit[RSButtonStringFromControlState(UIControlStateNormal)];
_titleLabel.text = self.titleDcit[RSButtonStringFromControlState(UIControlStateNormal)];
_backgroundImageView.image = self.backgroundImageDcit[RSButtonStringFromControlState(UIControlStateNormal)];
if (!self.enabled) {
UIImage * image = self.imageDcit[RSButtonStringFromControlState(UIControlStateDisabled)];
if (image) _imageView.image = image;
UIColor * color = self.titleColorDcit[RSButtonStringFromControlState(UIControlStateDisabled)];
if (color) _titleLabel.textColor = color;
NSString * text = self.titleDcit[RSButtonStringFromControlState(UIControlStateDisabled)];
if (![self empty:text]) _titleLabel.text = text;
UIImage * backgroundImage = self.backgroundImageDcit[RSButtonStringFromControlState(UIControlStateDisabled)];
if (backgroundImage) _backgroundImageView.image = backgroundImage;
} else if (self.selected) {
UIImage * image = self.imageDcit[RSButtonStringFromControlState(UIControlStateSelected)];
if (image) _imageView.image = image;
UIColor * color = self.titleColorDcit[RSButtonStringFromControlState(UIControlStateSelected)];
if (color) _titleLabel.textColor = color;
NSString * text = self.titleDcit[RSButtonStringFromControlState(UIControlStateSelected)];
if (![self empty:text]) _titleLabel.text = text;
UIImage * backgroundImage = self.backgroundImageDcit[RSButtonStringFromControlState(UIControlStateSelected)];
if (backgroundImage) _backgroundImageView.image = backgroundImage;
}
[self setNeedsLayout];
}
- (BOOL)empty:(NSString *)string{
if (string == nil || [string isKindOfClass:NSNull.class]) return YES;
else return string == nil || string.length == 0 || [string isEqualToString:@"<null>"];
}
@end