iOS swift之Button详解学习

{
    
    // 声明按钮是一个对象,是全局的
    var button1:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "六六六";
        
        button1 = UIButton.init(type: UIButtonType.custom)
//        button1 = UIButton(type:.custom)
//        button1 = UIButton.init(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
        button1 = UIButton(type:.custom)
        // 默认文字颜色为蓝色,带“!”图标按钮
//        button1 = UIButton(type: .detailDisclosure)
//        button1 = UIButton(type: .infoDark)
//        button1 = UIButton(type: .infoLight)
//        button1 = UIButton(type: .contactAdd)
        button1.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        button1.backgroundColor = UIColor.white
        button1.backgroundColor = UIColor.init(white: 0.9, alpha: 1)
        
        button1.setBackgroundImage(UIImage(named:"num_1"), for: UIControlState.normal) // 普通状态
        button1.setBackgroundImage(UIImage(named:"num_2"), for: UIControlState.highlighted) // 高亮状态
        button1.setBackgroundImage(UIImage(named:"num_3"), for: UIControlState.selected) // 选中状态
        
        // 设置按钮不同状态下的图片
        button1.setImage(UIImage(named:"num_2"), for: UIControlState.normal)
        button1.setTitle("普通", for: UIControlState.normal)
        button1.setImage(UIImage(named:"num_1"), for: UIControlState.highlighted)
        button1.setTitle("高亮", for: UIControlState.highlighted)
        button1.setImage(UIImage(named:"num_3"), for: UIControlState.selected)
        button1.setTitle("选择", for: UIControlState.selected)
        
        // 设置按钮上文字的阴影
        button1.setTitleColor(UIColor.purple, for: UIControlState.normal)
        button1.setTitleColor(UIColor.red, for: UIControlState.highlighted)
        button1.setTitleColor(UIColor.darkGray, for: UIControlState.selected)
        
        button1.setTitleShadowColor(UIColor.cyan, for: UIControlState.normal)
        button1.setTitleShadowColor(UIColor.yellow, for: UIControlState.highlighted)
        button1.setTitleShadowColor(UIColor.blue, for: UIControlState.selected)
        
        button1.addTarget(self, action: #selector(buttonClick(button:)), for: UIControlEvents.touchUpInside)
        
        // 按钮内容的边距(顶部、左边、底部、右边
        button1.contentEdgeInsets = UIEdgeInsetsMake(100, 0, 30, 0)
        
        // 按钮上图片的边距
        button1.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 20, 0)
        
        // 按钮上文本框的边距
        button1.titleEdgeInsets = UIEdgeInsetsMake(5, 0, 10, 0)
        
        // 按钮长按状态下去掉文字阴影
        button1.reversesTitleShadowWhenHighlighted = true
        
        // 高亮状态下调整图片
        button1.adjustsImageWhenHighlighted = true
        
        // 高亮状态下变灰
        button1.showsTouchWhenHighlighted = true
        
        // 设置按钮上的文字
        button1.titleLabel?.text = "逗比欢乐多";
        button1.imageView?.image = UIImage.init(named: "img111");
        
        //也可以按照这样来创建按钮
        self.view.addSubview(button1)
    }

    //按钮的点击事件的处理
    func buttonClick(button :UIButton){
        
        //反选效果
        button.isSelected = !button.isSelected;
        
        //获取按钮当前文字
        print(button.currentTitle)
        
        /*
         按钮当前的一些属性,注意:只能读取,不能修改
         
         currentTitle//当前文字
         currentTitleColor//当前文字颜色
         currentTitleShadowColor//当前文字阴影色
         
         currentImage//当前图片
         currentBackgroundImage//当前背景图片
         */
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
 
}

你可能感兴趣的:(iOS swift之Button详解学习)