zoukankan      html  css  js  c++  java
  • ios开发UI篇--UIButton

    概述

    • UIButton 是执行自定义代码以响应用户交互的控件。
    • UIButton 其实包含 UIImageViewUILabel 两个控件,UIButton 继承于 UIControl,所以有 addtarget 监听事件

    属性和方法

    初始化Button不用alloc init方法,使用便利构造器方法

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    

    UIButton的类型如下

    UIButton类型说明
    UIButtonTypeCustom 没有按钮样式,一般设置该样式,根据需要自定义
    UIButtonTypeSystem 系统样式按钮,例如导航栏和工具栏中显示的按钮。
    UIButtonTypeDetailDisclosure 详细披露按钮。
    UIButtonTypeInfoLight 具有浅色背景的信息按钮。
    UIButtonTypeInfoDark 信息按钮有一个黑暗的背景。
    UIButtonTypeContactAdd 联系人添加按钮。
    UIButtonTypePlain 没有模糊背景视图的标准系统按钮。
    UIButtonTypeRoundedRect 已经废弃,使用UIButtonTypeSystem代替

    UIButton的状态如下

    UIButton的状态说明
    UIControlStateNormal 控件的正常状态或默认状态 - 即已启用但未选中或高亮显示。
    UIControlStateHighlighted 突出显示的控制状态。按钮处于选中状态时的状态
    UIControlStateDisabled 一个控件的禁用状态。
    UIControlStateSelected 选择一个控件的状态。
    UIControlStateFocused 集中控制状态。
    UIControlStateApplication 额外的控制状态标志可用于应用程序使用。
    UIControlStateReserved 控制状态标志保留给内部框架使用。

    设置frame

    [btn setFrame:CGRectMake(100, 100, 100, 30)];
    

    设置某一状态下的标题

    [btn setTitle:@"测试" forState:(UIControlStateNormal)];
    

    设置某一状态下的标题颜色

    [btn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    

    设置某一状态下的阴影颜色

    [btn setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];
    

    设置某一状态下的背景颜色

    [btn setBackgroundColor:[UIColor blackColor]];
    

    设置标题字体大小

     btn.titleLabel.font = [UIFont systemFontOfSize:30];
    

    设置某一状态下的背景图片(背景图片显示在其标题和前景图像后面。)

    [btn setBackgroundImage:[UIImage imageNamed:@"登录logo"] forState:(UIControlStateNormal)];
    

    设置某一状态下的前景图片

    [btn setImage:[UIImage imageNamed:@"验证码"] forState:(UIControlStateNormal)];
    

    设置标题内边距

    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);   top, left, bottom, right;

    设置图片内边距

    btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    

    设置内容内边距

    btn.contentEdgeInsets = UIEdgeInsetsMake(10, -30, 10, 10);
    

    标题的阴影改变时,按钮是否高亮显示。默认为NO

    btn.reversesTitleShadowWhenHighlighted = YES;
    

    按钮高亮的情况下,图像的颜色是否要加深一点。默认是YES

    btn.adjustsImageWhenHighlighted = YES;
    

    按钮禁用的情况下,图像的颜色是否要加深一点。默认是YES

    btn.adjustsImageWhenDisabled = YES;
    

    按下按钮是否会发光 默认是NO

    btn.showsTouchWhenHighlighted = NO;
    

    返回button 某个状态下的标题

    - (nullable NSString *)titleForState:(UIControlState)state;
    

    返回button 某个状态下的标题颜色

    - (nullable UIColor *)titleColorForState:(UIControlState)state;
    

    返回button 某个状态下的阴影标题颜色

    - (nullable UIColor *)titleShadowColorForState:(UIControlState)state;
    

    返回button 某个状态下的图片

    - (nullable UIImage *)imageForState:(UIControlState)state;
    

    返回button 某个状态下的背景图片

    - (nullable UIImage *)backgroundImageForState:(UIControlState)state;
    

    返回button 某个状态下的富文本标题

    - (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);
    

    获取按钮当前标题

    NSString *title = btn.currentTitle;
    

    获取按钮当前标题颜色

    UIColor *color = btn.currentTitleColor;
    

    获取按钮当前阴影标题颜色

    UIColor *shandowColor = btn.currentTitleShadowColor;
    

    获取按钮当前按钮内图像

    UIImage *image = btn.currentImage;
    

    获取按钮当前标题背景图片

    UIImage *backgroundImage = btn.currentBackgroundImage;
    

    获取按钮当前标题富文本

    NSAttributedString *aString = btn.currentAttributedTitle;
    

    指定背景边界

    - (CGRect)backgroundRectForBounds:(CGRect)bounds;
    

    指定内容边界

    - (CGRect)contentRectForBounds:(CGRect)bounds;
    

    指定标题边界

    - (CGRect)titleRectForContentRect:(CGRect)contentRect;
    

    指定图片边界

    - (CGRect)imageRectForContentRect:(CGRect)contentRect;
    

    给按钮添加点击事件

    [btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    

    UIButton点击事件如下

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


    作者:coder小鹏

     

  • 相关阅读:
    Windows下git使用代理服务器的设置方法
    SQL backup&restore
    css3 随记
    HTML5 上传图片预览
    jQuery.event.move
    css3 html5 手机设备 列表的弹回和加速移动
    16进制与utf-8
    android 使用现成做get请求
    android 往sd卡中写入文件
    android 遍历控件
  • 原文地址:https://www.cnblogs.com/jiuyi/p/10485365.html
Copyright © 2011-2022 走看看