From f9a34737afd20e9eec4dfeb80e98c682de7ab155 Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Tue, 21 May 2024 16:32:24 +0800 Subject: [PATCH] feat: Added method to set Epoxy custom Row size --- Sources/Core/Utilities/Size.swift | 54 +++++++++++++++++++++++++++++++ Sources/Epoxy/Row/ButtonRow.swift | 18 +++++++++++ Sources/Epoxy/Row/ImageRow.swift | 20 +++++++++++- Sources/Epoxy/Row/SwitchRow.swift | 4 +++ Sources/Epoxy/Row/TextRow.swift | 16 ++++++++- 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 Sources/Core/Utilities/Size.swift diff --git a/Sources/Core/Utilities/Size.swift b/Sources/Core/Utilities/Size.swift new file mode 100644 index 0000000..97f3c73 --- /dev/null +++ b/Sources/Core/Utilities/Size.swift @@ -0,0 +1,54 @@ +// +// Size.swift +// RakuyoKit +// +// Created by Rakuyo on 2024/5/21. +// Copyright © 2024 RakuyoKit. All rights reserved. +// + +import Foundation + +// MARK: - Size + +public struct Size { + public var width: Float + + public var height: Float + + public init(width: Float, height: Float) { + self.width = width + self.height = height + } +} + +// MARK: - Logic + +extension Size { + public static var zero: Self { + .init(width: 0, height: 0) + } + + public static var greatestFiniteMagnitude: Self { + .init(width: .greatestFiniteMagnitude, height: .greatestFiniteMagnitude) + } + + public var cgSize: CGSize { + .init(width: CGFloat(width), height: CGFloat(height)) + } +} + +// MARK: Hashable + +extension Size: Hashable { } + +// MARK: Equatable + +extension Size: Equatable { } + +// MARK: CustomStringConvertible + +extension Size: CustomStringConvertible { + public var description: String { + "{ height:\(height), width:\(width) }" + } +} diff --git a/Sources/Epoxy/Row/ButtonRow.swift b/Sources/Epoxy/Row/ButtonRow.swift index f7b7ebc..64e5239 100644 --- a/Sources/Epoxy/Row/ButtonRow.swift +++ b/Sources/Epoxy/Row/ButtonRow.swift @@ -18,6 +18,8 @@ import RAKCore /// If you want to extend, consider building your own view with /// the help of `ButtonRow.Style`, `ButtonRow.Content` and `ButtonRow.Behaviors`. public final class ButtonRow: UIButton { + private lazy var size: Size? = .zero + /// Closure for touch down event. private lazy var didTouchDown: ButtonClosure? = nil @@ -25,6 +27,14 @@ public final class ButtonRow: UIButton { private lazy var didTap: ButtonClosure? = nil } +// MARK: - Life cycle + +extension ButtonRow { + override public var intrinsicContentSize: CGSize { + size?.cgSize ?? super.intrinsicContentSize + } +} + // MARK: Action extension ButtonRow { @@ -43,6 +53,11 @@ extension ButtonRow { extension ButtonRow: StyledView { public struct Style: Hashable { + /// button size + /// + /// When a side value is `greatestFiniteMagnitude`, adaptive size will be used on that side + public let size: Size? + /// The tint color. public let tintColor: UIColor? @@ -56,10 +71,12 @@ extension ButtonRow: StyledView { public let titleStyle: TextRow.Style? public init( + size: Size? = nil, tintColor: UIColor? = nil, type: UIButton.ButtonType = .system, titleStyle: TextRow.Style? = nil ) { + self.size = size self.tintColor = tintColor self.type = type self.titleStyle = titleStyle @@ -71,6 +88,7 @@ extension ButtonRow: StyledView { translatesAutoresizingMaskIntoConstraints = false + size = style.size tintColor = style.tintColor if let titleStyle = style.titleStyle { diff --git a/Sources/Epoxy/Row/ImageRow.swift b/Sources/Epoxy/Row/ImageRow.swift index 5fe32b6..b0a01bc 100644 --- a/Sources/Epoxy/Row/ImageRow.swift +++ b/Sources/Epoxy/Row/ImageRow.swift @@ -17,12 +17,27 @@ import RAKCore /// /// If you want to extend, consider building your own view with /// the help of `ImageRow.Style`, `ImageRow.Content` and `ImageRow.Behaviors`. -public final class ImageRow: UIImageView { } +public final class ImageRow: UIImageView { + private lazy var size: Size? = .zero +} + +// MARK: - Life cycle + +extension ImageRow { + override public var intrinsicContentSize: CGSize { + size?.cgSize ?? super.intrinsicContentSize + } +} // MARK: StyledView extension ImageRow: StyledView { public struct Style: Hashable { + /// Image row size + /// + /// When a side value is `greatestFiniteMagnitude`, adaptive size will be used on that side + public let size: Size? + /// The tint color. public let tintColor: UIColor? @@ -35,10 +50,12 @@ extension ImageRow: StyledView { public let blockAccessibilityDescription: Bool public init( + size: Size? = nil, tintColor: UIColor? = nil, contentMode: ContentMode = .scaleToFill, blockAccessibilityDescription: Bool = false ) { + self.size = size self.tintColor = tintColor self.contentMode = contentMode self.blockAccessibilityDescription = blockAccessibilityDescription @@ -50,6 +67,7 @@ extension ImageRow: StyledView { translatesAutoresizingMaskIntoConstraints = false + size = style.size tintColor = style.tintColor contentMode = style.contentMode diff --git a/Sources/Epoxy/Row/SwitchRow.swift b/Sources/Epoxy/Row/SwitchRow.swift index de4ba49..eefe62a 100644 --- a/Sources/Epoxy/Row/SwitchRow.swift +++ b/Sources/Epoxy/Row/SwitchRow.swift @@ -37,17 +37,20 @@ extension SwitchRow { extension SwitchRow: StyledView { public struct Style: Hashable { + public let scale: CGFloat public let onTintColor: UIColor? public let thumbTintColor: UIColor? public let onImage: UIImage? public let offImage: UIImage? public init( + scale: CGFloat = 1, onTintColor: ConvertibleToColor? = nil, thumbTintColor: ConvertibleToColor? = nil, onImage: UIImage? = nil, offImage: UIImage? = nil ) { + self.scale = scale self.onTintColor = onTintColor?.color self.thumbTintColor = thumbTintColor?.color self.onImage = onImage @@ -60,6 +63,7 @@ extension SwitchRow: StyledView { translatesAutoresizingMaskIntoConstraints = false + transform = { CGAffineTransform(scaleX: $0, y: $0) }(style.scale) onTintColor = style.onTintColor thumbTintColor = style.thumbTintColor onImage = style.onImage diff --git a/Sources/Epoxy/Row/TextRow.swift b/Sources/Epoxy/Row/TextRow.swift index 4d804a4..5771851 100644 --- a/Sources/Epoxy/Row/TextRow.swift +++ b/Sources/Epoxy/Row/TextRow.swift @@ -17,12 +17,23 @@ import RAKCore /// /// If you want to extend, consider building your own view with /// the help of `TextRow.Style` and `TextRow.Content`. -public final class TextRow: UILabel { } +public final class TextRow: UILabel { + private lazy var size: Size? = .zero +} + +// MARK: - Life cycle + +extension TextRow { + override public var intrinsicContentSize: CGSize { + size?.cgSize ?? super.intrinsicContentSize + } +} // MARK: StyledView extension TextRow: StyledView { public struct Style: Hashable { + public let size: Size? public let font: UIFont public let color: UIColor public let alignment: NSTextAlignment @@ -30,12 +41,14 @@ extension TextRow: StyledView { public let lineBreakMode: NSLineBreakMode public init( + size: Size? = nil, font: UIFont = .systemFont(ofSize: UIFont.labelFontSize), color: ConvertibleToColor = UIColor.label, alignment: NSTextAlignment = .left, numberOfLines: Int = 0, lineBreakMode: NSLineBreakMode = .byTruncatingTail ) { + self.size = size self.font = font self.color = color.color self.alignment = alignment @@ -49,6 +62,7 @@ extension TextRow: StyledView { translatesAutoresizingMaskIntoConstraints = false + size = style.size font = style.font textColor = style.color textAlignment = style.alignment