Swift基础(十五)UIButton

来源:互联网 发布:淘宝客服的上班时间 编辑:程序博客网 时间:2024/06/06 09:08
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.whiteColor()
        // 通过frame来初始化UIButton
        var button = UIButton(frame: CGRectMake(0, 300, 200, 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.orangeColor()
        // 添加到视图上
        self.view.addSubview(button)
        
        // 按钮的常用属性和方法
        // 获取标签控件
        var aLabel = button.titleLabel
        // 设置标题,需要注意,这里需要指定设置UIButton的那个状态的title
        /*
         public struct UIControlState : OptionSetType {
         public init(rawValue: UInt)
         
         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
         }
         */
        button.setTitle("", forState: .Normal)
        
        // 设置标题颜色
        button.setTitleColor(UIColor.redColor(), forState: .Normal) // 正常状态的标题颜色
        button.setTitleColor(UIColor.blueColor(), forState: .Highlighted) // 点击按钮之后标题颜色
        // 设置标题阴影颜色
        button.setTitleShadowColor(UIColor.blackColor(), forState: .Normal)
        // 设置图片,图片会遮挡button的标题
        button.setImage(UIImage(named: "testImage"), forState: .Normal)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "testImage"), forState: .Normal)
        // 设置富文本
        var buttonAttribute: NSMutableAttributedString = NSMutableAttributedString(string: "1234567890")
        // 文本0开始5个字符字体HelveticaNeue,16号
        buttonAttribute.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue", size: 16)!, range: NSMakeRange(0, 4))
        // 设置字体颜色
        buttonAttribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(4, 2))
        // 设置文字背景颜色
        buttonAttribute.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSMakeRange(0, 4))
        button.setAttributedTitle(buttonAttribute, forState: .Normal)
        
        // 默认情况下,按钮高亮的情况下图像的颜色会被画得深一些,如果下面的这个属性设置为false,那么可以去掉这个动能
        button.adjustsImageWhenHighlighted = false
        // 默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置
        button.adjustsImageWhenDisabled = false
        // 下面设个属性设置为yes的状态下,按下按钮会发光
        button.showsTouchWhenHighlighted = true
        
        // 获取富文本
        var attributedTitle = button.attributedTitleForState(.Normal)
        // 获取标题
        var titleOfState = button.titleForState(.Normal)
        // 获取标题颜色
        var titleColorOfState = button.titleColorForState(.Normal)
        // 获取标题阴影颜色
        var titleShadowColorOfState = button.titleShadowColorForState(.Normal)
        // 获取图片
        var imageOfState = button.imageForState(.Normal)
        // 按下按钮之后,按钮禁止变暗
        button.adjustsImageWhenHighlighted = false
        // 按钮禁用状态时禁止变暗
        button.adjustsImageWhenDisabled = false
        
        // 按钮事件
        button.addTarget(self, action: Selector("buttonAction"), forControlEvents: .TouchUpInside) // 按钮事件基本上是使用这个,其他的也有,用“command + 左键”进入,自己去试,大概有20种事件类型
    }
    
    func buttonAction() {
        print("123456789")
    }
0 0
原创粉丝点击