前端之家收集整理的这篇文章主要介绍了
swift--button的简单实用,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
func addButton(rect:(CGRect)) -> (UIButton) {
//两种方式初始化button
// var myButton = UIButton.init(frame: rect)
var myButton = UIButton.init(type: .DetailDisclosure)
// case Custom //自定义
// @available(iOS 7.0,*)
// case System // standard system button //系统
//
// case DetailDisclosure //蓝色的小箭头
// case InfoLight //亮色感叹号
// case InfoDark //暗色感叹号
// case ContactAdd //十字加好
myButton.frame = rect //设置frame
myButton.setTitle("button",forState: .Normal) //设置文本,需要注意要指定状态。如点击时出现的文本和正常状态下的显示文本
// public static var Normal: UIControlState { get }
// public static var Highlighted: UIControlState { get } // used when UIControl isHighlighted is set
// public static var Disabled: UIControlState { get }
// public static var Selected: UIControlState { get } // flag usable by app (see below)
// @available(iOS 9.0,*)
// public static var Focused: UIControlState { get } // Applicable only when the screen supports focus
// public static var Application: UIControlState { get } // additional flags available for application use
// public static var Reserved: UIControlState { get } // flags reserved for internal framework use
myButton.addTarget(self,action: "buttonAction",forControlEvents:.TouchUpInside )
myButton.backgroundColor = UIColor.orangeColor() //设置背景颜色
myButton.setTitleColor(UIColor.blackColor(),forState: .Normal) //设置字体颜色,同样需要指定状态
// myButton.setImage(<#T##image: UIImage?##UIImage?#>,forState: <#T##UIControlState#>) //设置图片,图片会遮挡文字
// myButton.setBackgroundImage(<#T##image: UIImage?##UIImage?#>,forState: <#T##UIControlState#>) //设置背景图片
//可以设置富文本即button上的标题文字可以单独的指定字体,大小等此过程需要设置富文本属性
var buttonTextAttr:NSMutableAttributedString = NSMutableAttributedString(string: "hello,nihao")
buttonTextAttr.addAttribute(NSFontAttributeName,value: UIFont(name: "HelveticaNeue-Bold",size: 10)!,range: NSMakeRange(0,2)) //设置文字字体
buttonTextAttr.addAttribute(NSForegroundColorAttributeName,value: UIColor.redColor(),4)) //设置文字字体颜色
buttonTextAttr.addAttribute(NSBackgroundColorAttributeName,value: UIColor.clearColor(),6)) //设置文字背景颜色
myButton.setAttributedTitle(buttonTextAttr,forState: .Normal)
myButton.adjustsImageWhenDisabled = false //当按钮禁用时颜色会变为深色,设置此属性就不会变为深色
myButton.showsTouchWhenHighlighted = true //将此属性设置为true点击按钮时会发光
myButton.adjustsImageWhenHighlighted = false //高亮状态下按钮图像颜色也会变深,可通过此属性设置
myButton.setTitleShadowColor(UIColor.blackColor(),forState: .Normal) //设置阴影
return myButton
}
func buttonAction() {
print("nihao")
}
// 点击事件触发情况有很多
// public static var TouchDown: UIControlEvents { get } // 单击触发
// public static var TouchDownRepeat: UIControlEvents { get } // 多点触摸,点击次数大于1时
// public static var TouchDragInside: UIControlEvents { get }//触摸在控件内拖动
// public static var TouchDragOutside: UIControlEvents { get } //触摸在空间外运动
// public static var TouchDragEnter: UIControlEvents { get } //触摸从控件外移动到空间内
// public static var TouchDragExit: UIControlEvents { get } //触摸从控件内移动到控件外
// public static var TouchUpInside: UIControlEvents { get } //在控件内触摸抬起手指的时候
// public static var TouchUpOutside: UIControlEvents { get } //在控件外触摸抬起手指时
// public static var TouchCancel: UIControlEvents { get } //触摸取消时间,即一次触摸因为放了太多手指而被取消,或接电话打断
//
// public static var ValueChanged: UIControlEvents { get } // 当控件的值发生改变
// @available(iOS 9.0,*)
// public static var PrimaryActionTriggered: UIControlEvents { get } // semantic action: for buttons,etc.
//
// public static var EditingDidBegin: UIControlEvents { get } // UITextField 文本开始编辑
// public static var EditingChanged: UIControlEvents { get } //文本改变
// public static var EditingDidEnd: UIControlEvents { get } //结束编辑
// public static var EditingDidEndOnExit: UIControlEvents { get } // 'return key' ending editing 按下回车键时
//
// public static var AllTouchEvents: UIControlEvents { get } // for touch events 通知所有触摸事件
// public static var AllEditingEvents: UIControlEvents { get } // for UITextField 通知所有编辑事件
// public static var ApplicationReserved: UIControlEvents { get } // range available for application use
// public static var SystemReserved: UIControlEvents { get } // range reserved for internal framework use
// public static var AllEvents: UIControlEvents { get }
原文链接:https://www.f2er.com/swift/323440.html