Permalink
Browse files

Refactored the color picker to use the QRadioElement code, also allow…

…s settings values using JSON.
  • Loading branch information...
2 parents 58a9960 + f63e18a commit 29dab93007badb46ea2419beb0faa55eb8236e94 @escoz committed Feb 5, 2013
@@ -48,6 +48,10 @@
5C50E215ECB7C7D4B503AA4C /* QuickDialogController+Animations.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C50EBF53D28B761CBA809FF /* QuickDialogController+Animations.m */; };
5C50E665E1BD1D5B1B8B944E /* navigation.json in Resources */ = {isa = PBXBuildFile; fileRef = 5C50E404FC15CFA443ABBB5C /* navigation.json */; };
5C50E6D6FE14BF78231F9671 /* QuickDialogController+Animations.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C50E22D2B906D69252BE7B4 /* QuickDialogController+Animations.h */; };
+ B45EC5681625E473007B96E2 /* QColorPickerElement.h in Headers */ = {isa = PBXBuildFile; fileRef = B45EC5621625E473007B96E2 /* QColorPickerElement.h */; };
+ B45EC56A1625E473007B96E2 /* QColorPickerElement.m in Sources */ = {isa = PBXBuildFile; fileRef = B45EC5631625E473007B96E2 /* QColorPickerElement.m */; };
+ B45EC56E1625E473007B96E2 /* UIColor+ColorUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = B45EC5661625E473007B96E2 /* UIColor+ColorUtilities.h */; };
+ B45EC5701625E473007B96E2 /* UIColor+ColorUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = B45EC5671625E473007B96E2 /* UIColor+ColorUtilities.m */; };
D80B0E3813E052DF00FA85CA /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D80B0E3713E052DF00FA85CA /* UIKit.framework */; };
D80B0E3A13E052DF00FA85CA /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D80B0E3913E052DF00FA85CA /* Foundation.framework */; };
D80B0E3C13E052DF00FA85CA /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D80B0E3B13E052DF00FA85CA /* CoreGraphics.framework */; };
@@ -199,6 +203,10 @@
5C50E404FC15CFA443ABBB5C /* navigation.json */ = {isa = PBXFileReference; lastKnownFileType = file.json; name = navigation.json; path = QuickDialog.xcodeproj/navigation.json; sourceTree = SOURCE_ROOT; };
5C50EB0025DCEF8A3740C477 /* QAppearance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = QAppearance.h; path = QuickDialog/quickdialog/QAppearance.h; sourceTree = "<group>"; };
5C50EBF53D28B761CBA809FF /* QuickDialogController+Animations.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "QuickDialogController+Animations.m"; path = "quickdialog/QuickDialogController+Animations.m"; sourceTree = SOURCE_ROOT; };
+ B45EC5621625E473007B96E2 /* QColorPickerElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = QColorPickerElement.h; path = quickdialog/QColorPickerElement.h; sourceTree = SOURCE_ROOT; };
+ B45EC5631625E473007B96E2 /* QColorPickerElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = QColorPickerElement.m; path = quickdialog/QColorPickerElement.m; sourceTree = SOURCE_ROOT; };
+ B45EC5661625E473007B96E2 /* UIColor+ColorUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIColor+ColorUtilities.h"; path = "quickdialog/UIColor+ColorUtilities.h"; sourceTree = SOURCE_ROOT; };
+ B45EC5671625E473007B96E2 /* UIColor+ColorUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIColor+ColorUtilities.m"; path = "quickdialog/UIColor+ColorUtilities.m"; sourceTree = SOURCE_ROOT; };
D80B0E3313E052DF00FA85CA /* QuickDialogExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QuickDialogExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
D80B0E3713E052DF00FA85CA /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
D80B0E3913E052DF00FA85CA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -531,6 +539,22 @@
D87B4FC414F16197006DA833 /* DOAutocompleteTextField.m */,
D894F38815559D34000E3C0F /* NSMutableArray+IMSExtensions.h */,
D894F38915559D34000E3C0F /* NSMutableArray+IMSExtensions.m */,
+ B45EC5621625E473007B96E2 /* QColorPickerElement.h */,
+ B45EC5631625E473007B96E2 /* QColorPickerElement.m */,
+ B45EC5661625E473007B96E2 /* UIColor+ColorUtilities.h */,
+ B45EC5671625E473007B96E2 /* UIColor+ColorUtilities.m */,
+ F2F23A26152DD48B00EB6688 /* QuickDialogController+Helpers.h */,
+ F2F23A26152DD48B00EB6686 /* QuickDialogController+Helpers.m */,
+ D8F180E813F0599A009B0CD3 /* QuickDialogWebController.h */,
+ D8F180E813F0599A009B0CD1 /* QuickDialogWebController.m */,
+ D8B394681514E8B3008ECB34 /* QSegmentedElement.m */,
+ D8B394671514E8B3008ECB34 /* QSegmentedElement.h */,
+ D8B39449151394E1008ECB34 /* QEmptyListElement.h */,
+ D8B3944A151394E1008ECB34 /* QEmptyListElement.m */,
+ D8B3944B151394E1008ECB34 /* QMultilineElement.h */,
+ D8B3944C151394E1008ECB34 /* QMultilineElement.m */,
+ D8B3944E151394E1008ECB34 /* QMultilineTextViewController.h */,
+ D8B3944F151394E1008ECB34 /* QMultilineTextViewController.m */,
D8F180E813F0599A009B0CC9 /* NSMutableArray+MoveObject.h */,
D8F180E813F0599A009B0CC7 /* NSMutableArray+MoveObject.m */,
D87B4FC514F16197006DA833 /* QAutoEntryElement.h */,
@@ -749,6 +773,8 @@
5C50E1E06A0557312EB9B187 /* QAppearance.h in Headers */,
D87A4D90169DB59500D4DD63 /* QAppearance.h in Headers */,
D8820B5116A246860070AE22 /* QCountdownElement.h in Headers */,
+ B45EC5681625E473007B96E2 /* QColorPickerElement.h in Headers */,
+ B45EC56E1625E473007B96E2 /* UIColor+ColorUtilities.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -916,6 +942,8 @@
D8820B5216A246860070AE22 /* QCountdownElement.m in Sources */,
D8820C7D16A3543D0070AE22 /* QImageElement.m in Sources */,
D8820C7E16A3543F0070AE22 /* QImageTableViewCell.m in Sources */,
+ B45EC56A1625E473007B96E2 /* QColorPickerElement.m in Sources */,
+ B45EC5701625E473007B96E2 /* UIColor+ColorUtilities.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,14 @@
+//
+// QColorPickerElement.h
+// Color Picker
+//
+// Created by Ben Wyatt on 10/7/12.
+// Copyright (c) 2012 Quickfire Software. All rights reserved.
+//
+
+#import "QLabelElement.h"
+
+@interface QColorPickerElement : QRadioElement
+
+
+@end
@@ -0,0 +1,80 @@
+//
+// QColorPickerElement.m
+// Color Picker
+//
+// Created by Ben Wyatt on 10/7/12.
+// Copyright (c) 2012 Quickfire Software. All rights reserved.
+//
+
+@implementation QColorPickerElement {
+ QSection *_internalRadioItemsSection;
+}
+
+
+- (QColorPickerElement *)init {
+ self = [super init];
+ if (self) {
+ self.items = @[
+ @[@"Black", [UIColor blackColor]],
+ @[@"White", [UIColor whiteColor]],
+ @[@"Blue", [UIColor grayColor]],
+ @[@"Red", [UIColor blueColor]],
+ @[@"Green", [UIColor greenColor]],
+ @[@"Yellow", [UIColor yellowColor]],
+ @[@"Purple", [UIColor purpleColor]],
+ @[@"Magenta", [UIColor magentaColor]]
+ ];
+ self.selected = 0;
+ }
+ return self;
+}
+
+- (void)updateCell:(QEntryTableViewCell *)cell selectedValue:(id)selectedValue {
+ if (self.title == NULL){
+ cell.textField.text = [[selectedValue objectAtIndex:0] description];
+ cell.detailTextLabel.text = nil;
+ cell.textField.textAlignment = self.appearance.labelAlignment;
+ } else {
+ cell.textLabel.text = _title;
+ cell.textField.text = [[selectedValue objectAtIndex:0] description];
+ cell.textField.textAlignment = self.appearance.valueAlignment;
+ }
+ self.image = [self getImageFromItem:selectedValue];;
+}
+
+- (UIImage *)getImageFromItem:(id)selectedValue {
+ id obj = [selectedValue objectAtIndex:1];
+ if ([obj isKindOfClass:[UIColor class]])
+ return [obj imageByDrawingCircleOfColor];
+ if ([obj isKindOfClass:[NSString class]])
+ return [[QColorPickerElement colorFromHexString:obj] imageByDrawingCircleOfColor];
+ return [[UIColor blackColor] imageByDrawingCircleOfColor];
+}
+
+
+- (void)createElements {
+ _sections = nil;
+ self.presentationMode = QPresentationModeNavigationInPopover;
+ _internalRadioItemsSection = [[QSection alloc] init];
+ _parentSection = _internalRadioItemsSection;
+
+ [self addSection:_parentSection];
+
+ for (NSUInteger i=0; i< [_items count]; i++){
+ QRadioItemElement *element = [[QRadioItemElement alloc] initWithIndex:i RadioElement:self];
+ element.image = [self getImageFromItem:[self.items objectAtIndex:i]];
+ element.title = [[self.items objectAtIndex:i] objectAtIndex:0];
+ [_parentSection addElement:element];
+ }
+}
+
++ (UIColor *)colorFromHexString:(NSString *)hexString {
+ unsigned rgbValue = 0;
+ NSScanner *scanner = [NSScanner scannerWithString:hexString];
+ [scanner setScanLocation:1]; // bypass '#' character
+ [scanner scanHexInt:&rgbValue];
+ return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
+}
+
+
+@end
@@ -70,13 +70,14 @@ - (QEntryTableViewCell *)init {
}
- (CGRect)calculateFrameForEntryElement {
-
int extra = (self.textField.clearButtonMode == UITextFieldViewModeNever) ? 15 :10;
if (_entryElement.title == NULL && _entryElement.image==NULL) {
return CGRectMake(10,10,self.contentView.frame.size.width-10-extra, self.frame.size.height-20);
}
if (_entryElement.title == NULL && _entryElement.image!=NULL){
- return CGRectMake( self.imageView.frame.size.width, 10, self.contentView.frame.size.width-10-self.imageView.frame.size.width-extra , self.frame.size.height-20);
+ self.imageView.image = _entryElement.image;
+ [self.imageView sizeToFit];
+ return CGRectMake( self.imageView.frame.size.width+10, 10, self.contentView.frame.size.width-10-self.imageView.frame.size.width-extra , self.frame.size.height-20);
}
CGFloat totalWidth = self.contentView.frame.size.width;
CGFloat titleWidth = 0;
@@ -36,4 +36,7 @@
- (QRadioElement *)initWithItems:(NSArray *)stringArray selected:(NSInteger)selected;
- (QRadioElement *)initWithItems:(NSArray *)stringArray selected:(NSInteger)selected title:(NSString *)title;
+
+- (void)updateCell:(QEntryTableViewCell *)cell selectedValue:(id)selectedValue;
+
@end
@@ -37,6 +37,7 @@ - (void)createElements {
for (NSUInteger i=0; i< [_items count]; i++){
QRadioItemElement *element = [[QRadioItemElement alloc] initWithIndex:i RadioElement:self];
element.imageNamed = [self.itemsImageNames objectAtIndex:i];
+ element.title = [self.items objectAtIndex:i];
[_parentSection addElement:element];
}
}
@@ -120,26 +121,30 @@ - (void)selected:(QuickDialogTableView *)tableView controller:(QuickDialogContro
- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller {
QEntryTableViewCell *cell = (QEntryTableViewCell *) [super getCellForTableView:tableView controller:controller];
- NSString *selectedValue = nil;
+ id selectedValue = nil;
if (_selected >= 0 && _selected <_items.count){
- selectedValue = [[_items objectAtIndex:(NSUInteger) _selected] description];
+ selectedValue = [_items objectAtIndex:(NSUInteger) _selected];
}
+ [self updateCell:cell selectedValue:selectedValue];
+ cell.accessoryType = self.enabled ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
+ cell.selectionStyle = self.enabled ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
+ cell.textField.userInteractionEnabled = NO;
+ [cell setNeedsLayout];
+ return cell;
+}
+
+- (void)updateCell:(QEntryTableViewCell *)cell selectedValue:(id)selectedValue {
if (self.title == NULL){
- cell.textField.text = selectedValue;
+ cell.textField.text = [selectedValue description];
cell.detailTextLabel.text = nil;
cell.textField.textAlignment = self.appearance.labelAlignment;
} else {
cell.textLabel.text = _title;
- cell.textField.text = selectedValue;
+ cell.textField.text = [selectedValue description];
cell.textField.textAlignment = self.appearance.valueAlignment;
}
cell.imageView.image = _image;
- cell.accessoryType = self.enabled ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
- cell.selectionStyle = self.enabled ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
- cell.textField.userInteractionEnabled = NO;
- [cell setNeedsLayout];
- return cell;
}
-(void)setSelected:(NSInteger)aSelected {
@@ -23,15 +23,13 @@ - (QRadioItemElement *)initWithIndex:(NSUInteger)index RadioElement:(QRadioEleme
self = [super init];
_radioElement = radioElement;
_index = index;
- _title = [[radioElement.items objectAtIndex:_index] description];
return self;
}
- (QRadioItemElement *)initWithIndex:(NSUInteger)index RadioSection:(QRadioSection *)section {
self = [super init];
_radioSection = section;
_index = index;
- _title = [[_radioSection.items objectAtIndex:_index] description];
return self;
}
@@ -29,4 +29,5 @@ - (void)setSelected:(NSInteger)selected
[self.selectedIndexes replaceObjectAtIndex:0 withObject:[NSNumber numberWithUnsignedInteger:selected]];
}
-@end
+
+@end
@@ -57,6 +57,9 @@
#import "QMultilineTextViewController.h"
#import "QMultilineElement.h"
#import "QImageElement.h"
+#import "QColorPickerElement.h"
+#import "UIColor+ColorUtilities.h"
+
#import "QuickDialogStyleProvider.h"
#import "QuickDialogEntryElementDelegate.h"
@@ -0,0 +1,16 @@
+//
+// UIColor+ColorUtilities.h
+// Color Picker
+//
+// Created by Ben Wyatt on 10/10/12.
+// Copyright (c) 2012 Quickfire Software. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface UIColor (ColorUtilities)
+
+- (UIColor *)darkerColor;
+- (UIImage *)imageByDrawingCircleOfColor;
+
+@end
@@ -0,0 +1,41 @@
+//
+// UIColor+ColorUtilities.m
+// Color Picker
+//
+// Created by Ben Wyatt on 10/10/12.
+// Copyright (c) 2012 Quickfire Software. All rights reserved.
+//
+
+@implementation UIColor (ColorUtilities)
+
+- (UIColor *)darkerColor
+{
+ float r, g, b, a;
+ if ([self getRed:&r green:&g blue:&b alpha:&a])
+ return [UIColor colorWithRed:MAX(r - 0.4, 0.0)
+ green:MAX(g - 0.4, 0.0)
+ blue:MAX(b - 0.4, 0.0)
+ alpha:a];
+ return nil;
+}
+
+- (UIImage *)imageByDrawingCircleOfColor {
+ UIGraphicsBeginImageContextWithOptions(CGSizeMake(30, 30), NO, [UIScreen mainScreen].scale);
+ CGContextRef ctx = UIGraphicsGetCurrentContext();
+
+ CGContextSetLineWidth(ctx, 1.5f);
+ [self setFill];
+
+ [[self darkerColor] setStroke];
+ CGRect circleRect = CGRectMake(0, 0, 30, 30);
+ circleRect = CGRectInset(circleRect, 8, 8);
+ CGContextFillEllipseInRect(ctx, circleRect);
+ CGContextStrokeEllipseInRect(ctx, circleRect);
+
+ UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+
+ return retImage;
+}
+
+@end
@@ -19,7 +19,9 @@
{"type":"QWebElement", "title":"Web", "url":"http://escoz.com"},
{"type":"QWebElement", "title":"HTML", "html":"<h1>Testing html content</h1><p>here's the content</p>"},
{"type":"QTextElement", "text":"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."},
- {"type":"QLoadingElement"}
+ {"type":"QLoadingElement"},
+ {"type":"QColorPickerElement"},
+ {"type":"QColorPickerElement", "items":[["White", "#ffffff"], ["Light Gray", "#cccccc"], ["Gray", "#777777"], ["Dark Gray", "#444444"], ["Black", "#000000"]]}
]
},
{ "title":"Entry Elements", "elements":[

0 comments on commit 29dab93

Please sign in to comment.