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. 高并发秒杀系统--junit测试类与SpringIoc容器的整合

    1.原理是在Junit启动时加载SpringIoC容器 2.SpringIoC容器要根据Spring的配置文件加载 [示例代码] package org.azcode.dao; import org. ...

  2. Linux LVM 逻辑分区

    LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现.普通 ...

  3. [Ynoi2016]这是我自己的发明 莫队

    传送门:here 很棒的莫队题啊..... 题意: 有一棵$ n$个点的树,树上每个点有点权,有$ m$次询问: 操作1:给定两个点$ x,y$,求二元组$ (a,b)$的数量,要求$ a$在$ x$ ...

  4. java创建对象 的初始化顺序

    java创建对象 的初始化顺序 1.初始化块 初始化块通常写在类的构造方法之前,由花括号括起来,通常包含对成员属性进行初始化的语句: 初始化块分为instance初始化块和static初始化块,初始化 ...

  5. Python 9 进程,线程

    本节内容 python GIL全局解释器锁 线程 进程 Python GIL(Global Interpreter Lock) In CPython, the global interpreter l ...

  6. python web cgi

    知识详解: cgi:通用网关接口,网络脚本的解析 python cgi 自带有cgi轻量级服务器,我们通过cgi命令可以开启该服务器 python2 python -m CGIHTTPServer p ...

  7. Vue.js简单记录

    官网:https://cn.vuejs.org/ https://cn.vuejs.org/v2/api/#methods v-bind 缩写 <!-- 完整语法 --> <a v- ...

  8. nova 命令管理虚拟机

    nova命令管理虚拟机: $ nova list #查看虚拟机$ nova stop [vm-name]或[vm-id] #关闭虚拟机$ nova start [vm-name]或[vm-id] #启 ...

  9. Mysql事务与JDBC事务管理

    一.事务概述 1.什么是事务 一件事情有n个组成单元 要不这n个组成单元同时成功 要不n个单元就同时失败 就是将n个组成单元放到一个事务中 2.mysql的事务 默认的事务:一条sql语句就是一个事务 ...

  10. 深入理解JVM - 1 - Java内存区域划分

    作者:梦工厂链接:https://www.jianshu.com/p/7ebbe102c1ae来源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. Java与C++之间有一堵 ...