| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // IBButton.swift
- // MTP2_iOS
- //
- // Created by zhongyuan on 2018/3/17.
- // Copyright © 2018年 zhongyuan.All rights reserved.
- //
- import UIKit
- @IBDesignable
- class IBButton: UIButton {
- /// 集成IBView 参数
- @IBInspectable var 圆角偏移量:CGFloat = 0.0
- @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0)
- @IBInspectable var maskToBounds:Bool = false
- @IBInspectable var borderColor:UIColor = UIColor.clear {
- willSet {
- layer.borderColor = newValue.cgColor
- }
- }
- @IBInspectable var borderWidth:CGFloat = 0.0
- /// IBButton参数
- @IBInspectable var 选中时描边颜色:UIColor = UIColor.clear {
- willSet {
- layer.borderColor = newValue.cgColor
- }
- }
- @IBInspectable var 高亮时描边颜色:UIColor = UIColor.clear
- @IBInspectable var 默认背景色:UIColor = UIColor.clear {
- willSet {
- backgroundColor = newValue
- }
- }
- @IBInspectable var 高亮背景色:UIColor = UIColor.clear
- @IBInspectable var 选中背景色:UIColor = UIColor.clear
- @IBInspectable var 选中时字体加粗:Bool = false
- override func layoutSubviews() {
- super.layoutSubviews()
- IBView.refreshIBEffect(view: self)
- self.refreshIBEffect()
- }
-
- override var isSelected: Bool {
- set{
- super.isSelected = newValue
- self.refreshIBEffect()
- }
- get{
- return super.isSelected
- }
- }
- override var isHighlighted: Bool {
- set{
- super.isHighlighted = newValue
- self.refreshIBEffect()
- }
- get{
- return super.isHighlighted
- }
- }
-
- func refreshIBEffect() {
- /// 背景颜色
- let dict = [UIControl.State.normal.rawValue : "默认背景色",
- UIControl.State.highlighted.rawValue : "高亮背景色",
- UIControl.State.selected.rawValue : "选中背景色"]
- let key = dict[self.state.rawValue]
- var color:UIColor? = nil
- if key != nil {
- switch key! {
- case "默认背景色":
- color = 默认背景色
- case "高亮背景色":
- color = 高亮背景色
- case "选中背景色":
- color = 选中背景色
- default:
- break
- }
- if color != nil{
- self.backgroundColor = color
- }
- }
-
- /// 选中时字体加粗
- if 选中时字体加粗{
- let font = self.titleLabel?.font
- if (self.state == UIControl.State.selected){
- let descriptor = font?.fontDescriptor.withSymbolicTraits(.traitBold)
- self.titleLabel?.font = UIFont(descriptor:descriptor!, size: (font?.pointSize)!)
- }else{
- let descriptor = font?.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(rawValue: 0))
- self.titleLabel?.font = UIFont(descriptor:descriptor!, size: (font?.pointSize)!)
- }
- }
- /// 边框颜色
- if (self.state == UIControl.State.selected){
- self.layer.borderColor = self.选中时描边颜色.cgColor
- } else if (self.state == UIControl.State.highlighted){
- self.layer.borderColor = self.高亮时描边颜色.cgColor
- } else {
- self.layer.borderColor = self.borderColor.cgColor
- }
- }
- }
|