| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- //
- // FLEXArgumentInputView.m
- // Flipboard
- //
- // Created by Ryan Olson on 5/30/14.
- // Copyright (c) 2014 Flipboard. All rights reserved.
- //
- #import "FLEXArgumentInputView.h"
- #import "FLEXUtility.h"
- @interface FLEXArgumentInputView ()
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UITextView *inputTextView;
- @end
- @implementation FLEXArgumentInputView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Default to two lines in the text view. Users of the class can customize.
- self.numberOfInputLines = 2;
-
- self.inputTextView = [[UITextView alloc] init];
- self.inputTextView.font = [[self class] inputFont];
- self.inputTextView.backgroundColor = [UIColor whiteColor];
- self.inputTextView.layer.borderColor = [[UIColor blackColor] CGColor];
- self.inputTextView.layer.borderWidth = 1.0;
- self.inputTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
- self.inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
- [self addSubview:self.inputTextView];
- }
- return self;
- }
- - (void)layoutSubviews
- {
- CGFloat originY = 0;
- CGFloat contentWidth = self.bounds.size.width;
- if ([self.title length] > 0) {
- CGSize constrainSize = CGSizeMake(contentWidth, CGFLOAT_MAX);
- CGSize labelSize = [self.titleLabel sizeThatFits:constrainSize];
- self.titleLabel.frame = CGRectMake(0, originY, labelSize.width, labelSize.height);
- originY = CGRectGetMaxY(self.titleLabel.frame) + [[self class] titleBottomPadding];
- }
-
- self.inputTextView.frame = CGRectMake(0, originY, contentWidth, [self inputTextViewHeight]);
- }
- - (void)setTitle:(NSString *)title
- {
- if (![_title isEqual:title]) {
- _title = title;
- self.titleLabel.text = title;
- [self setNeedsLayout];
- }
- }
- - (UILabel *)titleLabel
- {
- if (!_titleLabel) {
- _titleLabel = [[UILabel alloc] init];
- _titleLabel.font = [[self class] titleFont];
- _titleLabel.backgroundColor = self.backgroundColor;
- _titleLabel.textColor = [UIColor colorWithWhite:0.3 alpha:1.0];
- _titleLabel.numberOfLines = 0;
- [self addSubview:_titleLabel];
- }
- return _titleLabel;
- }
- #pragma mark - Text View Passthroughs
- - (void)setKeyboardType:(UIKeyboardType)keyboardType
- {
- self.inputTextView.keyboardType = keyboardType;
- }
- - (UIKeyboardType)keyboardType
- {
- return self.inputTextView.keyboardType;
- }
- - (void)setInputText:(NSString *)inputText
- {
- self.inputTextView.text = inputText;
- }
- - (NSString *)inputText
- {
- return self.inputTextView.text;
- }
- - (BOOL)inputViewIsFirstResponder
- {
- return self.inputTextView.isFirstResponder;
- }
- #pragma mark - Class Helpers
- + (UIFont *)inputFont
- {
- return [FLEXUtility defaultFontOfSize:14.0];
- }
- + (UIFont *)titleFont
- {
- return [FLEXUtility defaultFontOfSize:12.0];
- }
- + (CGFloat)titleBottomPadding
- {
- return 4.0;
- }
- #pragma mark - Sizing
- - (CGFloat)inputTextViewHeight
- {
- return ceil([[self class] inputFont].lineHeight * self.numberOfInputLines) + 20.0;
- }
- - (CGSize)sizeThatFits:(CGSize)size
- {
- CGFloat height = 0;
-
- if ([self.title length] > 0) {
- CGSize constrainSize = CGSizeMake(size.width, CGFLOAT_MAX);
- height += ceil([self.title sizeWithFont:[[self class] titleFont] constrainedToSize:constrainSize].height);
- height += [[self class] titleBottomPadding];
- }
-
- height += [self inputTextViewHeight];
-
- return CGSizeMake(size.width, height);
- }
- @end
|