欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios开发UI篇--UIButton

程序员文章站 2022-05-27 22:12:31
概述 UIButton 是执行自定义代码以响应用户交互的控件。 UIButton 其实包含 UIImageView 和 UILabel 两个控件,UIButton 继承于 UIControl,所以有 addtarget 监听事件 属性和方法 初始化Button不用alloc init方法,使用便利构 ......

概述

  • 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;
ios开发UI篇--UIButton

 

设置图片内边距

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

 


作者: