Adding width attribute to button, to set custom button size.

This commit is contained in:
Sumanth Nadigadda 2022-04-11 00:18:55 +05:30
parent 6138328383
commit e0a93cc71f
2 changed files with 23 additions and 11 deletions

View File

@ -23,6 +23,7 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat
public var title: String public var title: String
public var action: ActionModelProtocol public var action: ActionModelProtocol
public var enabled: Bool = true public var enabled: Bool = true
public var width: CGFloat?
public var style: Styler.Button.Style? { public var style: Styler.Button.Style? {
didSet { didSet {
guard let style = style else { return } guard let style = style else { return }
@ -183,6 +184,7 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat
case disabledFillColor case disabledFillColor
case disabledTextColor case disabledTextColor
case disabledBorderColor case disabledBorderColor
case width
} }
//-------------------------------------------------- //--------------------------------------------------
@ -242,6 +244,10 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat
if let disabledBorderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledBorderColor) { if let disabledBorderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledBorderColor) {
self.disabledBorderColor = disabledBorderColor self.disabledBorderColor = disabledBorderColor
} }
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 {
@ -263,5 +269,6 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat
try container.encodeIfPresent(style, forKey: .style) try container.encodeIfPresent(style, forKey: .style)
try container.encodeIfPresent(size, forKey: .size) try container.encodeIfPresent(size, forKey: .size)
try container.encodeIfPresent(groupName, forKey: .groupName) try container.encodeIfPresent(groupName, forKey: .groupName)
try container.encodeIfPresent(width, forKey: .width)
} }
} }

View File

@ -138,7 +138,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
PillButton.getHeight(for: buttonSize, size: size) PillButton.getHeight(for: buttonSize, size: size)
} }
private func getContentEdgeInsets() -> UIEdgeInsets { private func getTitleEdgeInsets() -> UIEdgeInsets {
var verticalPadding = 0.0 var verticalPadding = 0.0
var horizontalPadding = 0.0 var horizontalPadding = 0.0
switch buttonSize { switch buttonSize {
@ -196,16 +196,21 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
} }
open override var intrinsicContentSize: CGSize { open override var intrinsicContentSize: CGSize {
if buttonSize == .tiny {
let size = super.intrinsicContentSize if let buttonWidth = buttonModel?.width {
let width = size.width + (2 * getInnerPadding()) return CGSize(width: buttonWidth, height: getHeight())
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 { } else {
let width = (2 * Padding.Five) + (titleLabel?.attributedText?.boundingRect(with: CGSize(width: 1000, height: 1000), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size.width ?? 0) if buttonSize == .tiny {
return CGSize(width: max(ceil(width), getMinimumWidth()), height: getHeight()) 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() layer.cornerRadius = getInnerPadding()
titleEdgeInsets = getContentEdgeInsets() titleEdgeInsets = getTitleEdgeInsets()
} }
open override func setupView() { open override func setupView() {