From 3ff48cf700d010c88dad456bcdd1c3365b99669b Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 4 Sep 2019 13:45:41 -0400 Subject: [PATCH] Style --- .../Items/MoleculeTableViewCell.swift | 51 +++++++++++++++++-- .../HeadlineBody.swift | 24 +++++++-- MVMCoreUI/Styles/MFStyler.h | 1 + MVMCoreUI/Styles/MFStyler.m | 11 +++- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/MVMCoreUI/Molecules/Items/MoleculeTableViewCell.swift b/MVMCoreUI/Molecules/Items/MoleculeTableViewCell.swift index 167e0ea8..210eac6f 100644 --- a/MVMCoreUI/Molecules/Items/MoleculeTableViewCell.swift +++ b/MVMCoreUI/Molecules/Items/MoleculeTableViewCell.swift @@ -15,7 +15,6 @@ import UIKit // In updateView, will set padding to default. open var updateViewHorizontalDefaults = true - open var updateViewVerticalDefaults = true // For the accessory view convenience. public var caretView: CaretView? @@ -35,6 +34,45 @@ import UIKit /// For subclasses that want to use a custom accessory view. open var customAccessoryView = false + public var topMarginPadding: CGFloat = 24 + public var bottomMarginPadding: CGFloat = 24 + + // MARK: - Styling + func style(with styleString: String?) { + guard let styleString = styleString else { + return + } + switch styleString { + case "standard": + styleStandard() + case "header": + styleHeader() + case "none": + styleNone() + default: break + } + } + + func styleStandard() { + topMarginPadding = 24 + bottomMarginPadding = 24 + bottomSeparatorView?.show() + bottomSeparatorView?.setAsLight() + } + + func styleHeader() { + topMarginPadding = 48 + bottomMarginPadding = 16 + bottomSeparatorView?.show() + bottomSeparatorView?.setAsRegular() + } + + func styleNone() { + topMarginPadding = 0 + bottomMarginPadding = 0 + bottomSeparatorView?.hide() + } + // MARK: - Inits public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) @@ -48,7 +86,7 @@ import UIKit // MARK: - MFViewProtocol public func updateView(_ size: CGFloat) { - MFStyler.setDefaultMarginsFor(self, size: size, horizontal: updateViewHorizontalDefaults, vertical: updateViewVerticalDefaults) + MFStyler.setMarginsFor(self, size: size, defaultHorizontal: updateViewHorizontalDefaults, top: topMarginPadding, bottom: bottomMarginPadding) if #available(iOS 11.0, *) { if accessoryView != nil { // Smaller left margin if accessory view. @@ -94,11 +132,14 @@ import UIKit public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { self.json = json; + style(with: json?.optionalStringForKey("style")) + if let useHorizontalMargins = json?.optionalBoolForKey("useHorizontalMargins") { updateViewHorizontalDefaults = useHorizontalMargins } - if let useVerticalMargins = json?.optionalBoolForKey("useVerticalMargins") { - updateViewVerticalDefaults = useVerticalMargins + if json?.optionalBoolForKey("useVerticalMargins") ?? false { + topMarginPadding = 0 + bottomMarginPadding = 0 } if let backgroundColorString = json?.optionalStringForKey(KeyBackgroundColor) { @@ -143,8 +184,8 @@ import UIKit public func reset() { molecule?.reset?() - updateViewVerticalDefaults = true updateViewHorizontalDefaults = true + styleStandard() backgroundColor = .white } diff --git a/MVMCoreUI/Molecules/VerticalCombinationViews/HeadlineBody.swift b/MVMCoreUI/Molecules/VerticalCombinationViews/HeadlineBody.swift index ac0e416e..1077ac0e 100644 --- a/MVMCoreUI/Molecules/VerticalCombinationViews/HeadlineBody.swift +++ b/MVMCoreUI/Molecules/VerticalCombinationViews/HeadlineBody.swift @@ -18,6 +18,24 @@ open class HeadlineBody: ViewConstrainingView { var leftConstraintMessage: NSLayoutConstraint? var rightConstraintMessage: NSLayoutConstraint? + func hasText() -> Bool { + return headlineLabel.hasText || messageLabel.hasText + } + + // MARK: - Styling + func style(with styleString: String?) { + guard let styleString = styleString else { + return + } + switch styleString { + case "header": + stylePageHeader() + case "item": + styleListItem() + default: break + } + } + func styleLandingPageHeader() { headlineLabel.styleH1(true) messageLabel.styleB2(true) @@ -35,10 +53,6 @@ open class HeadlineBody: ViewConstrainingView { messageLabel.styleB2(true) spaceBetweenLabelsConstant = 0 } - - func hasText() -> Bool { - return headlineLabel.hasText || messageLabel.hasText - } // MARK: - MVMCoreViewProtocol open override func updateView(_ size: CGFloat) { @@ -109,6 +123,8 @@ open class HeadlineBody: ViewConstrainingView { open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) + style(with: json?.optionalStringForKey("style")) + let headlineJSON = json?.optionalDictionaryForKey("headline") headlineLabel.setWithJSON(headlineJSON, delegateObject: delegateObject, additionalData: additionalData) let bodyJSON = json?.optionalDictionaryForKey("body") diff --git a/MVMCoreUI/Styles/MFStyler.h b/MVMCoreUI/Styles/MFStyler.h index 2ab3c2f2..267a0377 100644 --- a/MVMCoreUI/Styles/MFStyler.h +++ b/MVMCoreUI/Styles/MFStyler.h @@ -93,6 +93,7 @@ B3 -> Legal + (CGFloat)defaultVerticalPaddingForSize:(CGFloat)size; + (void)setDefaultMarginsForView:(nullable UIView *)view size:(CGFloat)size; + (void)setDefaultMarginsForView:(nullable UIView *)view size:(CGFloat)size horizontal:(BOOL)horizontal vertical:(BOOL)vertical; ++ (void)setMarginsForView:(nullable UIView *)view size:(CGFloat)size defaultHorizontal:(BOOL)horizontal top:(CGFloat)top bottom:(CGFloat)bottom; //------------------------------------------------- // Returns the fonts for these styles. Scales them as needed by default diff --git a/MVMCoreUI/Styles/MFStyler.m b/MVMCoreUI/Styles/MFStyler.m index dfc9909a..abe6c960 100644 --- a/MVMCoreUI/Styles/MFStyler.m +++ b/MVMCoreUI/Styles/MFStyler.m @@ -96,13 +96,20 @@ CGFloat const LabelWithInternalButtonLineSpace = 2; } + (void)setDefaultMarginsForView:(nullable UIView *)view size:(CGFloat)size horizontal:(BOOL)horizontal vertical:(BOOL)vertical { + CGFloat horizontalPadding = horizontal ? [MFStyler defaultHorizontalPaddingForSize:size] : 0; + CGFloat verticalPadding = vertical ? PaddingDefaultVerticalSpacing3 : 0; [MVMCoreDispatchUtility performBlockOnMainThread:^{ - CGFloat horizontalPadding = horizontal ? [MFStyler defaultHorizontalPaddingForSize:size] : 0; - CGFloat verticalPadding = vertical ? PaddingDefaultVerticalSpacing3 : 0; [MVMCoreUIUtility setMarginsForView:view leading:horizontalPadding top:verticalPadding trailing:horizontalPadding bottom:verticalPadding]; }]; } ++ (void)setMarginsForView:(nullable UIView *)view size:(CGFloat)size defaultHorizontal:(BOOL)horizontal top:(CGFloat)top bottom:(CGFloat)bottom { + CGFloat horizontalPadding = horizontal ? [MFStyler defaultHorizontalPaddingForSize:size] : 0; + [MVMCoreDispatchUtility performBlockOnMainThread:^{ + [MVMCoreUIUtility setMarginsForView:view leading:horizontalPadding top:top trailing:horizontalPadding bottom:bottom]; + }]; +} + #pragma mark - 2.0 fonts + (nullable UIFont *)fontH1:(BOOL)genericScaling {