Added support for styling the PickerIOS

Summary:
 - PickerIOS accepts now a new prop: style
 - this prop modifies the native style of the RCTPicker allowing to modify the font size of the items (fontSize), color of the items (color, only 6 char HEX values for now) and alignment of the items (textAlign)
Closes https://github.com/facebook/react-native/pull/4490

Reviewed By: svcscm

Differential Revision: D2723190

Pulled By: nicklockwood

fb-gh-sync-id: ab9188192f1d0d087787dfed8c128073bfaa3235
This commit is contained in:
Emilio Rodriguez
2015-12-08 07:44:56 -08:00
committed by facebook-github-bot-3
parent e7a4b20d75
commit e2c35dddba
5 changed files with 107 additions and 29 deletions

View File

@@ -9,6 +9,7 @@
#import "RCTPicker.h"
#import "RCTConvert.h"
#import "RCTUtils.h"
@interface RCTPicker() <UIPickerViewDataSource, UIPickerViewDelegate>
@@ -19,7 +20,10 @@
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_color = [UIColor blackColor];
_font = [UIFont systemFontOfSize:21]; // TODO: selected title default should be 23.5
_selectedIndex = NSNotFound;
_textAlign = NSTextAlignmentCenter;
self.delegate = self;
}
return self;
@@ -59,20 +63,33 @@ numberOfRowsInComponent:(__unused NSInteger)component
#pragma mark - UIPickerViewDelegate methods
- (NSDictionary *)itemForRow:(NSInteger)row
{
return _items[row];
}
- (id)valueForRow:(NSInteger)row
{
return [self itemForRow:row][@"value"];
}
- (NSString *)pickerView:(__unused UIPickerView *)pickerView
titleForRow:(NSInteger)row forComponent:(__unused NSInteger)component
titleForRow:(NSInteger)row
forComponent:(__unused NSInteger)component
{
return [self itemForRow:row][@"label"];
return [RCTConvert NSString:_items[row][@"label"]];
}
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UILabel *)label
{
if (!label) {
label = [[UILabel alloc] initWithFrame:(CGRect){
CGPointZero,
{
[pickerView rowSizeForComponent:component].width,
[pickerView rowSizeForComponent:component].height,
}
}];
}
label.font = _font;
label.textColor = _color;
label.textAlignment = _textAlign;
label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
return label;
}
- (void)pickerView:(__unused UIPickerView *)pickerView
@@ -82,7 +99,7 @@ numberOfRowsInComponent:(__unused NSInteger)component
if (_onChange) {
_onChange(@{
@"newIndex": @(row),
@"newValue": [self valueForRow:row]
@"newValue": RCTNullIfNil(_items[row][@"value"]),
});
}
}