Browse Source

Add font argument input view.

Ryan Olson 12 years ago
parent
commit
8c112b9ae3

+ 13 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputFontView.h

@@ -0,0 +1,13 @@
+//
+//  FLEXArgumentInputFontView.h
+//  Flipboard
+//
+//  Created by Ryan Olson on 6/28/14.
+//  Copyright (c) 2014 Flipboard. All rights reserved.
+//
+
+#import "FLEXArgumentInputView.h"
+
+@interface FLEXArgumentInputFontView : FLEXArgumentInputView
+
+@end

+ 119 - 0
Classes/Editing/Argument Input Views/FLEXArgumentInputFontView.m

@@ -0,0 +1,119 @@
+//
+//  FLEXArgumentInputFontView.m
+//  Flipboard
+//
+//  Created by Ryan Olson on 6/28/14.
+//  Copyright (c) 2014 Flipboard. All rights reserved.
+//
+
+#import "FLEXArgumentInputFontView.h"
+#import "FLEXArgumentInputViewFactory.h"
+
+@interface FLEXArgumentInputFontView ()
+
+@property (nonatomic, strong) FLEXArgumentInputView *fontNameInput;
+@property (nonatomic, strong) FLEXArgumentInputView *pointSizeInput;
+
+@end
+
+@implementation FLEXArgumentInputFontView
+
+- (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding
+{
+    self = [super initWithArgumentTypeEncoding:typeEncoding];
+    if (self) {
+        self.fontNameInput = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:"@\"NSString\""];
+        self.fontNameInput.backgroundColor = self.backgroundColor;
+        self.fontNameInput.targetSize = FLEXArgumentInputViewSizeSmall;
+        self.fontNameInput.title = @"Font Name:";
+        [self addSubview:self.fontNameInput];
+        
+        self.pointSizeInput = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:@encode(CGFloat)];
+        self.pointSizeInput.backgroundColor = self.backgroundColor;
+        self.pointSizeInput.targetSize = FLEXArgumentInputViewSizeSmall;
+        self.pointSizeInput.title = @"Point Size:";
+        [self addSubview:self.pointSizeInput];
+    }
+    return self;
+}
+
+- (void)setBackgroundColor:(UIColor *)backgroundColor
+{
+    [super setBackgroundColor:backgroundColor];
+    self.fontNameInput.backgroundColor = backgroundColor;
+    self.pointSizeInput.backgroundColor = backgroundColor;
+}
+
+- (void)setInputValue:(id)inputValue
+{
+    if ([inputValue isKindOfClass:[UIFont class]]) {
+        UIFont *font = (UIFont *)inputValue;
+        self.fontNameInput.inputValue = font.fontName;
+        self.pointSizeInput.inputValue = @(font.pointSize);
+    }
+}
+
+- (id)inputValue
+{
+    CGFloat pointSize = 0;
+    if ([self.pointSizeInput.inputValue isKindOfClass:[NSValue class]]) {
+        NSValue *pointSizeValue = (NSValue *)self.pointSizeInput.inputValue;
+        if (strcmp([pointSizeValue objCType], @encode(CGFloat)) == 0) {
+            [pointSizeValue getValue:&pointSize];
+        }
+    }
+    return [UIFont fontWithName:self.fontNameInput.inputValue size:pointSize];
+}
+
+- (BOOL)inputViewIsFirstResponder
+{
+    return [self.fontNameInput inputViewIsFirstResponder] || [self.pointSizeInput inputViewIsFirstResponder];
+}
+
+
+#pragma mark - Layout and Sizing
+
+- (void)layoutSubviews
+{
+    [super layoutSubviews];
+    
+    CGFloat runningOriginY = self.topInputFieldVerticalLayoutGuide;
+    
+    CGSize fontNameFitSize = [self.fontNameInput sizeThatFits:self.bounds.size];
+    self.fontNameInput.frame = CGRectMake(0, runningOriginY, fontNameFitSize.width, fontNameFitSize.height);
+    runningOriginY = CGRectGetMaxY(self.fontNameInput.frame) + [[self class] verticalPaddingBetweenFields];
+    
+    CGSize pointSizeFitSize = [self.pointSizeInput sizeThatFits:self.bounds.size];
+    self.pointSizeInput.frame = CGRectMake(0, runningOriginY, pointSizeFitSize.width, pointSizeFitSize.height);
+}
+
++ (CGFloat)verticalPaddingBetweenFields
+{
+    return 10.0;
+}
+
+- (CGSize)sizeThatFits:(CGSize)size
+{
+    CGSize fitSize = [super sizeThatFits:size];
+    
+    CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
+    
+    CGFloat height = fitSize.height;
+    height += [self.fontNameInput sizeThatFits:constrainSize].height;
+    height += [[self class] verticalPaddingBetweenFields];
+    height += [self.pointSizeInput sizeThatFits:constrainSize].height;
+    
+    return CGSizeMake(fitSize.width, height);
+}
+
+
+#pragma mark -
+
++ (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value
+{
+    BOOL supported = strcmp(type, "@\"UIFont\"") == 0;
+    supported = supported || (value && [value isKindOfClass:[UIFont class]]);
+    return supported;
+}
+
+@end

+ 4 - 1
Classes/Editing/Argument Input Views/FLEXArgumentInputViewFactory.m

@@ -14,6 +14,7 @@
 #import "FLEXArgumentInputStructView.h"
 #import "FLEXArgumentInputNotSupportedView.h"
 #import "FLEXArgumentInputStringView.h"
+#import "FLEXArgumentInputFontView.h"
 
 @implementation FLEXArgumentInputViewFactory
 
@@ -35,7 +36,9 @@
     // Note that order is important here since multiple subclasses may support the same type.
     // An example is the number subclass and the bool subclass for the type @encode(BOOL).
     // Both work, but we'd prefer to use the bool subclass.
-    if ([FLEXArgumentInputStringView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
+    if ([FLEXArgumentInputFontView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
+        argumentInputViewSubclass = [FLEXArgumentInputFontView class];
+    } else if ([FLEXArgumentInputStringView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
         argumentInputViewSubclass = [FLEXArgumentInputStringView class];
     } else if ([FLEXArgumentInputStructView supportsObjCType:typeEncoding withCurrentValue:currentValue]) {
         argumentInputViewSubclass = [FLEXArgumentInputStructView class];