iOSのフォントを一覧で表示して見比べるためのツール
(XcodeのiOS6 Simulatorで試しています。)
ポイント
・UITextFieldに入力した文字をiOSにプリセットされた全フォントで表示
・Font familyは、 UIFontのfamilyNames
・Font nameは、UIFontのfontNamesForFamilyName
サンプルコード
#import “ViewController.h”
@interface ViewController () <UITextFieldDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UITextField *field;
@end
@implementation ViewController
@synthesize scroll, field;
– (void)viewDidLoad
{
[super viewDidLoad];
// スクロールVeiw
self.scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
self.scroll.scrollEnabled = YES;
self.scroll.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.scroll];
// フォント一覧を表示
[self showFonts];
// 文字入力スペース作成
self.field = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
self.field.placeholder = @”text here”;
self.field.borderStyle = UITextBorderStyleRoundedRect;
self.field.delegate = self;
[self.view addSubview:self.field];
}
#define LabelTypeFontName 1
– (void)showFonts
{
// タイトル
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 320, 50)];
title.font = [UIFont fontWithName:@”Chalkduster” size:35];
title.text = @”iOS font list”;
[self.scroll addSubview:title];
// フォントリスト
NSArray *familyNames = [UIFont familyNames];
int x = 20;
int y = 100;
for(NSString *familyName in familyNames ){
y += 5;
UILabel *familyLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 320, 30)];
familyLabel.font = [UIFont fontWithName:familyName size:20];
familyLabel.text = familyName;
familyLabel.backgroundColor = [UIColor clearColor];
[self.scroll addSubview:familyLabel];
y += 30;
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for( NSString *fontName in fontNames ){
UILabel *fontLabel = [[UILabel alloc] initWithFrame:CGRectMake(x + 30, y, 320, 20)];
fontLabel.font = [UIFont fontWithName:fontName size:15];
fontLabel.text = fontName;
fontLabel.backgroundColor = [UIColor clearColor];
fontLabel.tag = LabelTypeFontName;
[self.scroll addSubview:fontLabel];
y += 20;
}
}
self.scroll.contentSize = CGSizeMake(320, y + 100);
}
– (void)changeFontText:(NSString*)str
{
for (UIView *v in self.scroll.subviews) {
if (v.tag == LabelTypeFontName) {
UILabel *fontLabel = (UILabel*)v;
if ([str length] == 0) {
fontLabel.text = fontLabel.font.fontName;
} else {
fontLabel.text = str;
}
}
}
}
– (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[self changeFontText:textField.text];
return NO;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end