UI--UIButton

前言:UI控件整理之UIButton

一、显示图片(复选框)


显示图片

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 15, 60,  30, 30);

[button setImage:[UIImage imageNamed:@"icon_weixuanzhong-1"] forState:UIControlStateNormal];

[button setImage:[UIImage imageNamed:@"icon_xuanzhong-1"] forState:UIControlStateSelected];

[button setBackgroundColor:[UIColor whiteColor]];

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

[self.view addSubview:button];

二、显示文字


显示文字

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(30, 60,  [UIScreen mainScreen].bounds.size.width - 60, 30);

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

[button setTitle:@"UIButton" forState:UIControlStateNormal];

button.titleLabel.font = [UIFont fontWithName:@"IowanOldStyle-Bold" size:15];

[button setBackgroundColor:[UIColor whiteColor]];

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

[self.view addSubview:button];

三、部分常用属性解析

1、初始化Button(枚举值)

//初始化Button 不用alloca init 的方法 用便利构造器初始化

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

UIButtonTypeCustom // -自定义风格

UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),

UIButtonTypeDetailDisclosure //蓝色小箭头按钮,主要做详细说明

UIButtonTypeInfoLight //-亮色感叹号

UIButtonTypeInfoDark //-暗色感叹号

UIButtonTypeContactAdd // -十字加号按钮

UIButtonTypeRoundedRect = UIButtonTypeSystem //-圆角矩形

2、//设置button的背景图片

[button setBackgroundImage:[UIImage imageNamed:@"bg.png"] forState:UIControlStateNormal];

3、//设置前景图片 前景图片必须是镂空图,或者是线条勾勒的图片

[button setImage:[UIImage imageNamed:@"qj.png"] forState:UIControlStateNormal];

4、状态(forState)

/* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/

UIControlStateNormal = 0 // 常规状态显现

UIControlStateHighlighted = 1 << 0 // 高亮状态显现

UIControlStateDisabled = 1 << 1 // 禁用的状态才会显现

UIControlStateSelected = 1 << 2 // 选中状态

UIControlStateApplication = 0x00FF0000 //当应用程序标志时

UIControlStateReserved = 0xFF000000 //为内部框架预留,可以不管他

5、获取按钮当前标题

@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;

6、获取按钮当前按钮内图像

@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;

7、按钮点击事件(枚举值)

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            // 通知所有事件

8、标题内距离

@property(nonatomic) UIEdgeInsets titleEdgeInsets;

9、图片内边距

@property(nonatomic) UIEdgeInsets imageEdgeInsets;

四、常用拓展(系统属性排列)

@property(nonatomic) UIEdgeInsets contentEdgeInsets; 内容内距离

@property(nonatomic)  UIEdgeInsets titleEdgeInsets;  标题内距离

@property(nonatomic)  BOOL  reversesTitleShadowWhenHighlighted; 标题的阴影改变时,按钮是否高亮显示。默认为NO

@property(nonatomic)  UIEdgeInsets imageEdgeInsets;  图片内边距

@property(nonatomic)  BOOL adjustsImageWhenHighlighted;按钮高亮的情况下,图像的颜色是否要加深一点。默认是YES

@property(nonatomic)  BOOL adjustsImageWhenDisabled; 按钮禁用的情况下,图像的颜色是否要加深一点。默认是YES

@property(nonatomic)  BOOL showsTouchWhenHighlighted; 按下按钮是否会发光 默认是NO

@property(nonatomic,readonly) UIButtonType buttonType; button的类型

指定背景边界

- (CGRect)backgroundRectForBounds:(CGRect)bounds;

指定内容边界

- (CGRect)contentRectForBounds:(CGRect)bounds;

指定标题边界

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

指定图片边界

- (CGRect)imageRectForContentRect:(CGRect)contentRect;

示例:

- (CGRect)imageRectForContentRect:(CGRect)bounds{

return CGRectMake(0.0, 0.0, 44, 44);

}

获取按钮当前标题

@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;

获取按钮当前标题颜色

@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;

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

@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor;

获取按钮当前按钮内图像

@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;

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

@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;

获取按钮当前标题富文本

@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle

@property(nullable, nonatomic,readonly,strong) UILabel    *titleLabel NS_AVAILABLE_IOS(3_0);

@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);

设置button某个状态的标题

- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;

例: [button setTitle:@"开灯" forState:UIControlStateNormal];

设置button某个状态的标题颜色

- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state

例:[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

设置button某个状态阴影的标题颜色

- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state

例:[button setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];

设置button某个状态图片

- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;

设置button 某个状态背景图片

- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil

设置button 某个状态下的富文本标题

- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line

返回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);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 字数1443 阅读548 评论5 喜欢36UIButton 的全面解析建议收藏,用到的时候来这里一查就都明白了 初...
    xiao小马哥阅读 1,189评论 0 1
  • 一个UIButton的实例变量, 使一个按钮(button)在触摸屏上生效。一个按钮监听触摸事件,当被点击时,给目...
    wushuputi阅读 1,470评论 0 1
  • 概述 UIButton的父类是UIControl,UIControl的父类是UIView,UIView的父类是UI...
    guaker阅读 2,765评论 1 9
  • 一、UITabBarController以其相关控件之间的关系 @interface UITabBarContro...
    西门淋雨阅读 3,039评论 0 1
  • 需要改进的地方: 第一: 跟叔叔谈话时 要显现出自己的专业性,确定要的就一定要用,不能模棱两可,这样自己的专业性就...
    Janey袁敏阅读 134评论 0 0