OC/Swift 知识 UIButton
一直觉得自己写的不是技术而是情怀一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的希望我的这条路能让你们少走弯路希望我能帮你们抹去知识的蒙尘希望我能帮你们理清知识的脉络希望未来技术之巅上有你们也有我。属性属性注释frame约束showsTouchWhenHighlighted点击数是闪光效果会被前景图片盖住中间部分titleLabel.font设置文字大小enabled隐藏按键titleLabel.shadowOffset设置按键阴影layer.cornerRadius设置按键圆角layer.masksToBounds设置按键背景跟随圆角adjustsImageWhenDisabled当按键禁用的情况下图像的颜色会被画深一点默认为YESadjustsImageWhenHighlighted当按键禁用情况下图像的颜色会被画深一点默认为YESshowsTouchWhenHighlighted点击时的闪光效果会被前景图片盖住中间部分contentEdgeInsets设置按键内间距contentHorizontalAlignment设置按钮内容的左对齐方法方法注释- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state设置按键的图片- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state设置按键背景颜色- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;设置文字文本- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state设置文本颜色- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents按键添加点击事件- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state设置按键阴影颜色使用//创建对象并设置frameself.btn[[UIButton alloc]initWithFrame:CGRectMake(50,100,50,40)];[self.view addSubview:self.btn];//设置按键的图片[self.btn setImage:[UIImage imageNamed:msgS]forState:UIControlStateNormal];//点击数是闪光效果会被前景图片盖住中间部分[self.btn setShowsTouchWhenHighlighted:YES];//设置按键背景颜色[self.btn setBackgroundImage:[UIImage imageNamed:msgS]forState:UIControlStateNormal];//设置文字文本[self.btn setTitle:contentforState:UIControlStateNormal];//设置文字大小self.btn.titleLabel.font[UIFont systemFontOfSize:22];//设置文字加粗self.btn.titleLabel.font[UIFont boldSystemFontOfSize:22];//设置文本颜色[self.btn setTitleColor:[UIColor redColor]forState:UIControlStateNormal];//按键添加点击事件[self.btn addTarget:selfaction:selector(btnClick)forControlEvents:UIControlEventTouchUpInside];//方法不带参数[self.btn addTarget:selfaction:selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];//方法带参数#selector(updateProgress(notification:))//Swift的写法button.addTarget(self,action:#selector(tapped(_:)),for:.touchUpInside)//Swift的写法//是否允许按键点击self.btn.enabledNO;//设置按键阴影self.btn.titleLabel.shadowOffsetCGSizeMake(-5,-5);[self.btn setTitleShadowColor:[UIColor grayColor]forState:UIControlStateNormal];//设置按键圆角self.btn.layer.cornerRadius5;//设置按键背景跟随圆角self.btn.layer.masksToBoundsYES;//当按键禁用的情况下图像的颜色会被画深一点默认为YESself.btn.adjustsImageWhenDisabledNO;//当按键禁用情况下图像的颜色会被画深一点默认为YESself.btn.adjustsImageWhenHighlightedNO;//点击时的闪光效果会被前景图片盖住中间部分self.btn.showsTouchWhenHighlightedNO;//设置按键内间距self.btn.contentEdgeInsetsUIEdgeInsetsMake(0,20,0,0);//设置按钮内容的左对齐self.btn.contentHorizontalAlignmentUIControlContentHorizontalAlignmentLeft;//方法带参数-(void)btnClick:(UIButton*)button{}有时候需要我们继承UIControl自定义我们想要的按键例如上面图片下面文字的按键系统自带的是没有的。我自己也写过针对按键的封装。https://blog.csdn.net/weixin_38716347/article/details/113832932需求0.关于阴影的使用阴影的添加实际上就是在控件的后面中间有一个阴影图形的发散点发散出来的效果看起来就像阴影所以设置阴影的半径大小在控件的基础上2就可以了透明度设置0.5适宜颜色是黑色偏移y轴 CGSizeMake(0, 1)1圆角半径可选不裁剪shadowOffset: 阴影偏移x 设置是正数 阴影向右x 设置是负数 阴影向左y 设置是正数 阴影向下y 设置是负数 阴影向上shadowOpacity: 阴影的透明度0.0 - 1.0 之间shadowColor:阴影背景shadowRadius阴影半径cornerRadius圆角半径masksToBounds不裁剪超出部分方法1:(推荐)四周添加阴影// 设置阴影的颜色 self.markerButton.layer.shadowColor [UIColor blackColor].CGColor; // 设置阴影的偏移量(x, y)负值可以让阴影向上或向左偏移 self.markerButton.layer.shadowOffset CGSizeMake(0, 2); // 设置阴影的透明度 self.markerButton.layer.shadowOpacity 0.5; // 设置阴影的模糊半径 self.markerButton.layer.shadowRadius 5; // 设置圆角可选 self.markerButton.layer.cornerRadius 4; // 防止裁剪视图如果有圆角否则阴影可能会被裁剪 self.markerButton.layer.masksToBounds NO;方法2:四周添加阴影UIView*views_1[UIView new];[self.view addSubview:views_1];views_1.backgroundColor[UIColor blackColor];views_1.frameCGRectMake(50,350,100,100);// 阴影颜色views_1.layer.shadowColor[UIColor blueColor].CGColor;// 阴影偏移量 默认为(0,3)UIBezierPath*path[UIBezierPath bezierPathWithRect:CGRectMake(-4,-4,1008,10012)];// 阴影透明度views_1.layer.shadowOpacity2;views_1.layer.shadowPathpath.CGPath;1.是否允许按键点击NO 不允许点击 YES 允许点击//是否允许按键点击 self.btn.enabled NO;2.如何修改按键图片跟文字的间距OC推荐[self.refreshButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 7.5, 0, 0)]; [self.refreshButton setImageEdgeInsets:UIEdgeInsetsMake(0, -7.5, 0, 0)];修改前修改后直接上代码// 设置图片和文本的内边距CGFloat spacing8.0;// 图片和文本之间的间距self.trueButton.imageEdgeInsetsUIEdgeInsetsMake(0,-spacing/2,0,spacing/2);self.trueButton.titleEdgeInsetsUIEdgeInsetsMake(0,spacing/2,0,-spacing/2);self.trueButton.contentEdgeInsetsUIEdgeInsetsMake(0,spacing/2,0,spacing/2);// 可选设置按钮内容的内边距// 调整按钮的大小以适应内容[self.trueButton sizeToFit];SwiftiOS15以下let spacing: CGFloat 8 button.imageEdgeInsets UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing/2) button.titleEdgeInsets UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)iOS15以上var MOPuttonConfig UIButton.Configuration.plain() MOPuttonConfig.imagePadding 4 MOPuttonConfig.titlePadding 0 MOPuttonConfig.contentInsets NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0) MOPuttonConfig.titleTextAttributesTransformer UIConfigurationTextAttributesTransformer { incoming in var outgoing incoming outgoing.font UIFont.systemFont(ofSize: 14) outgoing.foregroundColor Color.textTheme return outgoing } MOPuttonConfig.baseForegroundColor Color.textTheme MOPuttonConfig.background.backgroundColor .clear MOPButton.configuration MOPuttonConfig3.button文本显示居中对齐self.memberButton.titleLabel.textAlignmentNSTextAlignmentCenter;self.memberButton.contentHorizontalAlignmentUIControlContentHorizontalAlignmentCenter;Swift 右边对齐button.contentHorizontalAlignment.right button.contentEdgeInsetsUIEdgeInsets(top:0,left:0,bottom:0,right:1)4.如何把button的文本内容全部显示出来self.button[UIButton buttonWithType:UIButtonTypeSystem];NSString*title123456789qwertyuiopasdfghjklzxcvbnm;self.button.addTo(self.view).str(title).fnt(16).color([UIColor whiteColor]).bgColor([UIColor orangeColor]).makeCons(^{make.center.equal.view(self.view);make.width.equal.constants(200);make.height.equal.constants(100);});self.button.titleLabel.adjustsFontSizeToFitWidthYES;[self.button sizeToFit];5.按键的文本内容如何换行显示self.button[UIButton buttonWithType:UIButtonTypeSystem];[self.button setTitle:This is a long button title that needs to wrap to the next lineforState:UIControlStateNormal];[self.button.titleLabel setFont:[UIFont systemFontOfSize:17.0]];[self.button.titleLabel setNumberOfLines:0];// 设置为0表示自动换行[self.button.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];[self.button sizeToFit];self.button.addTo(self.view).color([UIColor whiteColor]).bgColor([UIColor orangeColor]).makeCons(^{make.center.equal.view(self.view);make.width.equal.constants(200);make.height.equal.constants(100);});self.button.titleLabel.adjustsFontSizeToFitWidthYES;[self.button sizeToFit];6.自定义按键(左字右图)#import SwitchServerButton.h interface SwitchServerButton() property (nonatomic, strong) UILabel *titleLabel; property (nonatomic, strong) UIImageView *imageView; end implementation SwitchServerButton - (instancetype)initWithFrame:(CGRect)frame { self [super initWithFrame:frame]; [self buildUI]; return self; } -(void) buildUI{ self.backgroundColor [UIColor whiteColor]; self.titleLabel [UILabel new]; self.titleLabel.textColor [Color theme]; self.titleLabel.addTo(self).fnt(16).str(切换服务器).makeCons(^{ make.centerY.equal.view(self); make.left.equal.view(self).constants(5); }); self.imageView [UIImageView new]; self.imageView.addTo(self).img(switch_server).makeCons(^{ make.width.height.equal.constants(25); make.centerY.equal.view(self); make.left.equal.view(self.titleLabel).right.constants(5); }); } end7.系统按键(左字右图)OC关键代码self.getMoreBtn.semanticContentAttribute UISemanticContentAttributeForceRightToLeft;所有代码self.getMoreBtn [UIButton new]; self.getMoreBtn.semanticContentAttribute UISemanticContentAttributeForceRightToLeft; [self.getMoreBtn setTitleColor:[UIColor colorWithRed:205.0/255.0 green:0.0/255.0 blue:30.0/255.0 alpha:1.0] forState:UIControlStateNormal]; [self.getMoreBtn setImage:[UIImage imageNamed:red_rightArrow_normal] forState:UIControlStateNormal]; [self.getMoreBtn setTitle:更多 forState:UIControlStateNormal]; self.getMoreBtn.titleLabel.font [UIFont systemFontOfSize:14]; // self.getMoreBtn.imageEdgeInsets UIEdgeInsetsMake(0, 6, 0, -6); self.getMoreBtn.addTo(self.bgView).borderRadius(13).border(1,[UIColor colorWithRed:205.0/255.0 green:0.0/255.0 blue:30.0/255.0 alpha:1.0]).makeCons(^{ make.centerY.equal.view(self.titleLabel); make.right.equal.view(self.bgView).constants(-10); make.height.equal.constants(26); make.width.equal.constants(65); }).onClick(^{ });Swiftbutton.semanticContentAttribute .forceRightToLeft8.裁切单边圆角SwiftbgView.layer.maskedCorners [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]