Number convenience for NSDictionary category

KeyTextColor constant for MFLabel and core ui
Checker for hasText on MFLabel
update molecule protocol for mf label.
separator use molecule protocol
fix to header
This commit is contained in:
Pfeil, Scott Robert 2019-03-04 17:44:21 -05:00
parent f243d201b6
commit 25c754b234
12 changed files with 226 additions and 47 deletions

View File

@ -8,6 +8,7 @@
#import "PrimaryButton.h" #import "PrimaryButton.h"
#import <MVMCoreUI/MFSizeObject.h> #import <MVMCoreUI/MFSizeObject.h>
#import <MVMCoreUI/MVMCoreUIConstants.h>
#import "MVMCoreUISplitViewController.h" #import "MVMCoreUISplitViewController.h"
#import "MFStyler.h" #import "MFStyler.h"
#import "UIColor+MFConvenience.h" #import "UIColor+MFConvenience.h"
@ -566,7 +567,7 @@
self.primaryButtonType = PrimaryButtonTypeCustom; self.primaryButtonType = PrimaryButtonTypeCustom;
NSString *color = [json string:@"fillColor"]; NSString *color = [json string:@"fillColor"];
self.fillColor = (color ? [UIColor mfGetColorForHex:color] : nil); self.fillColor = (color ? [UIColor mfGetColorForHex:color] : nil);
color = [json string:@"textColor"]; color = [json string:KeyTextColor];
self.textColor = (color ? [UIColor mfGetColorForHex:color] : nil); self.textColor = (color ? [UIColor mfGetColorForHex:color] : nil);
color = [json string:@"borderColor"]; color = [json string:@"borderColor"];
self.borderColor = (color ? [UIColor mfGetColorForHex:color] : nil); self.borderColor = (color ? [UIColor mfGetColorForHex:color] : nil);

View File

@ -10,7 +10,7 @@
#import <MVMCoreUI/MFView.h> #import <MVMCoreUI/MFView.h>
@class MFSizeObject; @class MFSizeObject;
@interface MFLabel : UILabel <MVMCoreViewProtocol> @interface MFLabel : UILabel <MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol>
- (nullable instancetype)initWithStandardFontSize:(CGFloat)size; - (nullable instancetype)initWithStandardFontSize:(CGFloat)size;
@ -23,6 +23,9 @@
// Set the font and set to scale // Set the font and set to scale
- (void)setFont:(nonnull UIFont *)font scale:(BOOL)scale; - (void)setFont:(nonnull UIFont *)font scale:(BOOL)scale;
// Convenience checker for text or attributed text.
- (BOOL)hasText;
#pragma mark - 2.0 #pragma mark - 2.0
+ (nonnull MFLabel *)commonLabelB2:(BOOL)scale; + (nonnull MFLabel *)commonLabelB2:(BOOL)scale;

View File

