// // CLFontPickerView.m // // Created by sho yakushiji on 2013/12/14. // Copyright (c) 2013年 CALACULU. All rights reserved. // #import "CLFontPickerView.h" #import "UIView+Frame.h" #import "CLPickerView.h" const CGFloat kCLFontPickerViewConstantFontSize = 14; @interface CLFontPickerView() @end @implementation CLFontPickerView { CLPickerView *_pickerView; } + (NSArray*)allFontList { NSMutableArray *list = [NSMutableArray array]; for(NSString *familyName in [UIFont familyNames]){ for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]){ [list addObject:[UIFont fontWithName:fontName size:kCLFontPickerViewConstantFontSize]]; } } return [list sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"fontName" ascending:YES]]]; } + (NSArray*)defaultSizes { return @[@8, @10, @12, @14, @16, @18, @20, @24, @28, @32, @38, @44, @50]; } + (UIFont*)defaultFont { return [UIFont fontWithName:@"HiraKakuProN-W3"size:kCLFontPickerViewConstantFontSize]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.clipsToBounds = YES; _pickerView = [[CLPickerView alloc] initWithFrame:self.bounds]; _pickerView.center = CGPointMake(self.width/2, self.height/2); _pickerView.backgroundColor = [UIColor clearColor]; _pickerView.dataSource = self; _pickerView.delegate = self; [self addSubview:_pickerView]; self.fontList = [self.class allFontList]; self.fontSizes = [self.class defaultSizes]; self.font = [self.class defaultFont]; self.foregroundColor = [UIColor colorWithWhite:1 alpha:0.8]; self.textColor = [UIColor blackColor]; } return self; } - (void)setForegroundColor:(UIColor *)foregroundColor { _pickerView.foregroundColor = foregroundColor; } - (UIColor*)foregroundColor { return _pickerView.foregroundColor; } - (void)setFontList:(NSArray *)fontList { if(fontList != _fontList){ _fontList = fontList; [_pickerView reloadComponent:0]; } } - (void)setFontSizes:(NSArray *)fontSizes { if(fontSizes != _fontSizes){ _fontSizes = fontSizes; [_pickerView reloadComponent:1]; } } - (void)setFont:(UIFont *)font { UIFont *tmp = [font fontWithSize:kCLFontPickerViewConstantFontSize]; NSInteger fontIndex = [self.fontList indexOfObject:tmp]; if(fontIndex==NSNotFound){ fontIndex = 0; } NSInteger sizeIndex = 0; for(sizeIndex=0; sizeIndex0){ lbl.text = self.text; } else{ lbl.text = [NSString stringWithFormat:@"%@", lbl.font.fontName]; } break; case 1: lbl.font = [UIFont systemFontOfSize:kCLFontPickerViewConstantFontSize]; lbl.text = [NSString stringWithFormat:@"%@", self.fontSizes[row]]; break; default: break; } return lbl; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if([self.delegate respondsToSelector:@selector(fontPickerView:didSelectFont:)]){ [self.delegate fontPickerView:self didSelectFont:self.font]; } } @end