2021 swfit 重拾之路

swift - UIButton

2021-07-13  本文已影响0人  大也
let btn: UIButton = UIButton()//没有样式
//let btns:UIButton =UIButton(type: UIButtonType)//有样式
//let button = UIButton(frame:CGRect(x:10, y:150, width:100, height:30))//简化创建方式
self.view.addSubview(button)
public enum UIButtonType : Int {
    case custom // no button type
    @available(iOS 7.0, *)
    case system // standard system button
    case detailDisclosure
    case infoLight
    case infoDark
    case contactAdd
    public static var roundedRect: UIButtonType { get } // Deprecated, use UIButtonTypeSystem instead
}
//使用
let  btn: UIButton = UIButton(type: .Custom)
//显示文字
button1.setTitle("普通状态", for: .normal)
button1.setTitle("高粱状态", for: .highlighted)
button1.setTitle("禁用状态", for: .disabled)
//显示文字颜色
button1.setTitleColor(UIColor.red, for: .normal)
button1.setTitleColor(UIColor.blue, for: .highlighted)
button1.setTitleColor(UIColor.cyan, for: .selected)
button1.setTitleColor(UIColor.cyan, for: .disabled)
//阴影文字颜色设置
button1.setTitleShadowColor(UIColor.cyan, for: .normal)
button1.setTitleShadowColor(UIColor.green, for: .highlighted)
button1.setTitleShadowColor(UIColor.brown, for: .disabled)
button1.setTitleShadowColor(UIColor.darkGray, for: .selected)
//背景颜色
button2.backgroundColor = UIColor.orange
//背景图片    
button4.setBackgroundImage(UIImage(named:"XXX"), for: .normal)
//设置背景图片为圆角
buttonImage.imageView?.layer.cornerRadius = 50
//字体
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
//isEnabled
button.isEnabled = false
//圆角
button.layer.cornerRadius = 5
button.layer.masksToBounds = true
//边框宽度/颜色
button.layer.borderWidth = 2
button.layer.borderColor = UIColor.red.cgColor
//保留图标原来的颜色
let iconImage = UIImage(named:"icon2")?.withRenderingMode(.alwaysOriginal)
//图片
button.setImage(UIImage(named:"icon1"),forState:.Normal)  
 //使触摸模式下按钮也不会变暗(半透明)
button.adjustsImageWhenHighlighted=false
//使禁用模式下按钮也不会变暗(半透明)
button.adjustsImageWhenDisabled=false 
//imageEdgeInsets /titleEdgeInsets 
btn.imageEdgeInsets =UIEdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)
btn.titleEdgeInsets =UIEdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)
//点击
button1.addTarget(self,action:#selector(methodName), for: .touchUpInside)
//UIButton的图片文字布局 需要扩展
//地址
上一篇下一篇

猜你喜欢

热点阅读