iOS UIButton swift3.0 发表于 2016-11-06 | 分类于 Mobile | 评论数: | 阅读次数: iOS UIButton swift3.0 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172//通过frame来初始化UIButtonlet button = UIButton(frame: CGRect(x: 0, y: 300, width: 320, height: 60))//通过UIButtonType来初始化UIButton/*enum UIButtonType : Int {case Custom 自定义风格case System 系统风格case DetailDisclosure 蓝色小箭头按钮,主要做详细说明用case InfoLight 亮色感叹号case InfoDark 暗色感叹号case ContactAdd 十字加号按钮}*/var button2 = UIButton(type: UIButtonType.custom)//设置背景色,方便看到效果button.backgroundColor = UIColor.orange//添加到视图上self.view.addSubview(button)//---------------常用属性和方法//修改背景色button.backgroundColor = UIColor.orange//获取标题标签控件var aLabel = button.titleLabel//设置标题,需要注意,这里需要指定设置UIButton的哪个状态的title/*struct UIControlState : RawOptionSetType {init(_ value: UInt)static var Normal: UIControlState { get }static var Highlighted: UIControlState { get } // used when UIControl isHighlighted is setstatic var Disabled: UIControlState { get }static var Selected: UIControlState { get } // flag usable by app (see below)static var Application: UIControlState { get } // additional flags available for application usestatic var Reserved: UIControlState { get } // flags reserved for internal framework use}*/button.setTitle("", for: UIControlState())//设置标题颜色button.setTitleColor(UIColor.red, for: UIControlState())//正常状态标题颜色button.setTitleColor(UIColor.black, for: .highlighted)//鼠标按下标题颜色//设置标题阴影颜色button.setTitleShadowColor(UIColor.black, for: UIControlState())//设置图片,图片会遮挡butoon的标题// button.setImage(UIImage(named: "testImage"), forState: .Normal)//设置背景图片// button.setBackgroundImage(UIImage(named: "testImage"), forState: .Normal)//设置富文本let buttonAttribute:NSMutableAttributedString=NSMutableAttributedString(string: "swift大法好!")//文本0开始5个字符字体HelveticaNeue-Bold,16号buttonAttribute.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 20)!, range: NSMakeRange(0, 4))//设置字体颜色buttonAttribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(4, 2))//设置文字背景颜色buttonAttribute.addAttribute(NSBackgroundColorAttributeName, value: UIColor.green, range: NSMakeRange(0, 6))button.setAttributedTitle(buttonAttribute, for: UIControlState())//默认情况下,钮高亮的情况下图像的颜色会被画的深一些,如果下面的这个属性设置为false,那么可以去掉这个功能button.adjustsImageWhenHighlighted = false//默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置button.adjustsImageWhenDisabled = false//下面这个属性设置为yes的状态下,按钮按下会发光button.showsTouchWhenHighlighted = true//获取富文本var attributedTitle = button.attributedTitle(for: UIControlState())//获取标题var titleOfState = button.title(for: UIControlState())//获取标题颜色var titleColorOfState = button.titleColor(for: UIControlState())//获取标题阴影颜色var titleShadowColorOfState = button.titleShadowColor(for: UIControlState())//获取图片var imageOfState = button.image(for: UIControlState())//事件button.addTarget(self, action: #selector(ViewController.buttonUpInsideFunc), for: .touchUpInside) 代码仅供参考,希望可以帮到你,如果您有补充请直接私信希望能互相学习互相进步! GitHub下载地址