IBButton.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // IBButton.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/3/17.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. @IBDesignable
  10. class IBButton: UIButton {
  11. /// 集成IBView 参数
  12. @IBInspectable var 圆角偏移量:CGFloat = 0.0
  13. @IBInspectable var 圆角倍数:CGPoint = CGPoint.init(x: 0, y: 0)
  14. @IBInspectable var maskToBounds:Bool = false
  15. @IBInspectable var borderColor:UIColor = UIColor.clear {
  16. willSet {
  17. layer.borderColor = newValue.cgColor
  18. }
  19. }
  20. @IBInspectable var borderWidth:CGFloat = 0.0
  21. /// IBButton参数
  22. @IBInspectable var 选中时描边颜色:UIColor = UIColor.clear {
  23. willSet {
  24. layer.borderColor = newValue.cgColor
  25. }
  26. }
  27. @IBInspectable var 高亮时描边颜色:UIColor = UIColor.clear
  28. @IBInspectable var 默认背景色:UIColor = UIColor.clear {
  29. willSet {
  30. backgroundColor = newValue
  31. }
  32. }
  33. @IBInspectable var 高亮背景色:UIColor = UIColor.clear
  34. @IBInspectable var 选中背景色:UIColor = UIColor.clear
  35. @IBInspectable var 选中时字体加粗:Bool = false
  36. override func layoutSubviews() {
  37. super.layoutSubviews()
  38. IBView.refreshIBEffect(view: self)
  39. self.refreshIBEffect()
  40. }
  41. override var isSelected: Bool {
  42. set{
  43. super.isSelected = newValue
  44. self.refreshIBEffect()
  45. }
  46. get{
  47. return super.isSelected
  48. }
  49. }
  50. override var isHighlighted: Bool {
  51. set{
  52. super.isHighlighted = newValue
  53. self.refreshIBEffect()
  54. }
  55. get{
  56. return super.isHighlighted
  57. }
  58. }
  59. func refreshIBEffect() {
  60. /// 背景颜色
  61. let dict = [UIControl.State.normal.rawValue : "默认背景色",
  62. UIControl.State.highlighted.rawValue : "高亮背景色",
  63. UIControl.State.selected.rawValue : "选中背景色"]
  64. let key = dict[self.state.rawValue]
  65. var color:UIColor? = nil
  66. if key != nil {
  67. switch key! {
  68. case "默认背景色":
  69. color = 默认背景色
  70. case "高亮背景色":
  71. color = 高亮背景色
  72. case "选中背景色":
  73. color = 选中背景色
  74. default:
  75. break
  76. }
  77. if color != nil{
  78. self.backgroundColor = color
  79. }
  80. }
  81. /// 选中时字体加粗
  82. if 选中时字体加粗{
  83. let font = self.titleLabel?.font
  84. if (self.state == UIControl.State.selected){
  85. let descriptor = font?.fontDescriptor.withSymbolicTraits(.traitBold)
  86. self.titleLabel?.font = UIFont(descriptor:descriptor!, size: (font?.pointSize)!)
  87. }else{
  88. let descriptor = font?.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(rawValue: 0))
  89. self.titleLabel?.font = UIFont(descriptor:descriptor!, size: (font?.pointSize)!)
  90. }
  91. }
  92. /// 边框颜色
  93. if (self.state == UIControl.State.selected){
  94. self.layer.borderColor = self.选中时描边颜色.cgColor
  95. } else if (self.state == UIControl.State.highlighted){
  96. self.layer.borderColor = self.高亮时描边颜色.cgColor
  97. } else {
  98. self.layer.borderColor = self.borderColor.cgColor
  99. }
  100. }
  101. }