swift

1,按钮的创建

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

    contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
    detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
    system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
    custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
    infoDark:为感叹号“!”圆形按钮
    infoLight:为感叹号“!”圆形按钮
//创建一个ContactAdd类型的按钮
let button:UIButton = UIButton(type:.custom)
//设置按钮位置和大小
button.frame=CGRect(x:50,y:180,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,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

原文地址:https://www.cnblogs.com/hero11223/p/5690886.html