天天看點

Swift 換皮膚工具類

該工具類可設定 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)
  }