mvm_core_ui/MVMCoreUI/Molecules/TopLabelsView.m
2019-02-01 09:53:36 -05:00

249 lines
10 KiB
Objective-C

//
// TopLabelsView.m
// mobilefirst
//
// Created by Scott Pfeil on 2/24/16.
// Copyright © 2016 Verizon Wireless. All rights reserved.
//
#import "TopLabelsView.h"
#import "TopLabelsAndBottomButtonsTableViewController.h"
#import <MVMCoreUI/MFSizeObject.h>
#import <MVMCoreUI/MFFonts.h>
#import <MVMCore/MVMCoreDispatchUtility.h>
#import <MVMCoreUI/MFStyler.h>
#import <MVMCore/MVMCoreConstants.h>
#import <MVMCoreUI/NSLayoutConstraint+MFConvenience.h>
#import <MVMCoreUI/UIColor+MFConvenience.h>
@interface TopLabelsView ()
@property (nullable, weak, nonatomic) NSLayoutConstraint *leftConstraintTitle;
@property (nullable, weak, nonatomic) NSLayoutConstraint *rightConstraintTitle;
@property (nullable, weak, nonatomic) NSLayoutConstraint *leftConstraintMessage;
@property (nullable, weak, nonatomic) NSLayoutConstraint *rightConstraintMessage;
@property (nullable, weak, nonatomic) NSLayoutConstraint *leftConstraintSeparator;
@property (nullable, weak, nonatomic) NSLayoutConstraint *rightConstraintSeparator;
@property (nullable, strong, nonatomic) NSLayoutConstraint *heightConstraint;
@end
@implementation TopLabelsView
- (nullable instancetype)initWithTableView:(nullable TopLabelsAndBottomButtonsTableViewController *)tableView {
if (self = [super init]) {
self.tableView = tableView;
}
return self;
}
- (void)updateView:(CGFloat)size {
[super updateView:size];
[MVMCoreDispatchUtility performBlockOnMainThread:^{
[self.headlineLabel updateView:size];
[self.messageLabel updateView:size];
[self.separatorView setLeftAndRightPinConstant:[MFStyler defaultHorizontalPaddingForSize:size]];
}];
}
- (void)setupView {
[super setupView];
self.translatesAutoresizingMaskIntoConstraints = NO;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
[self.headlineLabel removeFromSuperview];
MFLabel *headlineLabel = [MFLabel commonLabelH2:YES];
[self addSubview:headlineLabel];
self.headlineLabel = headlineLabel;
[self.messageLabel removeFromSuperview];
MFLabel *messageLabel = [MFLabel commonLabelB2:YES];
[self addSubview:messageLabel];
self.messageLabel = messageLabel;
[headlineLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[messageLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
NSLayoutConstraint *topLabelConstraint = [NSLayoutConstraint constraintWithItem:headlineLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:PaddingFive];
topLabelConstraint.priority = 999;
topLabelConstraint.active = YES;
self.topLabelConstraint = topLabelConstraint;
self.spaceBetweenLabels = [NSLayoutConstraint constraintWithItem:messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:headlineLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:PaddingTwo];
self.spaceBetweenLabels.active = YES;
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:headlineLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:[MFStyler defaultHorizontalPaddingForApplicationWidth]];
leftConstraint.active = YES;
self.leftConstraintTitle = leftConstraint;
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:headlineLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:[MFStyler defaultHorizontalPaddingForApplicationWidth]];
rightConstraint.priority = 900;
rightConstraint.active = YES;
self.rightConstraintTitle = rightConstraint;
NSLayoutConstraint *leftConstraintMessage = [NSLayoutConstraint constraintWithItem:messageLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:[MFStyler defaultHorizontalPaddingForApplicationWidth]];
leftConstraintMessage.active = YES;
self.leftConstraintMessage = leftConstraintMessage;
NSLayoutConstraint *rightConstraintMessage = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:messageLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:[MFStyler defaultHorizontalPaddingForApplicationWidth]];
rightConstraintMessage.priority = 900;
rightConstraintMessage.active = YES;
self.rightConstraintMessage = rightConstraintMessage;
NSLayoutConstraint *bottomLabelConstraint = [self.bottomAnchor constraintEqualToAnchor:messageLabel.bottomAnchor constant:PaddingDefaultVerticalSpacing];
bottomLabelConstraint.active = YES;
self.bottomLabelConstraint = bottomLabelConstraint;
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:0];
heightConstraint.priority = 950;
self.heightConstraint = heightConstraint;
[self.separatorView removeFromSuperview];
SeparatorView *separatorView = [SeparatorView separatorAddToView:self position:SeparatorPositionBot withHorizontalPadding:[MFStyler defaultHorizontalPaddingForApplicationWidth]];
separatorView.translatesAutoresizingMaskIntoConstraints = NO;
[separatorView setAsHeavy];
separatorView.hidden = YES;
self.separatorView = separatorView;
}
- (void)showTopLabels {
self.topLabelConstraint.active = YES;
self.heightConstraint.active = NO;
[self layoutIfNeeded];
// Need to inform the tableview to unpdate the size of its header
if (self.tableView) {
[self.tableView showHeader];
}
}
- (void)hideTopLabels {
self.topLabelConstraint.active = NO;
self.heightConstraint.active = YES;
[self layoutIfNeeded];
// Need to inform the tableview to unpdate the size of its footer
if (self.tableView) {
[self.tableView hideHeader];
}
}
- (void)hideBottomLabels {
self.spaceBetweenLabels.constant = 0;
[self setMessageString:nil];
[self layoutIfNeeded];
// Need to inform the tableview to unpdate the size of its footer
if (self.tableView) {
[self.tableView hideHeader];
}
}
- (void)spaceLabels:(nullable NSString *)headlineString messageString:(nullable NSString *)messageString {
if (headlineString.length > 0 && messageString.length > 0) {
self.spaceBetweenLabels.constant = PaddingTwo;
[self showTopLabels];
} else if (headlineString.length > 0 || messageString.length > 0) {
self.spaceBetweenLabels.constant = 0;
[self showTopLabels];
} else {
[self hideTopLabels];
}
}
- (void)setMessageString:(nullable NSString *)messageString {
self.messageLabel.text = messageString;
}
- (void)setMessageAttributedString:(nullable NSAttributedString *)messageString {
self.messageLabel.attributedText = messageString;
}
- (void)setHeadlineString:(nullable NSString *)headlineString messageString:(nullable NSString *)messageString {
self.headlineLabel.text = headlineString;
[self setMessageString:messageString];
[self spaceLabels:headlineString messageString:messageString];
}
- (void)setHeadlineAttributedString:(nullable NSAttributedString *)headlineString messageAttributedString:(nullable NSAttributedString *)messageString {
self.headlineLabel.attributedText = headlineString;
[self setMessageAttributedString:messageString];
[self spaceLabels:headlineString.string messageString:messageString.string];
}
#pragma mark - Common styles
- (void)setAsLargeHeadline {
[MFStyler styleLabelHeadlineLarge:self.headlineLabel];
[self layoutIfNeeded];
if (self.tableView) {
[self.tableView showHeader];
}
}
- (void)styleMessageLabelBold {
[self removeFromSuperview];
[MFStyler styleLabelH3:self.messageLabel];
[self layoutIfNeeded];
if (self.tableView) {
//[MFUtility sizeViewToFit:self];
CGRect frame = self.frame;
frame.size.height = CGRectGetHeight(self.frame);
//self.tableView.tableView.tableHeaderView = [[UIView alloc]initWithFrame:frame];
//[self.tableView.tableView.tableHeaderView addSubview:self];
[NSLayoutConstraint constraintPinSubviewToSuperview:self];
}
}
- (void)setLeftConstant:(CGFloat)leftConstant {
self.leftConstraintTitle.constant = leftConstant;
self.leftConstraintMessage.constant = leftConstant;
self.leftConstraintSeparator.constant = leftConstant;
}
- (void)setRightConstant:(CGFloat)rightConstant {
self.rightConstraintTitle.constant = rightConstant;
self.rightConstraintMessage.constant = rightConstant;
self.rightConstraintSeparator.constant = rightConstant;
}
- (void)setWithJSON:(nullable NSDictionary *)json {
[self setHeadlineString:[json stringForKey:KeyTitle] messageString:[json stringForKey:KeyMessage]];
UIColor *textColor = [self colorForTopLabelsWithJSON:json];
self.headlineLabel.textColor = textColor;
self.messageLabel.textColor = textColor;
self.backgroundColor = [self colorForBackgroundWithJSON:json];
NSNumber *showBottomLine = [json objectForKey:@"showBottomLine" ofType:[NSNumber class]];
if (showBottomLine) {
self.separatorView.hidden = ![showBottomLine boolValue];
}
}
- (nonnull UIColor *)colorForTopLabelsWithJSON:(nullable NSDictionary *)json {
NSString *titleColor = [json string:@"titleBgColor"];
if (titleColor) {
return [UIColor mfGetColorForHex:titleColor];
} else {
return [UIColor blackColor];
}
}
- (nonnull UIColor *)colorForBackgroundWithJSON:(nullable NSDictionary *)json {
NSString *color = [json string:@"backgroundColor"];
if (color) {
return [UIColor mfGetColorForHex:color];
} else {
return [UIColor clearColor];
}
}
@end