From e0a93cc71f228e317bde1dca8fb9ec47116568a3 Mon Sep 17 00:00:00 2001 From: Sumanth Nadigadda Date: Mon, 11 Apr 2022 00:18:55 +0530 Subject: [PATCH] Adding width attribute to button, to set custom button size. --- .../Atomic/Atoms/Buttons/ButtonModel.swift | 7 +++++ .../Atomic/Atoms/Buttons/PillButton.swift | 27 +++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift b/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift index 3b72d37c..8b3da748 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift @@ -23,6 +23,7 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat public var title: String public var action: ActionModelProtocol public var enabled: Bool = true + public var width: CGFloat? public var style: Styler.Button.Style? { didSet { guard let style = style else { return } @@ -183,6 +184,7 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat case disabledFillColor case disabledTextColor case disabledBorderColor + case width } //-------------------------------------------------- @@ -242,6 +244,10 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat if let disabledBorderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledBorderColor) { self.disabledBorderColor = disabledBorderColor } + + if let width = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .width) { + self.width = width + } } open func encode(to encoder: Encoder) throws { @@ -263,5 +269,6 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat try container.encodeIfPresent(style, forKey: .style) try container.encodeIfPresent(size, forKey: .size) try container.encodeIfPresent(groupName, forKey: .groupName) + try container.encodeIfPresent(width, forKey: .width) } } diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift b/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift index e7d0aacc..4722e3fc 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift @@ -138,7 +138,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { PillButton.getHeight(for: buttonSize, size: size) } - private func getContentEdgeInsets() -> UIEdgeInsets { + private func getTitleEdgeInsets() -> UIEdgeInsets { var verticalPadding = 0.0 var horizontalPadding = 0.0 switch buttonSize { @@ -196,16 +196,21 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { } open override var intrinsicContentSize: CGSize { - if buttonSize == .tiny { - let size = super.intrinsicContentSize - let width = size.width + (2 * getInnerPadding()) - return CGSize(width: max(width, getMinimumWidth()), height: getHeight()) - } else if buttonSize == .small { - let width = (2 * Padding.Four) + (titleLabel?.attributedText?.boundingRect(with: CGSize(width: 1000, height: 1000), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size.width ?? 0) - return CGSize(width: max(ceil(width), getMinimumWidth()), height: getHeight()) + + if let buttonWidth = buttonModel?.width { + return CGSize(width: buttonWidth, height: getHeight()) } else { - let width = (2 * Padding.Five) + (titleLabel?.attributedText?.boundingRect(with: CGSize(width: 1000, height: 1000), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size.width ?? 0) - return CGSize(width: max(ceil(width), getMinimumWidth()), height: getHeight()) + if buttonSize == .tiny { + let size = super.intrinsicContentSize + let width = size.width + (2 * getInnerPadding()) + return CGSize(width: max(width, getMinimumWidth()), height: getHeight()) + } else if buttonSize == .small { + let width = (2 * Padding.Four) + (titleLabel?.attributedText?.boundingRect(with: CGSize(width: 1000, height: 1000), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size.width ?? 0) + return CGSize(width: max(ceil(width), getMinimumWidth()), height: getHeight()) + } else { + let width = (2 * Padding.Five) + (titleLabel?.attributedText?.boundingRect(with: CGSize(width: 1000, height: 1000), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size.width ?? 0) + return CGSize(width: max(ceil(width), getMinimumWidth()), height: getHeight()) + } } } @@ -256,7 +261,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { } layer.cornerRadius = getInnerPadding() - titleEdgeInsets = getContentEdgeInsets() + titleEdgeInsets = getTitleEdgeInsets() } open override func setupView() {