// // CustomButton.m // HD wallpaper #import "CustomButton.h" @implementation CustomButton - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self commonInit]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; [self commonInit]; } - (void)commonInit { self.cornerRadius = 5.0; self.borderWidth = 1.0; self.borderColor = [UIColor blackColor]; self.shadowColor = [UIColor blackColor]; self.shadowRadius = 5.0; self.shadowOpacity = 0.5; self.shadowOffset = CGSizeMake(0, 2); [self updateAppearance]; } - (void)setCornerRadius:(CGFloat)cornerRadius { _cornerRadius = cornerRadius; [self updateAppearance]; } - (void)setBorderWidth:(CGFloat)borderWidth { _borderWidth = borderWidth; [self updateAppearance]; } - (void)setBorderColor:(UIColor *)borderColor { _borderColor = borderColor; [self updateAppearance]; } - (void)setShadowColor:(UIColor *)shadowColor { _shadowColor = shadowColor; [self updateAppearance]; } - (void)setShadowRadius:(CGFloat)shadowRadius { _shadowRadius = shadowRadius; [self updateAppearance]; } - (void)setShadowOpacity:(CGFloat)shadowOpacity { _shadowOpacity = shadowOpacity; [self updateAppearance]; } - (void)setShadowOffset:(CGSize)shadowOffset { _shadowOffset = shadowOffset; [self updateAppearance]; } - (void)updateAppearance { self.layer.cornerRadius = self.cornerRadius; self.layer.borderWidth = self.borderWidth; self.layer.borderColor = self.borderColor.CGColor; self.layer.shadowColor = self.shadowColor.CGColor; self.layer.shadowRadius = self.shadowRadius; self.layer.shadowOpacity = self.shadowOpacity; self.layer.shadowOffset = self.shadowOffset; } - (void)setImageAboveTitleWithSpacing:(CGFloat)spacing { self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView.frame.size.width, -self.imageView.frame.size.height - spacing, 0); self.imageEdgeInsets = UIEdgeInsetsMake(-self.titleLabel.intrinsicContentSize.height - spacing, 0, 0, -self.titleLabel.intrinsicContentSize.width); } @end //#import "CustomButton.h" // //// 创建并添加CustomButton //CustomButton *button = [[CustomButton alloc] initWithFrame:CGRectMake(100-1,100-1,200-1,50-1)]; //[button setTitle:@"自定义按钮" forState:UIControlStateNormal]; //[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; //[button setBackgroundColor:[UIColor systemBlueColor]]; //button.cornerRadius = 10.0; //button.borderWidth = 2.0; //button.borderColor = [UIColor whiteColor]; //button.shadowColor = [UIColor blackColor]; //button.shadowRadius = 5.0