From b972bdba5b2c4e7ff08617d3309507c37cc1476e Mon Sep 17 00:00:00 2001 From: Sumanth Nadigadda Date: Wed, 13 Apr 2022 00:26:35 +0530 Subject: [PATCH] Setting button width with constraint --- .../Atomic/Atoms/Buttons/ButtonModel.swift | 5 +-- .../Atomic/Atoms/Buttons/PillButton.swift | 37 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift b/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift index 30b5b496..abb2038c 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/ButtonModel.swift @@ -262,10 +262,7 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat } backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) - - if let width = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .width) { - self.width = width - } + self.width = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .width) } open func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift b/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift index cd62c8d0..e86d8a7b 100644 --- a/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift +++ b/MVMCoreUI/Atomic/Atoms/Buttons/PillButton.swift @@ -30,6 +30,12 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { didSet { buttonModel?.size = buttonSize } } + //-------------------------------------------------- + // MARK: - Constraints + //-------------------------------------------------- + + public var widthConstraint: NSLayoutConstraint? + //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- @@ -138,7 +144,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { PillButton.getHeight(for: buttonSize, size: size) } - private func getTitleEdgeInsets() -> UIEdgeInsets { + private func getContentEdgeInsets() -> UIEdgeInsets { var verticalPadding = 0.0 var horizontalPadding = 0.0 switch buttonSize { @@ -195,25 +201,6 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { } } - open override var intrinsicContentSize: CGSize { - - if let buttonWidth = buttonModel?.width { - return CGSize(width: buttonWidth, height: getHeight()) - } else { - 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()) - } - } - } - //-------------------------------------------------- // MARK: - MVMCoreViewProtocol //-------------------------------------------------- @@ -261,7 +248,12 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { } layer.cornerRadius = getInnerPadding() - titleEdgeInsets = getTitleEdgeInsets() + contentEdgeInsets = getContentEdgeInsets() + + if let contraint = buttonModel?.width { + widthConstraint = widthAnchor.constraint(equalToConstant: contraint) + widthConstraint?.isActive = true + } } open override func setupView() { @@ -272,6 +264,9 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { titleLabel?.textAlignment = .center contentHorizontalAlignment = .center stylePrimary() + + widthConstraint = widthAnchor.constraint(greaterThanOrEqualToConstant: getMinimumWidth()) + widthConstraint?.isActive = true } //--------------------------------------------------