UIButton 常用方法及属性

IOS UIButton事件
UIControlEventTouchDown单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside
当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside
****当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
****当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
****当一次触摸从控件窗口内部拖动到外部时。****
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
****所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
****当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents
通知所有事件。

// readonly 只读属性
NSString *title = button.currentTitle; // 获取按钮当前的标题
NSAttributedString *attributedTitle = button.currentAttributedTitle; // 获取按钮当前的标题属性
UIColor *titleColor = button.currentTitleColor; // 获取按钮当前的标题颜色
UIColor *shadowColor = button.currentTitleShadowColor; // 获取按钮当前的标题阴影颜色
UIImage *currentImage = button.currentImage; // 获取按钮当前的图片
UIImage *backgroundImage = button.currentBackgroundImage; // 获取按钮当前的背景图片
NSString *title1 = [button titleForState:UIControlStateNormal]; // 获取按钮指定状态的文字

    // 获取按钮指定状态的标题属性
    NSAttributedString *attributedTitle1 = [button attributedTitleForState:UIControlStateNormal];

    // 获取按钮指定状态的标题颜色
    UIColor *titleColor1 = [button titleColorForState:UIControlStateNormal];

    // 获取按钮指定状态的标题阴影颜色
    UIColor *shadowColor1 = [button titleShadowColorForState:UIControlStateNormal];

    // 获取按钮指定状态的图片
    UIImage *currentImage1 = [button imageForState:UIControlStateNormal];

    // 获取按钮指定状态的背景图片
    UIImage *backgroundImage1 = [button backgroundImageForState:UIControlStateNormal];

/** 下面是本人简单的封装 和 应用 /
/
*
Buttom

@param title 文本
@param titleColor 文本颜色
@param titleFount 文本字体大小
@param boldFount 文本字体粗细
@param backGroundColor 背景颜色
@param backGroundImg 背景图片


UIButton 常用方法及属性_第1张图片
269340B3-525A-4F64-BEA0-73B6BC60C68F.png

@return Button
*/

  • (UIButton *)setButtomTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFount:(CGFloat)titleFount boldSystem:(CGFloat)boldFount backGroundColor:(UIColor *)backGroundColor backGroundImg:(UIImage *)backGroundImg;

  • (UIButton *)setButtomTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFount:(CGFloat)titleFount boldSystem:(CGFloat)boldFount backGroundColor:(UIColor *)backGroundColor backGroundImg:(UIImage *)backGroundImg {
    UIButton *button = [UIButton new];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:titleFount];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:boldFount];
    [button setBackgroundColor:backGroundColor];
    [button setBackgroundImage:backGroundImg forState:UIControlStateNormal];
    return button;
    }

UIButton *button1 = [commonTool setButtomTitle:@"按钮1" titleColor:[UIColor redColor] titleFount:16 boldSystem:25 backGroundColor:[UIColor blueColor] backGroundImg:nil];
button1.frame = CGRectMake((SCREEN_WIDTH - 200)/2, 40, 200, 40);
// 内容内边距(标题文字和图片)
// button1.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 10, 10);
// 标题文字内边距
// button1.titleEdgeInsets = UIEdgeInsetsMake(40, 20, 10, 10);
// 图片内边距
// button1.imageEdgeInsets = UIEdgeInsetsMake(20, 10, 10, 10);
// 触摸状态时图标不变暗
// button1.adjustsImageWhenHighlighted = NO;
// 禁用状态时图标不变暗
// button1.adjustsImageWhenDisabled = YES;
// YES 激活状态,默认状态,NO 失效状态(不可点击)
button1.enabled = YES;
[self.view addSubview:button1];

UIButton *button2 = [commonTool setButtomTitle:@"按钮2" titleColor:[UIColor redColor] titleFount:16 boldSystem:25 backGroundColor:[UIColor blueColor] backGroundImg:[UIImage imageNamed:@"素材1"]];
button2.frame = CGRectMake((SCREEN_WIDTH - 200)/2, 90, 200, 40);
[button2 setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:@"素材2"] forState:UIControlStateSelected];
[button2 addTarget:self action:@selector(setButton2Action:) forControlEvents:UIControlEventTouchUpInside];
button2.showsTouchWhenHighlighted = YES;//点击按钮会发光
[self.view addSubview:button2];



UIButton *button3 = [commonTool setButtomTitle:@"按钮3" titleColor:[UIColor redColor] titleFount:16 boldSystem:25 backGroundColor:[UIColor blueColor] backGroundImg:nil];
button3.frame = CGRectMake((SCREEN_WIDTH - 200)/2, 140, 200, 40);
[self.view addSubview:button1];
//    button变为不可选状态且变为灰色
button3.userInteractionEnabled=NO;
button3.alpha=0.4;
[self.view addSubview:button3];

UIButton *button4 = [commonTool setButtomTitle:@"按钮4" titleColor:[UIColor redColor] titleFount:16 boldSystem:25 backGroundColor:[UIColor blueColor] backGroundImg:nil];
button4.frame = CGRectMake((SCREEN_WIDTH - 200)/2, 190, 200, 40);
//圆角 + 边框
button4.layer.cornerRadius = 5;
button4.layer.masksToBounds = YES;
button4.layer.borderWidth = 5;
button4.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1] CGColor];
[self.view addSubview:button4];

你可能感兴趣的:(UIButton 常用方法及属性)