UIButton+Extension.swift 922 B

12345678910111213141516171819202122232425262728293031
  1. //
  2. // UIButton+Extension.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/4/28.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. extension UIButton {
  11. /// 便利构造方法
  12. ///
  13. /// - Parameters:
  14. /// - title: 标题
  15. /// - fontSize: 字体大小
  16. /// - imageName: 显示图片名
  17. convenience init(title: String,fontSize: CGFloat = 14,normalColor: UIColor = UIColor.black, highlightedColor: UIColor = UIColor.darkGray, imageName: String? = nil) {
  18. self.init()
  19. self.setTitle(title, for: .normal)
  20. self.setTitleColor(normalColor, for: .normal)
  21. self.setTitleColor(highlightedColor, for: .highlighted)
  22. self.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
  23. if imageName != nil {
  24. self.setImage(UIImage(named: imageName!), for: .normal)
  25. }
  26. self.sizeToFit()
  27. }
  28. }