import UIKit

class ViewController: UIViewController {

// 按钮的创建

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

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

// UIButtonType.contactAdd: 前面带 + 图标按钮,默认文字蓝色,无触摸高亮

// UIButtonType.detailDisclosure: 前面带 ! 图标, 默认文字蓝色, 有触摸高亮

// UIButtonType.infoDark: 同上

// UIButtonType.infoLight: 同上

override func viewDidLoad() {

super.viewDidLoad()

let button:UIButton = UIButton(type: .custom)

button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)

button.setTitle("按钮", for: .normal)

self.view.addSubview(button)

button.backgroundColor = UIColor.red

// 添加点击事件

button.addTarget(self, action: #selector(tapped), for: .touchUpInside)

button.addTarget(self, action: #selector(touchBtn(sender:)), for: .touchUpInside)

// button 文字太长 设置 titleLabel 的 lineBreakMode 属性 调整

button.titleLabel?.lineBreakMode = .byClipping

// lineBreakMode 共支持如下几种样式

// .byTruncatingHead: 省略头部文字, 省略部分用 ... 代替

// .byTruncatingMiddle: 省略中间部分文字

// .byTruncatingTail: 省略尾部文字

// .byClipping: 直接将多余的部分截断

// .byWordWrapping: 自动换行 (按词拆分)

// .byCharWrapping: 自动换行 (按字符拆分)

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

}

func tapped() {

print("测试")

}

func touchBtn(sender: UIButton) {

if let text = sender.titleLabel?.text {

print(text)

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

swift 实践- 04 -- UIButton的更多相关文章

  1. 使用Bootstrap 3开发响应式网站实践04,使用Panels展示内容

    在Bootstrap页面中,通常用Panels来展示主要功能的内容.该部分Html为: <div class="row" id="featureHeading&qu ...

  2. iOS -Swift 3.0 -UIButton属性大全

    // //  ViewController.swift //  Swift-UIButton // //  Created by luorende on 16/9/9. //  Copyright © ...

  3. Swift基础之UIButton

    //设置全局变量,将下面的替换即可    //var myButton = UIButton();    //系统生成的viewDidLoad()方法    override func viewDid ...

  4. swift学习之UIButton

    // //  ViewController.swift //  button // //  Created by su on 15/12/7. //  Copyright © 2015年 tian. ...

  5. Swift - 按钮(UIButton)的用法

    1,按钮的创建 (1)按钮有下面四种类型: UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果 UIButtonType.DetailDisc ...

  6. swift 实践- 12 -- UIPickerView

    import UIKit class ViewController: UIViewController , UIPickerViewDelegate,UIPickerViewDataSource{ v ...

  7. swift 实践- 08 -- UISegmentedControl

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  8. Swift 学习- 04 -- 字符串和字符

    // 字符串 和 字符 // 字符串 是有序的 Character (字符) 类型的值的集合,  通过 String 类型的集合 // swift 的 String 和 Character 类型提供了 ...

  9. Swift 实践之UIWebView

    1.选中工程,点击右键,New File>在iOS下选中Othe>Empty,生成一个.js的脚本文件,将代码粘贴过去保存; var script = document.createEle ...

随机推荐

  1. Coursera Deep Learning 3 Structuring Machine Learning Projects, ML Strategy

    Why ML stategy 怎么提高预测准确度?有了stategy就知道从哪些地方入手,而不至于找错方向做无用功. Satisficing and Optimizing metric 上图中,run ...

  2. luogu P4099 [HEOI2013]SAO

    传送门 吐槽题目标题 这个依赖关系是个树,可以考虑树型dp,设f_i表示子树i的答案 因为这是个序列问题,是要考虑某个数的位置的,所以设\(f_{i,j}\)表示子树i构成的序列,i在第j个位置的方案 ...

  3. JSON格式说明

    JSON的优点 相比XML拥有更简单的格式. 不同WEB浏览器处理的结果一样. 纯文本数据交换格式. JSON格式特点 {} 对象定义域 key:value 定义属性 key 字符串格式,value ...

  4. ionic 照相机 Camera

    1.官网: https://ionicframework.com/docs/native/camera/#DestinationType 2.引入插件 $ ionic cordova plugin a ...

  5. proxysql 系列 ~ 读写分离核心功能

    一 相关表介绍 1 mysql_user 列表配置    1 username && password 账号密码    2 default_hostgroup 默认的组ID2 mysq ...

  6. Challenge Create a Launch Pad

    在头文件中定义网格体组件和重叠组件 UPROPERTY(VisibleAnywhere,Category="Components") UStaticMeshComponent* M ...

  7. php编程 之 php进阶练习

    1,php的date相关操作: PHP date() 函数可把时间戳格式化为可读性更好的日期和时间. <?php echo date("Y/m/d") . "< ...

  8. 2017-2018-2 20165325 实验四《Android程序设计》实验报告

    一.Android程序设计-1 1.检查点要求 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android ...

  9. Python 面向对象【1】

    对象 = 属性 + 方法   面向对象特征:分装 继承 多态[不同对象对同一方法响应不同行动]   类定义 class xxx: .... ....   类对象 类对象支持两种操作:属性引用和实例化 ...

  10. 【运维】浪潮服务器一块硬盘显示红色Offline(或者Failed)解决办法

    [写在前面]           最近服务器不知道为什么总是出现故障,以前戴尔服务器硬盘出现故障,也就是说硬盘旁边的灯显示为红色的时候,一般情况下都是表示硬盘坏掉了,直接买一块新的硬盘,将坏掉的硬盘拿 ...