Skip to content

Commit

Permalink
make font of picker label ready for customize.
Browse files Browse the repository at this point in the history
  • Loading branch information
ainopara committed Oct 7, 2015
1 parent ad8364f commit 5b311fb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ - (IBAction)customButtons:(id)sender {
- (IBAction)customBackgroundAndText:(id)sender {
ActionSheetStringPicker *picker = [[ActionSheetStringPicker alloc] initWithTitle:@"" rows:@[@"choiceA", @"choiceB", @"choiceC"] initialSelection:1 doneBlock:nil cancelBlock:nil origin:sender];
picker.pickerBackgroundColor = [UIColor blackColor];
NSMutableParagraphStyle *labelParagraphStyle = [[NSMutableParagraphStyle alloc] init];
labelParagraphStyle.alignment = NSTextAlignmentCenter;
picker.pickerTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
NSParagraphStyleAttributeName: labelParagraphStyle,
NSFontAttributeName:[self getRandomFont],};
[picker showActionSheetPicker];
}
Expand All @@ -262,6 +265,9 @@ - (UIFont *)getRandomFont
NSArray *familyNames = [UIFont familyNames];
NSString *familyName = familyNames[arc4random() % [familyNames count]];
NSArray *namesForFamilyName = [UIFont fontNamesForFamilyName:familyName];
if ([namesForFamilyName count] == 0) {
return [self getRandomFont];
}
NSString *fontName = namesForFamilyName[arc4random() % [namesForFamilyName count]];
UIFont *randomFont = [UIFont fontWithName:fontName size:[UIFont systemFontSize]];
return randomFont;
Expand Down
2 changes: 1 addition & 1 deletion Pickers/AbstractActionSheetPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static NSString *const kActionTarget = @"buttonActionTarget";
@property (nonatomic, assign) CGRect presentFromRect;
@property (nonatomic) NSDictionary *titleTextAttributes; // default is nil. Used to specify Title Label attributes.
@property (nonatomic) NSAttributedString *attributedTitle; // default is nil. If titleTextAttributes not nil this value ignored.
@property (nonatomic) NSDictionary *pickerTextAttributes; // default is nil. Used to specify Picker Label attributes.
@property (nonatomic) NSDictionary *pickerTextAttributes; // default with a NSMutableParagraphStyle to set label align center. Used to specify Picker Label attributes.
@property (nonatomic) UIColor *pickerBackgroundColor;
@property (nonatomic, retain) Class popoverBackgroundViewClass; //allow popover customization on iPad
@property (nonatomic) UIInterfaceOrientationMask supportedInterfaceOrientations; // You can set your own supportedInterfaceOrientations value to prevent dismissing picker in some special cases.
Expand Down
4 changes: 4 additions & 0 deletions Pickers/AbstractActionSheetPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ - (instancetype)init {
self.tapDismissAction = TapActionNone;
//allows us to use this without needing to store a reference in calling class
self.selfReference = self;

NSMutableParagraphStyle *labelParagraphStyle = [[NSMutableParagraphStyle alloc] init];
labelParagraphStyle.alignment = NSTextAlignmentCenter;
self.pickerTextAttributes = @{NSParagraphStyleAttributeName: labelParagraphStyle};
}

return self;
Expand Down
24 changes: 24 additions & 0 deletions Pickers/ActionSheetStringPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@ - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleFor
return nil;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
id obj = (self.data)[(NSUInteger) row];
UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
pickerLabel = [[UILabel alloc] init];
}
NSAttributedString *attributeTitle = nil;
// return the object if it is already a NSString,
// otherwise, return the description, just like the toString() method in Java
// else, return nil to prevent exception

if ([obj isKindOfClass:[NSString class]])
attributeTitle = [[NSAttributedString alloc] initWithString:obj attributes:self.pickerTextAttributes];

if ([obj respondsToSelector:@selector(description)])
attributeTitle = [[NSAttributedString alloc] initWithString:[obj performSelector:@selector(description)] attributes:self.pickerTextAttributes];

if (attributeTitle != nil) {
pickerLabel.attributedText = attributeTitle;
return pickerLabel;
}
return nil;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return pickerView.frame.size.width - 30;
}
Expand Down

0 comments on commit 5b311fb

Please sign in to comment.