更新:支持 iOS5 和 iOS7。感谢 mave99a 和 chintan100!
FKWFontPicker 是一个使用 UIPickerView 的字体选择控件。
KWFontPicker *fontPicker = [[KWFontPicker alloc] init];
[fontPicker setChangeHandler:^{
NSLog(@"fontName=%@ pointSize=%.1f", fontPicker.font.fontName, fontPicker.font.pointSize);
}];
[self.view addSubview:fontPicker];
下面的 KWTextEditor 将帮助您在大多数情况下使用选择器。
KWTextEditor 在 KWFontPicker 或 iOS 的软件键盘上提供了一个带有 [键盘]、[字体] 和 [关闭] 按钮的工具栏。
KWTextEditor* textEditor = [[KWTextEditor alloc] initWithTextView:textView];
[textEditor setScrollView:self.scrollView];
[textEditor showInView:self.view];
可用的字体名称在 KWFontPicker 的左侧显示。iOS 中的所有字体默认列出。您可以使用 NSArray 指定名称
textEditor.fontPicker.fontList = @[ @"AmericanTypewriter", @"Baskerville",
@"Copperplate", @"Didot", @"EuphemiaUCAS", @"Futura-Medium", @"GillSans",
@"Helvetica", @"Marion-Regular", @"Optima-Regular", @"Palatino-Roman",
@"TimesNewRomanPSMT", @"Verdana"];
可用的字体大小在 KWFontPicker 的中心显示。您可以通过范围
textEditor.fontPicker.minFontSize = 10;
textEditor.fontPicker.maxFontSize = 30;
textEditor.fontPicker.stepFontSize = 4;
或通过 NSArray 指定大小
textEditor.fontPicker.sizeList = @[ @9.5, @13.5, @17.5, @21.5, @25.5 ];
可用的字体颜色在 KWFontPicker 的右侧显示。默认列出 125 种颜色,意味着 RGB 的 5 个级别。您可以指定颜色范围
textEditor.fontPicker.colorVariants = KWFontPickerColorVariantsNone; // no colors. use grayscales
textEditor.fontPicker.colorVariants = KWFontPickerColorVariants222; // 8 colors
textEditor.fontPicker.colorVariants = KWFontPickerColorVariants333; // 27 colors
textEditor.fontPicker.colorVariants = KWFontPickerColorVariants444; // 64 colors
textEditor.fontPicker.colorVariants = KWFontPickerColorVariants555; // 125 colors = default
textEditor.fontPicker.colorVariants = KWFontPickerColorVariants666; // 216 colors = web safe color
textEditor.fontPicker.grayVariants = 2; // black and white
textEditor.fontPicker.grayVariants = 16; // 16 gradients of grayscale
或通过 NSArray 指定大小
textEditor.fontPicker.colorList = @[
[UIColor blackColor], [UIColor grayColor], [UIColor whiteColor],
[UIColor redColor], [UIColor yellowColor], [UIColor greenColor],
[UIColor cyanColor], [UIColor blueColor], [UIColor purpleColor]];
以下处理程序块可用。
[textEditor setTextDidChangeHandler:^{
NSLog(@"TextDidChangeHandler: text=%@", textView.text);
}];
[textEditor setFontDidChangeHandler:^{
NSLog(@"fontDidChangeHandler: fontName=%@ pointSize=%.1f", textView.font.fontName, textView.font.pointSize);
}];
[textEditor setEditorDidShowHandler:^{
NSString *mode = @"";
if (textEditor.editorMode == KWTextEditorModeKeyboard) mode = @"keyboard";
if (textEditor.editorMode == KWTextEditorModeFontPicker) mode = @"font picker";
NSLog(@"editorDidShowHandler: %@", mode);
}];
[textEditor setEditorDidHideHandler:^{
NSLog(@"editorDidHideHandler");
}];
[textEditor setCloseButtonDidTapHandler:^{
NSLog(@"closeButtonDidTapHandler");
}];
textEditor.keyboardButton.title = @"TEXT";
textEditor.fontButton.title = @"FONT";
textEditor.closeButton.title = @"DONE";
以下版权声明适用于此分布中提供的所有文件,包括二进制文件,除非明确指出。
Copyright 2012-2013 Yusuke Kawasaki