美文网首页视图控件
Swift - 学习 按钮(UIButton)的用法

Swift - 学习 按钮(UIButton)的用法

作者: 麦穗0615 | 来源:发表于2019-06-16 00:07 被阅读9次

前言

开发中,按钮必不可少,学习Swift按钮的基础知识,也绝对要了解。本篇文章,目的在于学习、留有资料,方便查看。

目录:

1.按钮的创建
2. 按钮的文字设置
3. 按钮文字颜色的设置
4. 按钮文字阴影颜色的设置
5. 按钮文字的字体和大小设置
6. 按钮背景颜色设置
7. 按钮文字图标的设置
8. 设置按钮背景图片
9. 按钮触摸点击事件响应
10. 按钮文字太长时的处理方法

1.按钮的创建

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

- UIButtonType.system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果   

- UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果

-  UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果

- UIButtonType.infoDark:为感叹号“!”圆形按钮

- UIButtonType.infoLight:为感叹号“!”圆形按钮

(注意:自ios7起,infoDark、infoLight、detailDisclosure效果都是一样的)

图例

图例

图例代码:

        // UIButtonType.system
         let btn1 = UIButton(type: .system)
         btn1.frame = CGRect(x: 35, y: 100, width: 250, height: 40)
         btn1.setTitle("UIButtonType.system", for: .normal)
         btn1.backgroundColor = UIColor.orange
         self.view.addSubview(btn1)
         
         // UIButtonType.custom
         let btn2 = UIButton(type: .custom)
         btn2.frame = CGRect(x: 35, y: 150, width: 250, height: 40)
         btn2.setTitle("UIButtonType.custom", for: .normal)
         btn2.backgroundColor = UIColor.cyan
         self.view.addSubview(btn2)
         
         // UIButtonType.contactAdd
         let btn3 = UIButton(type: .contactAdd)
         btn3.frame = CGRect(x: 35, y: 200, width: 250, height: 40)
         btn3.setTitle("UIButtonType.contactAdd", for: .normal)
         btn3.backgroundColor = UIColor.magenta
         self.view.addSubview(btn3)
         
         // UIButtonType.detailDisclosure
         let btn4 = UIButton(type: .detailDisclosure)
         btn4.frame = CGRect(x: 35, y: 250, width: 250, height: 40)
         btn4.setTitle("UIButtonType.detailDisclosure", for: .normal)
         btn4.backgroundColor = UIColor.green
         self.view.addSubview(btn4)
        
         // UIButtonType.infoDark
        let btn5 = UIButton(type: .infoDark)
         btn5.frame = CGRect(x: 35, y: 300, width: 250, height: 40)
        btn5.setTitle("UIButtonType.infoDark", for: .normal)
         btn5.backgroundColor = UIColor.yellow
         self.view.addSubview(btn5)
         
         // UIButtonType.infoLight
         let btn6 = UIButton(type: .infoLight)
         btn6.frame = CGRect(x: 35, y: 350, width: 250, height: 40)
         btn6.setTitle("UIButtonType.infoLight", for: .normal)
         btn6.backgroundColor = UIColor.brown
         self.view.addSubview(btn6)

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

let button = UIButton(frame:CGRect(x:10, y:150, width:100, height:30))

2. 按钮的文字设置

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

3. 按钮文字颜色的设置

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

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

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

5. 按钮文字的字体和大小设置

 button.titleLabel?.font = UIFont.systemFont(ofSize: 11)

6. 按钮背景颜色设置

 button.backgroundColor = UIColor.black

7. 按钮文字图标的设置

(1)默认情况下按钮会被渲染成单一颜色

let btn7 = UIButton(type:.system)
btn7.frame = CGRect(x: 35, y: 400, width: 250, height: 40)
btn7.setTitle("   按钮文字", for: .normal)
btn7.setImage(UIImage(named: "xunbao_icon"), for: .normal)
btn7.backgroundColor = UIColor.cyan
btn7.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗(半透明)
btn7.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗(半透明)
self.view.addSubview(btn7)

(2)也可以设置成保留图标原来的颜色

 let iconImage = UIImage(named: "xunbao_iconv")?.withRenderingMode(.alwaysOriginal)
 btn7.setImage(iconImage, for: .normal)

8. 设置按钮背景图片

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

9. 按钮触摸点击事件响应

 //不传递触摸对象(即点击的按钮)
 button.addTarget(self, action:#selector(tapped), for:.touchUpInside)
 @objc func tapped(){
      print("tapped")
 }

 //传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
 button.addTarget(self, action:#selector(tapped(_:)), for:.touchUpInside)
  
 @objc func tapped(_ button:UIButton){
      print(button.title(for: .normal))
 }

常用的触摸事件类型:

  touchDown:单点触摸按下事件,点触屏幕

  touchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候

  touchDragInside:触摸在控件内拖动时

  touchDragOutside:触摸在控件外拖动时

  touchDragEnter:触摸从控件之外拖动到内部时

  touchDragExit:触摸从控件内部拖动到外部时

  touchUpInside:在控件之内触摸并抬起事件

  touchUpOutside:在控件之外触摸抬起事件

  touchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断

10. 按钮文字太长时的处理方法

默认情况下,如果按钮文字太长超过按钮尺寸,则会省略中间的文字。比如下面代码:

 let button = UIButton(frame:CGRect(x:20, y:50, width:130, height:50))
 button.setTitle("这个是一段 very 长的文字", for:.normal) //普通状态下的文字
 button.setTitleColor(UIColor.white, for: .normal) //普通状态下文字的颜色
 button.backgroundColor = UIColor.orange
 self.view.addSubview(button)

运行效果如下,中间文字使用 ... 代替


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

   //省略尾部文字
   button.titleLabel?.lineBreakMode = .byTruncatingTail

lineBreakMode 共支持如下几种样式:

  • .byTruncatingHead:省略头部文字,省略部分用...代替
  • .byTruncatingMiddle:省略中间部分文字,省略部分用...代替(默认)
  • .byTruncatingTail:省略尾部文字,省略部分用...代替
  • .byClipping:直接将多余的部分截断
  • .byWordWrapping:自动换行(按词拆分)
  • .byCharWrapping:自动换行(按字符拆分)
image.png

注意:当设置自动换行后(byWordWrapping 或 byCharWrapping),我们可以在设置 title 时通过 \n 进行手动换行。

原文学习地址:Swift - 按钮(UIButton)的用法

相关文章

网友评论

    本文标题:Swift - 学习 按钮(UIButton)的用法

    本文链接:https://www.haomeiwen.com/subject/kavifctx.html