🐾

UIKitの便利イニシャライザたち

2022/11/08に公開

初めに

UIKitでよく使うイニシャライザの紹介です。
(あくまで個人用なので、適宜カスタマイズしていただければと思います。)

記事の最後にコピペ用にまとめたものを用意しています。

UILabel

extension UILabel {

    convenience init(
        text: String? = nil,
        font: UIFont? = nil,
        textColor: UIColor? = nil,
        backgroundColor: UIColor? = nil
    ) {
        self.init()
        if let text = text {
            self.text = text
        }

        if let font = font {
            self.font = font
        }

        if let textColor = textColor {
            self.textColor = textColor
        }

        if let backgroundColor = backgroundColor {
            self.backgroundColor = backgroundColor
        }
    }
}

UIButton

extension UIButton {

    struct Action {
        var target: Any
        var selector: Selector
    }

    convenience init(
        title: String? = nil,
        image: UIImage? = nil,
        font: UIFont? = nil,
        titleColor: UIColor? = nil,
        backgroundColor: UIColor? = nil,
        action: Action
    ) {
        self.init()

        if let title = title {
            self.setTitle(title, for: .normal)
        } else if let image = image {
            self.setImage(image, for: .normal)
        }

        if let font = font {
            self.titleLabel?.font = font
        }

        if let titleColor = titleColor {
            self.setTitleColor(titleColor, for: .normal)
        } else {
            self.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
        }

        if let backgroundColor = backgroundColor {
            self.backgroundColor = backgroundColor
        }

        self.addTarget(action.target, action: action.selector, for: .touchUpInside)
    }
}

UIStackView

extension UIStackView {

    convenience init(
        views: [UIView],
        axis: NSLayoutConstraint.Axis,
        distribution: UIStackView.Distribution? = nil,
        alignment: UIStackView.Alignment? = nil,
        spacing: CGFloat? = nil
    ) {
        self.init()

        views.forEach { addArrangedSubview($0) }

        self.axis = axis

        if let distribution = distribution {
            self.distribution = distribution
        }

        if let alignment = alignment {
            self.alignment = alignment
        }

        if let spacing = spacing {
            self.spacing = spacing
        }
    }
}

UITextField

extension UITextField {

    convenience init(
        font: UIFont? = nil,
        textColor: UIColor? = nil,
        backgroundColor: UIColor? = nil,
        placeholder: String? = nil
    ) {
        self.init()
        self.borderStyle = .roundedRect

        if let font = font {
            self.font = font
        }

        if let textColor = textColor {
            self.textColor = textColor
        }

        if let backgroundColor = backgroundColor {
            self.backgroundColor = backgroundColor
        }

        if let placeholder = placeholder {
            self.placeholder = placeholder
        }
    }
}

UIColor

extension UIColor {

    /// rgb: 0~255, alpha: 0~1
    convenience init(
        r: Float,
        g: Float,
        b: Float,
        alpha: CGFloat? = nil
    ) {
        self.init(
            red: CGFloat(r / 255.0),
            green: CGFloat(g / 255.0),
            blue: CGFloat(b / 255.0),
            alpha: alpha ?? 1)
    }
}

コピペ用

import UIKit

extension UILabel {

    convenience init(
        text: String? = nil,
        font: UIFont? = nil,
        textColor: UIColor? = nil,
        backgroundColor: UIColor? = nil
    ) {
        self.init()
        if let text = text {
            self.text = text
        }

        if let font = font {
            self.font = font
        }

        if let textColor = textColor {
            self.textColor = textColor
        }

        if let backgroundColor = backgroundColor {
            self.backgroundColor = backgroundColor
        }
    }
}

extension UIButton {

    struct Action {
        var target: Any
        var selector: Selector
    }

    convenience init(
        title: String? = nil,
        image: UIImage? = nil,
        font: UIFont? = nil,
        titleColor: UIColor? = nil,
        backgroundColor: UIColor? = nil,
        action: Action
    ) {
        self.init()

        if let title = title {
            self.setTitle(title, for: .normal)
        } else if let image = image {
            self.setImage(image, for: .normal)
        }

        if let font = font {
            self.titleLabel?.font = font
        }

        if let titleColor = titleColor {
            self.setTitleColor(titleColor, for: .normal)
        } else {
            self.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
        }

        if let backgroundColor = backgroundColor {
            self.backgroundColor = backgroundColor
        }

        self.addTarget(action.target, action: action.selector, for: .touchUpInside)
    }
}

extension UIStackView {

    convenience init(
        views: [UIView],
        axis: NSLayoutConstraint.Axis,
        distribution: UIStackView.Distribution? = nil,
        alignment: UIStackView.Alignment? = nil,
        spacing: CGFloat? = nil
    ) {
        self.init()

        views.forEach { addArrangedSubview($0) }

        self.axis = axis

        if let distribution = distribution {
            self.distribution = distribution
        }

        if let alignment = alignment {
            self.alignment = alignment
        }

        if let spacing = spacing {
            self.spacing = spacing
        }
    }
}

extension UITextField {

    convenience init(
        font: UIFont? = nil,
        textColor: UIColor? = nil,
        backgroundColor: UIColor? = nil,
        placeholder: String? = nil
    ) {
        self.init()
        self.borderStyle = .roundedRect

        if let font = font {
            self.font = font
        }

        if let textColor = textColor {
            self.textColor = textColor
        }

        if let backgroundColor = backgroundColor {
            self.backgroundColor = backgroundColor
        }

        if let placeholder = placeholder {
            self.placeholder = placeholder
        }
    }
}

extension UIColor {

    /// rgb: 0~255, alpha: 0~1
    convenience init(
        r: Float,
        g: Float,
        b: Float,
        alpha: CGFloat? = nil
    ) {
        self.init(
            red: CGFloat(r / 255.0),
            green: CGFloat(g / 255.0),
            blue: CGFloat(b / 255.0),
            alpha: alpha ?? 1)
    }
}
GitHubで編集を提案

Discussion