Setting button width with constraint

This commit is contained in:
Sumanth Nadigadda 2022-04-13 00:26:35 +05:30
parent b2a25f840a
commit b972bdba5b
2 changed files with 17 additions and 25 deletions

View File

@ -262,10 +262,7 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat
} }
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
self.width = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .width)
if let width = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .width) {
self.width = width
}
} }
open func encode(to encoder: Encoder) throws { open func encode(to encoder: Encoder) throws {

View File

@ -30,6 +30,12 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
didSet { buttonModel?.size = buttonSize } didSet { buttonModel?.size = buttonSize }
} }
//--------------------------------------------------
// MARK: - Constraints
//--------------------------------------------------
public var widthConstraint: NSLayoutConstraint?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
@ -138,7 +144,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
PillButton.getHeight(for: buttonSize, size: size) PillButton.getHeight(for: buttonSize, size: size)
} }
private func getTitleEdgeInsets() -> UIEdgeInsets { private func getContentEdgeInsets() -> UIEdgeInsets {
var verticalPadding = 0.0 var verticalPadding = 0.0
var horizontalPadding = 0.0 var horizontalPadding = 0.0
switch buttonSize { 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 // MARK: - MVMCoreViewProtocol
//-------------------------------------------------- //--------------------------------------------------
@ -261,7 +248,12 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
} }
layer.cornerRadius = getInnerPadding() layer.cornerRadius = getInnerPadding()
titleEdgeInsets = getTitleEdgeInsets() contentEdgeInsets = getContentEdgeInsets()
if let contraint = buttonModel?.width {
widthConstraint = widthAnchor.constraint(equalToConstant: contraint)
widthConstraint?.isActive = true
}
} }
open override func setupView() { open override func setupView() {
@ -272,6 +264,9 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
titleLabel?.textAlignment = .center titleLabel?.textAlignment = .center
contentHorizontalAlignment = .center contentHorizontalAlignment = .center
stylePrimary() stylePrimary()
widthConstraint = widthAnchor.constraint(greaterThanOrEqualToConstant: getMinimumWidth())
widthConstraint?.isActive = true
} }
//-------------------------------------------------- //--------------------------------------------------