mvm_core_ui/MVMCoreUI/Styles/Styler.swift
2020-04-01 16:40:58 -04:00

301 lines
11 KiB
Swift

//
// Styler.swift
// MVMCoreUI
//
// Created by Kevin Christiano on 4/1/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import Foundation
import MVMCore
public protocol VogueProtocol {
}
public protocol VogueViewProtocol {
}
public protocol VogueTextProtocol: VogueProtocol {
func styleFont(_ font: UIFont)
func styleTextColor(_ textColor: UIColor)
}
open class Styler {
//--------------------------------------------------
// MARK: - Enums
//--------------------------------------------------
enum MFViewBorder: Int {
case top
case left
case bottom
case right
}
public enum FontStyle: String {
case Title2XLarge
case TitleXLarge
case BoldTitleLarge
case RegularTitleLarge
case BoldTitleMedium
case RegularTitleMedium
case BoldBodyLarge
case RegularBodyLarge
case BoldBodySmall
case RegularBodySmall
case BoldMicro
case RegularMicro
func pointSize() -> CGFloat {
switch self {
case .Title2XLarge:
return 36
case .TitleXLarge:
return 32
case .BoldTitleLarge,
.RegularTitleLarge:
return 24
case .BoldTitleMedium,
.RegularTitleMedium:
return 20
case .BoldBodyLarge,
.RegularBodyLarge:
return 16
case .BoldBodySmall,
.RegularBodySmall:
return 13
case .BoldMicro, .RegularMicro:
return 11
}
}
func isBold() -> Bool {
switch self {
case .Title2XLarge,
.TitleXLarge,
.RegularTitleLarge,
.RegularTitleMedium,
.RegularBodyLarge,
.RegularBodySmall,
.RegularMicro:
return false
case .BoldTitleLarge,
.BoldTitleMedium,
.BoldBodyLarge,
.BoldBodySmall,
.BoldMicro:
return true
}
}
func getFont(_ genericScaling: Bool = true) -> UIFont? {
let size = genericScaling ? sizeFontGeneric(forCurrentDevice: pointSize()) : pointSize()
return getMVA3FontSize(size, bold: isBold())
}
func styleLabel(_ label: UILabel, genericScaling: Bool = true) {
label.font = getFont(genericScaling)
label.textColor = .black
}
}
public enum Tier {
case primary
case secondary
case ternary
case quaternary
}
//--------------------------------------------------
// MARK: - Functions
//--------------------------------------------------
class func splitTextFieldWidth() -> CGFloat {
return splitTextFieldWidth(forViewWidth: MVMCoreUISplitViewController.getDetailViewWidth())
}
class func splitTextFieldWidth(forViewWidth width: CGFloat) -> CGFloat {
return (width - CGFloat(PaddingOne)) / 2 - Padding.Default.HorizontalPaddingForApplicationWidth
}
class func sizeObjectGeneric(forCurrentDevice size: CGFloat) -> MFSizeObject? {
let sizeObject = MFSizeObject(standardSize: size, standardiPadPortraitSize: size * 1.3)
sizeObject?.addLargerThanCustomSize(size * 1.4, forThreshold: MFSizeStandardiPadLandscapeThreshold)
sizeObject?.addLargerThanCustomSize(size * 1.5, forThreshold: MFSizeiPadProLandscapeThreshold)
return sizeObject
}
class func sizeFontGeneric(forCurrentDevice size: CGFloat) -> CGFloat {
return sizeObjectGeneric(forCurrentDevice: size)?.getValueBasedOnApplicationWidth() ?? 0
}
//--------------------------------------------------
// MARK: - Spacing
//--------------------------------------------------
class func setDefaultMarginsFor(_ view: UIView?, size: CGFloat, horizontal: Bool = true, vertical: Bool = false) {
let horizontalPadding: CGFloat = horizontal ? Padding.Default.horizontalPadding(forSize: size) : 0
let verticalPadding: CGFloat = vertical ? PaddingDefaultVerticalSpacing3 : 0
DispatchQueue.main.async {
MVMCoreUIUtility.setMarginsFor(view, leading: horizontalPadding, top: verticalPadding, trailing: horizontalPadding, bottom: verticalPadding)
}
}
class func setMarginsFor(_ view: UIView?, size: CGFloat, defaultHorizontal horizontal: Bool, top: CGFloat, bottom: CGFloat) {
let horizontalPadding: CGFloat = horizontal ? Padding.Default.horizontalPadding(forSize: size) : 0
DispatchQueue.main.async {
MVMCoreUIUtility.setMarginsFor(view, leading: horizontalPadding, top: top, trailing: horizontalPadding, bottom: bottom)
}
}
//--------------------------------------------------
// MARK: - 3.0 fonts
//--------------------------------------------------
class func getMVA3FontSize(_ size: CGFloat, bold isBold: Bool) -> UIFont {
if isBold {
return size >= 15 ? MFFonts.mfFontDSBold(size) : MFFonts.mfFontTXBold(size)
} else {
return size >= 15 ? MFFonts.mfFontDSRegular(size) : MFFonts.mfFontTXRegular(size)
}
}
//--------------------------------------------------
// MARK: - Styles
//--------------------------------------------------
class func styleGetAlignCenteredAttrituedString(_ string: inout NSMutableAttributedString?) {
if let string = string, string.length > 0 {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
string.addAttribute(.paragraphStyle,
value: paragraphStyle,
range: NSRange(location: 0, length: string.length))
}
}
class func styleGetBoldString(with inputFont: UIFont?, from inputString: String?) -> NSAttributedString? {
let openingRange = (inputString as NSString?)?.range(of: "{")
let closingRange = (inputString as NSString?)?.range(of: "}")
let attrString = NSMutableAttributedString(string: inputString ?? "")
if openingRange?.location != NSNotFound && closingRange?.location != NSNotFound {
let boldLength = (closingRange?.location ?? 0) - (openingRange?.location ?? 0)
if let inputFont = inputFont {
attrString.setAttributes([NSAttributedString.Key.font: inputFont],
range: NSRange(location: openingRange?.location ?? 0,
length: boldLength))
}
if let closingRange = closingRange {
attrString.replaceCharacters(in: closingRange, with: "")
}
if let openingRange = openingRange {
attrString.replaceCharacters(in: openingRange, with: "")
}
}
return attrString
}
//--------------------------------------------------
// MARK: - Gradient Colors
//--------------------------------------------------
class func gradientSpecialTicketGold() -> [AnyHashable] {
return [UIColor(red: 0.72, green: 0.6, blue: 0.33, alpha: 1),
UIColor(red: 1, green: 0.85, blue: 0.52, alpha: 1),
UIColor(red: 1, green: 0.85, blue: 0.52, alpha: 1),
UIColor(red: 1, green: 0.85, blue: 0.52, alpha: 1),
UIColor(red: 0.6, green: 0.42, blue: 0.07, alpha: 1)]
}
class func gradientSpecialTicketGoldCGColor() -> [AnyHashable] {
return [UIColor(red: 0.72, green: 0.6, blue: 0.33, alpha: 1).cgColor,
UIColor(red: 1, green: 0.85, blue: 0.52, alpha: 1).cgColor,
UIColor(red: 1, green: 0.85, blue: 0.52, alpha: 1).cgColor,
UIColor(red: 1, green: 0.85, blue: 0.52, alpha: 1).cgColor,
UIColor(red: 0.6, green: 0.42, blue: 0.07, alpha: 1).cgColor].compactMap { $0 }
}
class func styleGetLowCaseSpace(_ inputString: String?) -> String? {
if (inputString?.count ?? 0) > 0 {
let trimmedString = inputString?.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
return trimmedString
}
return ""
}
class func labelStrokeAttributes(_ color: UIColor?) -> [AnyHashable: Any]? {
guard let color = color else { return nil }
return [NSAttributedString.Key.strokeColor: color,
NSAttributedString.Key.strokeWidth: -1.0]
}
//--------------------------------------------------
// MARK: - Custom Styling Views
//--------------------------------------------------
class func styleView(_ view: UIView, show border: MFViewBorder, with color: UIColor?, borderLineWidth: CGFloat, borderLineLength borderLineLengh: CGFloat) {
let borderLine = UIView(frame: .zero)
borderLine.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(borderLine)
switch border {
case .top:
NSLayoutConstraint.constraintPinSubview(borderLine, pinTop: true, pinBottom: false, pinLeft: false, pinRight: false)
NSLayoutConstraint.constraintPinView(borderLine, heightConstraint: true, heightConstant: borderLineWidth, widthConstraint: true, widthConstant: borderLineLengh)
NSLayoutConstraint.constraintPinSubview(borderLine, pinCenterX: true, pinCenterY: false)
case .left:
NSLayoutConstraint.constraintPinSubview(borderLine, pinTop: false, pinBottom: false, pinLeft: true, pinRight: false)
NSLayoutConstraint.constraintPinView(borderLine, heightConstraint: true, heightConstant: borderLineLengh, widthConstraint: true, widthConstant: borderLineWidth)
NSLayoutConstraint.constraintPinSubview(borderLine, pinCenterX: false, pinCenterY: true)
case .bottom:
NSLayoutConstraint.constraintPinSubview(borderLine, pinTop: false, pinBottom: true, pinLeft: false, pinRight: false)
NSLayoutConstraint.constraintPinView(borderLine, heightConstraint: true, heightConstant: borderLineWidth, widthConstraint: true, widthConstant: borderLineLengh)
NSLayoutConstraint.constraintPinSubview(borderLine, pinCenterX: true, pinCenterY: false)
case .right:
NSLayoutConstraint.constraintPinSubview(borderLine, pinTop: false, pinBottom: false, pinLeft: false, pinRight: true)
NSLayoutConstraint.constraintPinView(borderLine, heightConstraint: true, heightConstant: borderLineLengh, widthConstraint: true, widthConstant: borderLineWidth)
NSLayoutConstraint.constraintPinSubview(borderLine, pinCenterX: false, pinCenterY: true)
}
borderLine.backgroundColor = color
}
}