Skip to content

Instantly share code, notes, and snippets.

@moudy
Last active December 22, 2015 18:49
Show Gist options
  • Save moudy/6515488 to your computer and use it in GitHub Desktop.
Save moudy/6515488 to your computer and use it in GitHub Desktop.
Constraints category
#import "PomTaskView+Constraints.h"
@implementation PomTaskView (Constraints)
- (void)updateConstraints {
[super updateConstraints];
NSMutableArray *constraints = [NSMutableArray new];
NSDictionary *views = [self subviewsDictionary];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_button(buttonHeight)]|"
options:0
metrics:[self metrics]
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_button]|"
options:0
metrics:nil
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_repeatLabel(70)][_button]"
options:0
metrics:nil
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-padding-[_repeatLabel]-[_repeatSwitch]-padding-|"
options:NSLayoutFormatAlignAllCenterY
metrics:[self metrics]
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_textField]|"
options:0
metrics:nil
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_textField][_repeatLabel]"
options:0
metrics:nil
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_pickerView][_textField]"
options:0
metrics:nil
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_pickerView]|"
options:0
metrics:nil
views:views]];
[self addConstraints:constraints];
}
- (NSDictionary *)metrics {
return @{
@"buttonHeight": @80,
@"padding": [NSNumber numberWithInt:POM_TEXT_FIELD_PADDING]
};
}
@end
//
// PomTaskView.m
// PomodoroTasks
//
// Created by Moudy Elkammash on 8/31/13.
// Copyright (c) 2013 Moudy. All rights reserved.
//
#import "PomTaskView.h"
#import "PomTaskView+Constraints.h"
#import "PomTextField.h"
#import "UIColor+PomColor.h"
#import "PomScrollForTextField.h"
#import "PomPickerView.h"
#import "PomAppDelegate.h"
#define POMPickerLabelTag 1
@interface PomTaskView()
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *repeatLabel;
@property (nonatomic, strong) UISwitch *repeatSwitch;
@property (nonatomic, strong) PomTextField *textField;
@property (nonatomic, strong) PomPickerView *pickerView;
@property (nonatomic, strong) PomScrollForTextField *scrollForTextField;
@end
@implementation PomTaskView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor pomBackgroundColor];
[self addSubviews];
[self registerForNotifications];
}
return self;
}
- (NSDictionary *)subviewsDictionary {
return NSDictionaryOfVariableBindings(_button,
_repeatLabel,
_repeatSwitch,
_pickerView,
_textField);
}
- (void)addSubviews {
[@[self.button,
self.repeatLabel,
self.textField,
self.pickerView,
self.repeatSwitch] enumerateObjectsUsingBlock:^(UIView *subview, NSUInteger idx, BOOL *stop) {
subview.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:subview];
}];
self.scrollForTextField = [[PomScrollForTextField alloc] initWithTextField:self.textField inView:self];
}
- (void)configureWithTask:(PomTaskPresenter *)task {
// TODO - constants for component ints
[self.pickerView selectWorkTimeRow:[task rowForTaskTime]];
[self.pickerView selectBreakTimeRow:[task rowForBreakTime]];
self.textField.text = task.name;
self.repeatSwitch.on = task.repeat;
}
- (void)registerForNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldTextDidChange:)
name:UITextFieldTextDidChangeNotification
object:self.textField];
}
#pragma mark - Picker View Delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
if (!view){
view = [UIView new];
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 34)];
l.tag = POMPickerLabelTag;
l.textAlignment = NSTextAlignmentRight;
l.font=[l.font fontWithSize:20];
int originX = component == 0 ? 0 : -40;
CGRect frame = l.frame;
frame.origin.x = originX;
l.frame = frame;
[view addSubview:l];
}
UILabel *label = (UILabel *)[view viewWithTag:POMPickerLabelTag];
label.text = [[PomAppDelegate sharedDelegate].timeOptions titleForRow:row forIndex:component];
return view;
}
#pragma mark - Picker View Data Source
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return [[PomAppDelegate sharedDelegate].timeOptions numberOfOptionTypes];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [[PomAppDelegate sharedDelegate].timeOptions numberOfOptionsForTypeWithIndex:component];
}
#pragma mark - Name Textfield
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Any additional checks to ensure you have the correct textField here.
[textField resignFirstResponder];
return YES;
}
- (void)textFieldTextDidChange:(NSNotification *)notification {
UITextField *tf = notification.object;
NSString *name = tf.text;
NSLog(@"name was chaged: %@", name);
}
#pragma mark - Subview Initializers
- (UILabel *)repeatLabel {
if(!_repeatLabel) {
_repeatLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
_repeatLabel.text = @"Repeat";
//_repeatLabel.backgroundColor = [UIColor blueColor];
}
return _repeatLabel;
}
- (UIButton *)button {
if(!_button) {
_button = [UIButton new];
//_button.backgroundColor = [UIColor redColor];
[_button setTitle:@"Button Text" forState:UIControlStateNormal];
}
return _button;
}
- (UISwitch *)repeatSwitch {
if(!_repeatSwitch) {
_repeatSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
}
return _repeatSwitch;
}
- (PomTextField *)textField {
if(!_textField) {
_textField = [PomTextField new];
_textField.placeholder = @"Task Name";
_textField.delegate = self;
//_textField.backgroundColor = [UIColor greenColor];
_textField.returnKeyType = UIReturnKeyDone;
}
return _textField;
}
- (UIPickerView *)pickerView {
if(!_pickerView) {
_pickerView = [PomPickerView new];
_pickerView.delegate = self;
_pickerView.dataSource = self;
}
return _pickerView;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment