Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Added support for UIColor #25

Merged
merged 34 commits into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
09e9082
Add UIColor property binding to a tweak.
sgl0v Apr 7, 2014
c6d9f3b
Implemented FBSliderView control. Represents a regular slider with ab…
sgl0v Apr 8, 2014
059b5a9
Created ColorWheel class for color selection(hue and saturation compo…
sgl0v Apr 9, 2014
f9488fc
Updated color wheel bitmap generation, fixed code style formatting, a…
sgl0v Apr 9, 2014
8856bcb
Implemented view controller to select a color with RGBA components.
sgl0v Apr 11, 2014
f179ee7
Modified: target-action notifying, when the slider's value changed; U…
sgl0v Apr 11, 2014
ad5e84a
Implemented view, that contains UI elements to select color value fro…
sgl0v Apr 13, 2014
f88b6bc
Integrated color selection with the example the project.
sgl0v Apr 13, 2014
7be7941
Springs&struts substituted with autolayout constraints for color sele…
sgl0v Apr 14, 2014
41c3bcd
Fixed auto layout constraints for color components views.
sgl0v Apr 15, 2014
f2eda9e
Changed mechanism of adjusting scrollview when the keyboard appears/d…
sgl0v Apr 16, 2014
45c9997
Code refactored, added comments.
sgl0v Apr 18, 2014
c96b4d1
Code refactored, updated unit tests, created category for UIColor.
sgl0v Apr 18, 2014
2718a83
Code refactored.
sgl0v Apr 21, 2014
b7ff467
Moved UI construction & setting layout constraints to the separate vi…
sgl0v Apr 22, 2014
29a43d5
Added view controller & views to select color using HSB color space.
sgl0v Apr 22, 2014
3a4fa8e
Implemented view, that contains UI elements to select color value fro…
sgl0v Apr 23, 2014
0266e29
Implemented keyboard manager
sgl0v Apr 29, 2014
23fc6ec
Changed keyboard manager to make it reusable all over the project.
sgl0v Apr 29, 2014
85c4c8c
Refactored rgba color components selection view.
sgl0v Apr 30, 2014
15e2bf4
Implemented color selection controller. Refactored RGB & HSB color vi…
sgl0v Apr 30, 2014
66a0e80
Code refactored, implemented color selection controller , resolved pr…
sgl0v May 1, 2014
fc6d3c4
Added Facebook licence to the _FBSliderView.m file.
sgl0v May 1, 2014
df24db0
Changed thumb position calculation in the _FBSliderView class.
sgl0v Jul 17, 2014
6e6fca5
Changed the autolayout constraints that cause the application crash f…
sgl0v Aug 22, 2014
4b87bf8
Merge branch 'master' into color
sgl0v Aug 16, 2015
8b62da2
Removed the global keyboard manager the FBTweakShakeWindow. Defined t…
sgl0v Aug 16, 2015
098ff94
Added the '_' prefix for private classes.
sgl0v Aug 16, 2015
a1af7bd
Moved to the UITableView usage in order to make application UI more c…
sgl0v Aug 17, 2015
b532683
Added the pan gesture recognizer to handle tap gestures on the color …
sgl0v Aug 17, 2015
2fed6de
Replaced the hex-string based solution with UIColor one. Added the la…
sgl0v Aug 25, 2015
e9105f4
Defined function that creates the color wheel.
sgl0v Aug 25, 2015
94da040
Removed the _FBColorWheelView. Defined all needed UI in the _FBColorW…
sgl0v Aug 26, 2015
eab620c
Had to specify the property explicitly to bind UIColor. Now we use d…
sgl0v Aug 27, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion FBTweak.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions FBTweak/FBTweak.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ - (instancetype)initWithIdentifier:(NSString *)identifier
{
if ((self = [super init])) {
_identifier = identifier;
_currentValue = [[NSUserDefaults standardUserDefaults] objectForKey:_identifier];
NSData *archivedValue = [[NSUserDefaults standardUserDefaults] objectForKey:_identifier];
_currentValue = (archivedValue == nil ? archivedValue : [NSKeyedUnarchiver unarchiveObjectWithData:archivedValue]);
}

return self;
Expand Down Expand Up @@ -176,8 +177,9 @@ - (void)setCurrentValue:(FBTweakValue)currentValue

if (_currentValue != currentValue) {
_currentValue = currentValue;
[[NSUserDefaults standardUserDefaults] setObject:_currentValue forKey:_identifier];

// we can't store UIColor to the plist file. That is why we archive value to the NSData.
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_currentValue] forKey:_identifier];

for (id<FBTweakObserver> observer in [_observers setRepresentation]) {
[observer tweakDidChange:self];
}
Expand Down
1 change: 1 addition & 0 deletions FBTweak/FBTweakInline.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "FBTweakStore.h"
#import "FBTweakCategory.h"

#import <UIKit/UIKit.h>
#import <libkern/OSAtomic.h>
#import <mach-o/getsect.h>
#import <mach-o/dyld.h>
Expand Down
2 changes: 1 addition & 1 deletion FBTweak/FBTweakInlineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef struct {
#define fb_tweak_entry_block_field(type, entry, field) (*(type (^__unsafe_unretained (*))(void))(entry->field))()

extern NSString *_FBTweakIdentifier(fb_tweak_entry *entry);

#if __has_feature(objc_arc)
#define _FBTweakRelease(x)
#else
Expand Down
1 change: 1 addition & 0 deletions FBTweak/FBTweakShakeWindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "FBTweakStore.h"
#import "FBTweakShakeWindow.h"
#import "FBTweakViewController.h"
#import "_FBKeyboardManager.h"

// Minimum shake time required to present tweaks on device.
static CFTimeInterval _FBTweakShakeWindowMinTimeInterval = 0.4;
Expand Down
46 changes: 46 additions & 0 deletions FBTweak/_FBColorComponentCell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
Copyright (c) 2014-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
*/

#import <UIKit/UIKit.h>

@class _FBColorComponentCell;

@protocol _FBColorComponentCellDelegate <NSObject>

- (void)colorComponentCell:(_FBColorComponentCell*)cell didChangeValue:(CGFloat)value;

@end

/**
@abstract A cell to edit a color component.
*/
@interface _FBColorComponentCell : UITableViewCell

//! @abstract The title.
@property(nonatomic, copy) NSString *title;

//! @abstract The current value. The default value is 0.0.
@property(nonatomic, assign) CGFloat value;

//! @abstract The minimum value. The default value is 0.0.
@property(nonatomic, assign) CGFloat minimumValue;

//! @abstract The maximum value. The default value is 255.0.
@property(nonatomic, assign) CGFloat maximumValue;

//! @abstract The format string to apply for textfield value. `%.f` by default.
@property(nonatomic, copy) NSString *format;

//! @abstract The array of CGColorRef objects defining the color of each gradient stop on the track.
@property(nonatomic, copy) NSArray *colors;

//! @abstract The cell's delegate.
@property(nonatomic, weak) id<_FBColorComponentCellDelegate> delegate;

@end
166 changes: 166 additions & 0 deletions FBTweak/_FBColorComponentCell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**
Copyright (c) 2014-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
*/

#import "_FBColorComponentCell.h"
#import "_FBSliderView.h"

extern CGFloat const _FBRGBColorComponentMaxValue;
static CGFloat const _FBColorComponentMargin = 5.0f;
static CGFloat const _FBColorComponentTextSpacing = 10.0f;
static CGFloat const _FBColorComponentTextFieldWidth = 50.0f;

@interface _FBColorComponentCell () <UITextFieldDelegate>

@end

@implementation _FBColorComponentCell {
UILabel *_label;
_FBSliderView *_slider;
UITextField *_textField;
}

- (instancetype)init
{
if ((self = [super init])) {
[self _init];
}

return self;
}

- (void)setTitle:(NSString *)title
{
_label.text = title;
}

- (void)setMinimumValue:(CGFloat)minimumValue
{
_slider.minimumValue = minimumValue;
}

- (void)setMaximumValue:(CGFloat)maximumValue
{
_slider.maximumValue = maximumValue;
}

- (void)setValue:(CGFloat)value
{
_slider.value = value;
_textField.text = [NSString stringWithFormat:_format, value];
}

- (NSString*)title
{
return _label.text;
}

- (CGFloat)minimumValue
{
return _slider.minimumValue;
}

- (CGFloat)maximumValue
{
return _slider.maximumValue;
}

- (CGFloat)value
{
return _slider.value;
}

- (void)setColors:(NSArray *)colors
{
_slider.colors = colors;
}

- (NSArray *)colors
{
return _slider.colors;
}

#pragma mark - UITextFieldDelegate methods

- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setValue:[textField.text floatValue]];
[self.delegate colorComponentCell:self didChangeValue:self.value];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

//first, check if the new string is numeric only. If not, return NO;
if ([newString rangeOfCharacterFromSet:[self _invertedDecimalDigitAndDotCharacterSet]].location != NSNotFound) {
return NO;
}

return [newString floatValue] <= _slider.maximumValue;
}

-(void)layoutSubviews
{
[super layoutSubviews];

CGFloat cellHeight = CGRectGetHeight(self.bounds);
CGFloat cellWidth = CGRectGetWidth(self.bounds);
CGFloat labelWidth = [_label sizeThatFits:CGSizeZero].width;
_label.frame = CGRectIntegral((CGRect){_FBColorComponentMargin, _FBColorComponentMargin, labelWidth, cellHeight - 2 * _FBColorComponentMargin});
_textField.frame = CGRectIntegral((CGRect){cellWidth - _FBColorComponentMargin - _FBColorComponentTextFieldWidth, _FBColorComponentMargin, _FBColorComponentTextFieldWidth, cellHeight - 2 * _FBColorComponentMargin});
CGFloat sliderWidth = CGRectGetMinX(_textField.frame) - CGRectGetMaxX(_label.frame) - 2 * _FBColorComponentTextSpacing;
_slider.frame = CGRectIntegral((CGRect){CGRectGetMaxX(_label.frame) + _FBColorComponentTextSpacing, _FBColorComponentMargin, sliderWidth, cellHeight - 2 * _FBColorComponentMargin});
}

#pragma mark - Private methods

- (void)_init
{
self.selectionStyle = UITableViewCellSelectionStyleNone;

_format = @"%.f";

_label = [[UILabel alloc] init];
_label.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:_label];

_slider = [[_FBSliderView alloc] init];
_slider.maximumValue = _FBRGBColorComponentMaxValue;
[self.contentView addSubview:_slider];

_textField = [[UITextField alloc] init];
_textField.textAlignment = NSTextAlignmentCenter;
[_textField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: dot synatx

[self.contentView addSubview:_textField];

[self setValue:0.0f];
[_slider addTarget:self action:@selector(_didChangeSliderValue:) forControlEvents:UIControlEventValueChanged];
[_textField setDelegate:self];
}

- (void)_didChangeSliderValue:(_FBSliderView*)sender
{
[self setValue:sender.value];
[self.delegate colorComponentCell:self didChangeValue:self.value];
}

- (NSCharacterSet*)_invertedDecimalDigitAndDotCharacterSet
{
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet decimalDigitCharacterSet];
[characterSet addCharactersInString:@"."];
return [[characterSet invertedSet] copy];
}

@end
93 changes: 93 additions & 0 deletions FBTweak/_FBColorUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
Copyright (c) 2014-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
*/

#import <UIKit/UIKit.h>

typedef struct { CGFloat red, green, blue, alpha; } RGB;
typedef struct { CGFloat hue, saturation, brightness, alpha; } HSB;

extern CGFloat const _FBRGBColorComponentMaxValue;
extern CGFloat const _FBAlphaComponentMaxValue;
extern CGFloat const _FBHSBColorComponentMaxValue;
extern NSUInteger const _FBRGBAColorComponentsSize;
extern NSUInteger const _FBHSBAColorComponentsSize;

typedef NS_ENUM(NSUInteger, _FBRGBColorComponent) {
_FBRGBColorComponentRed,
_FBRGBColorComponentGreed,
_FBRGBColorComponentBlue,
_FBRGBColorComponentAlpha
};

typedef NS_ENUM(NSUInteger, _FBHSBColorComponent) {
_FBHSBColorComponentHue,
_FBHSBColorComponentSaturation,
_FBHSBColorComponentBrightness,
_FBHSBColorComponentAlpha
};

/**
* Converts an RGB color value to HSV.
* Assumes r, g, and b are contained in the set [0, 1] and
* returns h, s, and b in the set [0, 1].
*
* @param rgb The rgb color values
* @return The hsb color values
*/
extern HSB _FBRGB2HSB(RGB rgb);

/**
* Converts an HSB color value to RGB.
* Assumes h, s, and b are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param hsb The hsb color values
* @return The rgb color values
*/
extern RGB _FBHSB2RGB(HSB hsb);

/**
* Returns the rgb values of the color components.
*
* @param color The color value.
*
* @return The values of the color components (including alpha).
*/
extern RGB _FBRGBColorComponents(UIColor *color);

/**
* Returns the color wheel's hue value according to the position, color wheel's center and radius.
*
* @param position The position in the color wheel.
* @param center The color wheel's center.
* @param radius The color wheel's radius.
*
* @return The hue value.
*/
extern CGFloat _FBGetColorWheelHue(CGPoint position, CGPoint center, CGFloat radius);

/**
* Returns the color wheel's saturation value according to the position, color wheel's center and radius.
*
* @param position The position in the color wheel.
* @param center The color wheel's center.
* @param radius The color wheel's radius.
*
* @return The saturation value.
*/
extern CGFloat _FBGetColorWheelSaturation(CGPoint position, CGPoint center, CGFloat radius);

/**
* Creates the color wheel with specified diameter.
*
* @param diameter The color wheel's diameter.
*
* @return The color wheel image.
*/
extern CGImageRef _FBCreateColorWheelImage(CGFloat diameter);
Loading