iOS开发

Swift 换皮肤工具类

2022-12-12  本文已影响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)
  }
上一篇下一篇

猜你喜欢

热点阅读