Swift 换皮肤工具类

10 篇文章 0 订阅

该工具类可设置 UIView 、UIButton、UITableViewCell 的皮肤色。

一、首先,定义 Skin :

import Foundation
import UIKit

struct Skin {
  let backgroundColor: UIColor
  let controlBackground: UIColor?
  let controlBorder: UIColor?
  let controlTextColor: UIColor?
  let tableCellTextColor: UIColor?
  let stepperColor: UIColor?

  // 几种不同类型的皮肤
  static let login = Skin(
    backgroundColor: UIColor(named: "canary")!,
    controlBackground: UIColor(named: "pink"),
    controlBorder: UIColor(named: "controlBorderGray"),
    controlTextColor: UIColor(named: "yellow"),
    tableCellTextColor: nil,
    stepperColor: nil)
  static let loginAlert = Skin(
    backgroundColor: UIColor(named: "pink")!,
    controlBackground: nil,
    controlBorder: nil,
    controlTextColor: UIColor(named: "yellow"),
    tableCellTextColor: nil,
    stepperColor: nil)
  static let announcements = Skin(
    backgroundColor: UIColor(named: "purple")!,
    controlBackground: nil,
    controlBorder: nil,
    controlTextColor: .darkText,
    tableCellTextColor: .bizLightGray,
    stepperColor: nil)
  static let purchaseOrder = Skin(
    backgroundColor: .bizCanary,
    controlBackground: nil,
    controlBorder: nil,
    controlTextColor: .darkText,
    tableCellTextColor: .darkGray,
    stepperColor: .bizPurple)
  static let orgChart = Skin(
    backgroundColor: .bizYellow,
    controlBackground: nil,
    controlBorder: nil,
    controlTextColor: .darkText,
    tableCellTextColor: .darkText,
    stepperColor: nil)
}

二、Styler 工具类

import UIKit

class Styler {
  static let shared = Styler()

  let configuration = AppDelegate.configuration!

  func style(
    background: UIView? = nil,
    buttons: [UIButton]? = nil,
    with skin: Skin
  ) {
    background.flatMap { style(background: $0, skin: skin) }
    buttons?.forEach { style(button: $0, skin: skin) }
  }

  func style(background: UIView, skin: Skin) {
    background.backgroundColor = skin.backgroundColor
  }

  func style(button: UIButton, skin: Skin) {
    if let borderColor = skin.controlBorder {
      // cornerRadius borderWidth 根据需要自己调整
      button.layer.cornerRadius = CGFloat(configuration.ui.button.cornerRadius)
      button.layer.borderWidth = CGFloat(configuration.ui.button.borderWidth)
      button.layer.borderColor = borderColor.cgColor
    }
    button.backgroundColor = skin.controlBackground
    button.setTitleColor(skin.controlTextColor, for: .normal)
  }

  func style(cell: UITableViewCell, with skin: Skin) {
    cell.backgroundColor = skin.backgroundColor
    for view in cell.contentView.subviews {
      if let label = view as? UILabel {
        label.textColor = skin.tableCellTextColor
      }
      if let textField = view as? UITextField {
        textField.textColor = skin.tableCellTextColor
      }
      if let stepper = view as? UIStepper {
        stepper.tintColor = skin.tableCellTextColor
        stepper.backgroundColor = skin.stepperColor
      }
    }
  }
}

三、使用方式

在需要设置皮肤的类头部定义

let skin: Skin = .purchaseOrder
  1. 设置 view 的皮肤:
override func viewDidLoad() {
    super.viewDidLoad()

    Styler.shared.style(background: view, skin: skin)
  }
  1. 设置 cell 的皮肤:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    Styler.shared.style(cell: cell, with: skin)
  }
  1. 设置 buttons 的皮肤:
private func updateSkin() {
    guard let skin = skin else { return }
    Styler.shared.style(
      background: alertView,
      buttons: [okButton, secondaryButton],
      with: skin)
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值