首页 技术 正文
技术 2022年11月16日
0 收藏 513 点赞 3,143 浏览 2314 个字

1,按钮的创建

(1)按钮有下面四种类型:

    contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
infoDark:为感叹号“!”圆形按钮
infoLight:为感叹号“!”圆形按钮
//创建一个ContactAdd类型的按钮
let button:UIButton = UIButton(type:.custom)
//设置按钮位置和大小
button.frame=CGRect(x:50,y:180,width:self.view.bounds.size.width - 100,height:50)
//设置按钮文字
button.setTitle("按钮", for: .normal)
self.view.addSubview(button);

(2)对于Custom定制类型按钮,代码可简化为:

let btn = CGRect(x:100,y:200,width:80,height:50)

2,按钮的文字设置

button.setTitle("普通状态", for:.normal) //普通状态下的文字
button.setTitle("触摸状态", for:.highlighted) //触摸状态下的文字
button.setTitle("禁用状态", for:.disabled) //禁用状态下的文字

3,按钮文字颜色的设置

button.setTitleColor(UIColor.black,for: .normal) //普通状态下文字的颜色
button.setTitleColor(UIColor.green,for: .highlighted) //触摸状态下文字的颜色
button.setTitleColor(UIColor.gray,for: .disabled) //禁用状态下文字的颜色

4,按钮文字阴影颜色的设置

button.setTitleShadowColor(UIColor.green,for:.normal) //普通状态下文字阴影的颜色
button.setTitleShadowColor(UIColor.yellow,for:.highlighted) //普通状态下文字阴影的颜色
button.setTitleShadowColor(UIColor.gray,for:.disabled) //普通状态下文字阴影的颜色

5,按钮背景颜色设置

button.backgroundColor=UIColor.black

6,按钮文字图标的设置

button.setImage(UIImage(named:"icon1"),for:.normal)  //设置图标
button.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗
button.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗

7,设置按钮背景图片

button.setBackgroundImage(UIImage(named:"background1"),for:.normal)

8,按钮触摸点击事件响应

//不传递触摸对象(即点击的按钮)
button.addTarget(self,action:#selector(tapped),for:.touchUpInside)
func tapped(){
print("tapped")
}//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
button.addTarget(self,action:#selector(tapped(_button:)),for:.touchUpInside)
func tapped(_button:UIButton){}
常用的触摸事件类型:
touchDown:单点触摸按下事件,点触屏幕
touchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
touchDragInside:触摸在控件内拖动时
touchDragOutside:触摸在控件外拖动时
touchDragEnter:触摸从控件之外拖动到内部时
touchDragExit:触摸从控件内部拖动到外部时
touchUpInside:在控件之内触摸并抬起事件
touchUpOutside:在控件之外触摸抬起事件
touchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断

9.我们通过修改button按钮中的titleLabel的lineBreakMode属性,遍可以调整按钮在文字超长的情况下如何显示,以及是否换行

    case byWordWrapping // Wrap at word boundaries, default 自动换行,按词拆分    case byCharWrapping // Wrap at character boundaries 自动换行,按字符拆分    case byClipping // Simply clip 直接将多余的部分截断    case byTruncatingHead // Truncate at head of line: "...wxyz"    case byTruncatingTail // Truncate at tail of line: "abcd..."    case byTruncatingMiddle // Truncate middle of line:  "ab...yz"

换行符:/n

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,911
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731