@ -7,23 +7,60 @@
// //
#import "MFLabel.h" #import "MFLabel.h"
#import "MFStyler.h" #import <MVMCoreUI/MFStyler.h>
#import <MVMCoreUI/MFSizeObject.h> #import <MVMCoreUI/MFSizeObject.h>
#import <MVMCoreUI/MVMCoreUIConstants.h>
#import "UIColor+MFConvenience.h" #import "UIColor+MFConvenience.h"
#import "MFFonts.h" #import "MFFonts.h"
#import "MVMCoreUISplitViewController.h" #import "MVMCoreUISplitViewController.h"
@import MVMCore.MVMCoreGetterUtility; @import MVMCore.MVMCoreGetterUtility;
@import MVMCore.NSDictionary_MFConvenience;
@import MVMCore.MVMCoreJSONConstants;
@interface MFLabel () @interface MFLabel ()
@property (strong, nonatomic) NSNumber *scaleSize; @property (strong, nonatomic) NSNumber *scaleSize;
// Used for scaling the font in updateView.
@property (strong, nonatomic) NSAttributedString *originalAttributedString;
@end @end
@implementation MFLabel @implementation MFLabel
- (instancetype)initWithStandardFontSize:(CGFloat)size { - (void)setupView {
self.backgroundColor = [UIColor clearColor];
self.numberOfLines = 0;
self.lineBreakMode = NSLineBreakByWordWrapping;
self.translatesAutoresizingMaskIntoConstraints = NO;
[self styleB2:YES];
}
- (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
[self setupView];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
[self setupView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupView];
}
return self;
}
- (instancetype)initWithStandardFontSize:(CGFloat)size {
if ([self init]) {
self.standardFontSize = size; self.standardFontSize = size;
} }
return self; return self;
@ -31,7 +68,16 @@
- (void)updateView:(CGFloat)size { - (void)updateView:(CGFloat)size {
self.scaleSize = @(size); self.scaleSize = @(size);
if (!fequal(self.standardFontSize, 0)) { if (self.originalAttributedString) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.originalAttributedString];
[attributedString removeAttribute:NSFontAttributeName range:NSMakeRange(0, attributedString.length)];
[self.originalAttributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, self.originalAttributedString.length) options:0 usingBlock:^(UIFont *value, NSRange range, BOOL * _Nonnull stop) {
// Loop the original attributed string, resize the fonts.
UIFont *font = [value fontWithSize:[[MFStyler sizeObjectGenericForCurrentDevice:value.pointSize] getValueBasedOnSize:size]];
[attributedString addAttribute:NSFontAttributeName value:font range:range];
}];
self.attributedText = attributedString;
} else if (!fequal(self.standardFontSize, 0)) {
MFSizeObject *sizeObject = self.sizeObject; MFSizeObject *sizeObject = self.sizeObject;
if (!sizeObject) { if (!sizeObject) {
sizeObject = [MFStyler sizeObjectGenericForCurrentDevice:self.standardFontSize]; sizeObject = [MFStyler sizeObjectGenericForCurrentDevice:self.standardFontSize];
@ -56,13 +102,12 @@
#pragma mark - Getters #pragma mark - Getters
- (BOOL)hasText {
return self.text.length > 0 || self.attributedText.length > 0;
}
+ (nonnull MFLabel *)label { + (nonnull MFLabel *)label {
MFLabel *label = [[MFLabel alloc] initWithFrame:CGRectZero]; return [[MFLabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.translatesAutoresizingMaskIntoConstraints = NO;
return label;
} }
+ (nonnull MFLabel *)commonLabelB2:(BOOL)scale { + (nonnull MFLabel *)commonLabelB2:(BOOL)scale {
@ -193,6 +238,72 @@
#pragma mark - Setters #pragma mark - Setters
+ (void)setUILabel:(nullable UILabel *)label withJSON:(nullable NSDictionary *)json delegate:(nullable NSObject *)delegate additionalData:(nullable NSDictionary *)additionalData {
if (label) {
label.text = [json string:KeyText];
NSString *textColor = [json string:KeyTextColor];
if (textColor) {
label.textColor = [UIColor mfGetColorForHex:textColor];
}
NSString *backgroundColor = [json string:KeyBackgroundColor];
if (backgroundColor) {
label.backgroundColor = [UIColor mfGetColorForHex:backgroundColor];
}
NSString *accessibilityText = [json string:@"accessibilityText"];
if (accessibilityText) {
label.accessibilityLabel = accessibilityText;
}
NSString *fontName = [json string:@"fontName"];
NSNumber *fontSize = [json optionalNumberForKey:@"fontSize"];
if (fontName) {
label.font = [MFFonts mfFontWithName:fontName size:fontSize ? fontSize.doubleValue : label.font.pointSize];
} else if (fontSize) {
label.font = [label.font fontWithSize:fontSize.doubleValue];
}
NSArray *attributes = [json array:@"attributes"];
if (attributes) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@{NSFontAttributeName:label.font,NSForegroundColorAttributeName:label.textColor}];
for (NSDictionary *attribute in attributes) {
NSNumber *location = [attribute optionalNumberForKey:@"location"];
NSNumber *length = [attribute optionalNumberForKey:@"length"];
if (location && length) {
NSRange range = NSMakeRange(location.unsignedIntegerValue, length.unsignedIntegerValue);
NSString *type = [attribute string:KeyType];
if ([type isEqualToString:@"underline"]) {
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
} else if ([type isEqualToString:@"strikethrough"]) {
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleThick) range:range];
} else if ([type isEqualToString:@"color"]) {
NSString *color = [attribute string:KeyTextColor];
if (color) {
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor mfGetColorForHex:color] range:range];
}
} else if ([type isEqualToString:@"font"]) {
NSString *fontName = [attribute string:@"name"];
NSNumber *fontSize = [attribute optionalNumberForKey:@"size"];
UIFont *font = nil;
if (fontName) {
font = [MFFonts mfFontWithName:fontName size:fontSize ? fontSize.doubleValue : label.font.pointSize];
} else if (fontSize) {
font = [label.font fontWithSize:fontSize.doubleValue];
}
if (font) {
[attributedString addAttribute:NSFontAttributeName value:font range:range];
}
}
}
}
label.attributedText = attributedString;
}
}
}
- (void)setWithJSON:(NSDictionary *)json delegate:(NSObject *)delegate additionalData:(NSDictionary *)additionalData {
[MFLabel setUILabel:self withJSON:json delegate:delegate additionalData:additionalData];
self.originalAttributedString = self.attributedText;
}
- (void)styleB2:(BOOL)scale { - (void)styleB2:(BOOL)scale {
[MFStyler styleLabelB2:self genericScaling:NO]; [MFStyler styleLabelB2:self genericScaling:NO];
[self setScale:scale]; [self setScale:scale];

View File

@ -8,8 +8,11 @@
#import "SeparatorView.h" #import "SeparatorView.h"
#import <MVMCoreUI/MFSizeObject.h> #import <MVMCoreUI/MFSizeObject.h>
#import <MVMCoreUI/MVMCoreUIConstants.h>
#import "MVMCoreUICommonViewsUtility.h" #import "MVMCoreUICommonViewsUtility.h"
#import "UIColor+MFConvenience.h" #import "UIColor+MFConvenience.h"
@import MVMCore.MVMCoreJSONConstants;
@import MVMCore.NSDictionary_MFConvenience;
@interface SeparatorView() @interface SeparatorView()
@ -17,9 +20,6 @@
@implementation SeparatorView @implementation SeparatorView
#pragma mark - class methods #pragma mark - class methods
+ (nullable SeparatorView *)separatorAddToView:(nullable UIView *)superView position:(SeparatorPosition)position positionPadding:(CGFloat)positionPadding withHorizontalPadding:(CGFloat)padding { + (nullable SeparatorView *)separatorAddToView:(nullable UIView *)superView position:(SeparatorPosition)position positionPadding:(CGFloat)positionPadding withHorizontalPadding:(CGFloat)padding {
@ -95,6 +95,27 @@
} }
} }
- (void)setWithJSON:(NSDictionary *)json delegate:(NSObject *)delegate additionalData:(NSDictionary *)additionalData {
[super setWithJSON:json delegate:delegate additionalData:additionalData];
if (json) {
self.hidden = NO;
NSString *type = [json string:KeyType];
if ([type isEqualToString:@"standard"]) {
[self setSize:1];
} else if ([type isEqualToString:@"medium"]) {
[self setSize:2];
} else if ([type isEqualToString:@"heavy"]) {
[self setSize:4];
}
NSString *backgroundColor = [json string:KeyBackgroundColor];
if (backgroundColor) {
self.backgroundColor = [UIColor mfGetColorForHex:backgroundColor];
}
} else {
self.hidden = YES;
}
}
#pragma mark - helper #pragma mark - helper
- (void)hide { - (void)hide {
@ -104,37 +125,37 @@
self.hidden = NO; self.hidden = NO;
} }
- (void)setAsHeavy { - (void)setSize:(CGFloat)size {
self.heightSizeObject.standardSize = 4; self.heightSizeObject.standardSize = size;
self.height.constant = [self.heightSizeObject getValueBasedOnScreenSize]; self.height.constant = [self.heightSizeObject getValueBasedOnScreenSize];
}
- (void)setAsHeavy {
[self setSize:4];
self.backgroundColor = [UIColor blackColor]; self.backgroundColor = [UIColor blackColor];
[self setNeedsLayout]; [self setNeedsLayout];
[self layoutIfNeeded]; [self layoutIfNeeded];
} }
- (void)setAsRegular { - (void)setAsRegular {
self.heightSizeObject.standardSize = 1; [self setSize:1];
self.height.constant = [self.heightSizeObject getValueBasedOnScreenSize];
self.backgroundColor = [UIColor blackColor]; self.backgroundColor = [UIColor blackColor];
[self setNeedsLayout]; [self setNeedsLayout];
[self layoutIfNeeded]; [self layoutIfNeeded];
} }
- (void)setAsLight { - (void)setAsLight {
self.heightSizeObject.standardSize = 1; [self setSize:1];
self.height.constant = [self.heightSizeObject getValueBasedOnScreenSize];
self.backgroundColor = [UIColor mfSilver]; self.backgroundColor = [UIColor mfSilver];
[self setNeedsLayout]; [self setNeedsLayout];
[self layoutIfNeeded]; [self layoutIfNeeded];
} }
- (void)setAsMedium { - (void)setAsMedium {
self.heightSizeObject.standardSize = 2; [self setSize:2];
self.height.constant = [self.heightSizeObject getValueBasedOnScreenSize];
self.backgroundColor = [UIColor blackColor]; self.backgroundColor = [UIColor blackColor];
[self setNeedsLayout]; [self setNeedsLayout];
[self layoutIfNeeded]; [self layoutIfNeeded];
} }
@end @end

View File

@ -164,7 +164,7 @@
// If we are not loading from the cache, check if the server has provided an index to start with. (When loading from the cache, we use the requested pagetype and modules to find the tab) // If we are not loading from the cache, check if the server has provided an index to start with. (When loading from the cache, we use the requested pagetype and modules to find the tab)
__block NSInteger currentIndex = NSNotFound; __block NSInteger currentIndex = NSNotFound;
if (!self.loadObject.pageDataFromCache) { if (!self.loadObject.pageDataFromCache) {
NSNumber *currentIndexNumber = [self.loadObject.pageJSON objectForKey:@"currentTabIndex" ofType:[NSNumber class]]; NSNumber *currentIndexNumber = [self.loadObject.pageJSON optionalNumberForKey:@"currentTabIndex"];
if (currentIndexNumber) { if (currentIndexNumber) {
currentIndex = [currentIndexNumber integerValue]; currentIndex = [currentIndexNumber integerValue];
} }

View File

@ -24,6 +24,7 @@ public class MVMCoreUIHeaderView: ViewConstrainingView {
headlineLabel.updateView(size) headlineLabel.updateView(size)
messageLabel.updateView(size) messageLabel.updateView(size)
separatorView?.updateView(size) separatorView?.updateView(size)
setSpacing()
} }
public override func setupView() { public override func setupView() {
@ -74,6 +75,30 @@ public class MVMCoreUIHeaderView: ViewConstrainingView {
} }
} }
public func setSpacing() {
if headlineLabel.hasText() && messageLabel.hasText() {
spaceBetweenLabels?.constant = PaddingTwo
show()
} else if headlineLabel.hasText() || messageLabel.hasText() {
spaceBetweenLabels?.constant = 0
show()
} else {
hide()
}
}
public override func show() {
super.show()
heightConstraint?.isActive = false
layoutIfNeeded()
}
public override func hide() {
super.hide()
heightConstraint?.isActive = true
layoutIfNeeded()
}
public override func setLeftPinConstant(_ constant: CGFloat) { public override func setLeftPinConstant(_ constant: CGFloat) {
leftConstraintTitle?.constant = constant leftConstraintTitle?.constant = constant
leftConstraintMessage?.constant = constant leftConstraintMessage?.constant = constant
@ -88,10 +113,7 @@ public class MVMCoreUIHeaderView: ViewConstrainingView {
public override func setWithJSON(_ json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) { public override func setWithJSON(_ json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) {
super.setWithJSON(json, delegate: delegate, additionalData: additionalData) super.setWithJSON(json, delegate: delegate, additionalData: additionalData)
headlineLabel.text = json?.optionalStringForKey(KeyTitle) if let colorString = json?.optionalStringForKey(KeyBackgroundColor) {
messageLabel.text = json?.optionalStringForKey(KeyMessage)
separatorView?.isHidden = !(json?.boolForKey("showSeparator") ?? false)
if let colorString = json?.optionalStringForKey("backgroundColor") {
backgroundColor = .mfGet(forHex: colorString) backgroundColor = .mfGet(forHex: colorString)
} }
if let colorString = json?.optionalStringForKey("contentColor") { if let colorString = json?.optionalStringForKey("contentColor") {
@ -101,6 +123,13 @@ public class MVMCoreUIHeaderView: ViewConstrainingView {
separatorView?.backgroundColor = color separatorView?.backgroundColor = color
} }
let headlineJSON = json?.optionalDictionaryForKey("headline")
headlineLabel.setWithJSON(headlineJSON, delegate: delegate, additionalData: additionalData)
let bodyJSON = json?.optionalDictionaryForKey("body")
messageLabel.setWithJSON(bodyJSON, delegate: delegate, additionalData: additionalData)
let separatorJSON = json?.optionalDictionaryForKey("separator")
separatorView?.setWithJSON(separatorJSON, delegate: delegate, additionalData: additionalData)
if separatorView?.isHidden ?? true { if separatorView?.isHidden ?? true {
bottomPin?.constant = 0 bottomPin?.constant = 0
} else { } else {

View File

@ -12,6 +12,10 @@ public class MVMCoreUIMoleculeStackView: MFView {
var spacingBlock: ((Any) -> UIEdgeInsets)? var spacingBlock: ((Any) -> UIEdgeInsets)?
var moleculesArray: [UIView]? var moleculesArray: [UIView]?
public override init(frame: CGRect) {
super.init(frame: frame)
}
init(withJSON json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) { init(withJSON json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) {
super.init(frame: CGRect.zero) super.init(frame: CGRect.zero)
setWithJSON(json, delegate: delegate, additionalData: additionalData) setWithJSON(json, delegate: delegate, additionalData: additionalData)

View File

@ -11,7 +11,7 @@ import UIKit
public class MoleculeStackTemplate: ThreeLayerViewController { public class MoleculeStackTemplate: ThreeLayerViewController {
public override func spaceBetweenMiddleAndBottom() -> CGFloat? { public override func spaceBetweenTopAndMiddle() -> CGFloat? {
return PaddingTwo return PaddingTwo
} }

View File

@ -15,5 +15,6 @@
+ (nullable UIFont *)mfFont75Bd:(CGFloat)size; + (nullable UIFont *)mfFont75Bd:(CGFloat)size;
+ (nullable UIFont *)mfFont55Rg:(CGFloat)size; + (nullable UIFont *)mfFont55Rg:(CGFloat)size;
+ (nullable UIFont *)mfFontOcratxt:(CGFloat)size; + (nullable UIFont *)mfFontOcratxt:(CGFloat)size;
+ (nullable UIFont *)mfFontWithName:(nonnull NSString *)name size:(CGFloat)size;
@end @end

View File

@ -13,6 +13,15 @@
@implementation MFFonts @implementation MFFonts
+ (void)loadMVMFonts {
static dispatch_once_t once;
dispatch_once(&once, ^{
[MFFonts loadFont:@"NHaasGroteskDSStd-75Bd" type:@"otf"];
[MFFonts loadFont:@"NHaasGroteskDSStd-55Rg" type:@"otf"];
[MFFonts loadFont:@"OCRAExtended" type:@"ttf"];
});
}
+ (void)loadFont:(NSString *)fontName type:(NSString *)type{ + (void)loadFont:(NSString *)fontName type:(NSString *)type{
NSString *fontPath = [[MVMCoreUIUtility bundleForMVMCoreUI] pathForResource:fontName ofType:type]; NSString *fontPath = [[MVMCoreUIUtility bundleForMVMCoreUI] pathForResource:fontName ofType:type];
@ -31,30 +40,22 @@
} }
+ (nullable UIFont *)mfFont75Bd:(CGFloat)size { + (nullable UIFont *)mfFont75Bd:(CGFloat)size {
NSString *fontName = @"NHaasGroteskDSStd-75Bd"; [self loadMVMFonts];
static dispatch_once_t once; return [UIFont fontWithName:@"NHaasGroteskDSStd-75Bd" size:size];
dispatch_once(&once, ^{
[MFFonts loadFont:fontName type:@"otf"];
});
return [UIFont fontWithName:fontName size:size];
} }
+ (nullable UIFont *)mfFont55Rg:(CGFloat)size { + (nullable UIFont *)mfFont55Rg:(CGFloat)size {
NSString *fontName = @"NHaasGroteskDSStd-55Rg"; [self loadMVMFonts];
static dispatch_once_t once; return [UIFont fontWithName:@"NHaasGroteskDSStd-55Rg" size:size];
dispatch_once(&once, ^{
[MFFonts loadFont:fontName type:@"otf"];
});
return [UIFont fontWithName:fontName size:size];
} }
+ (nullable UIFont *)mfFontOcratxt:(CGFloat)size { + (nullable UIFont *)mfFontOcratxt:(CGFloat)size {
NSString *fontName = @"OCRAExtended"; [self loadMVMFonts];
static dispatch_once_t once; return [UIFont fontWithName:@"OCRAExtended" size:size];
dispatch_once(&once, ^{ }
[MFFonts loadFont:fontName type:@"ttf"];
}); + (nullable UIFont *)mfFontWithName:(nonnull NSString *)name size:(CGFloat)size {
return [UIFont fontWithName:fontName size:size]; return [UIFont fontWithName:name size:size] ?: [self mfFont55Rg:size];
} }
@end @end

View File

@ -28,6 +28,10 @@ extern NSString * const KeySecondaryButton;
extern NSString * const KeyTitlePrefix; extern NSString * const KeyTitlePrefix;
extern NSString * const KeyTitlePostfix; extern NSString * const KeyTitlePostfix;
extern NSString * const KeyBackgroundColor;
extern NSString * const KeyText;
extern NSString * const KeyTextColor;
#pragma mark - Values #pragma mark - Values
extern NSString * const StringY; extern NSString * const StringY;

View File

@ -27,6 +27,10 @@ NSString * const KeySecondaryButton = @"SecondaryButton";
NSString * const KeyTitlePrefix = @"titlePrefix"; NSString * const KeyTitlePrefix = @"titlePrefix";
NSString * const KeyTitlePostfix = @"titlePostfix"; NSString * const KeyTitlePostfix = @"titlePostfix";
NSString * const KeyBackgroundColor = @"backgroundColor";
NSString * const KeyText = @"text";
NSString * const KeyTextColor = @"textColor";
#pragma mark - Values #pragma mark - Values
NSString * const StringY = @"Y"; NSString * const StringY = @"Y";