UILabel appearance proxy. setFont deprecated

今日も元気にはま寿C!!

やりたいこと→アプリ全体のフォントを統一したい

どうやら調べてみるとiOS5からはappearance proxyっていうのがあるらしい

http://stackoverflow.com/questions/8707082/set-a-default-font-for-whole-ios-app

ふむふむ

[cpp]
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17.0]];
[/cpp]

みたいに書けばいいよ~とは書いてあるんだけど、どこに書くの??いや、当然

[cpp]
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
[/cpp]

っしょ、みたいな。。。とりあえず、試してみる(←これ大事)

[cpp]
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

// URL REF: http://stackoverflow.com/questions/8707082/set-a-default-font-for-whole-ios-app
[[UILabel appearance] setFont:[UIFont fontWithName:@"Meiryo" size:16.0f]];

return YES;
}
[/cpp]

変わらないんけどー。チッ

さらに調べてみるとsetFontはdeprecatedだから~っていうのとappearance proxyを使えるのはUI_APPEARANCE_SELECTORが付けられたメソッドだけだから!っていうことを知る
(UI_APPEARANCE_SELECTORがついているかどうかはUILabel.h等のソースコードまで読め!!って事らしいです。ぷっ)

↑もはや同じ道を辿った人にしか理解できない文章になっている。でもいい、それだけのために存在しているので

なるほどなるほど

http://stackoverflow.com/questions/11308379/uiapperance-not-taking-effect-on-uilabels-created-programatically

だから?もっと具体的な解決法教えて!と思いながらさらにコメントを見ていくと、”こんなメソッド作ってUILabelのカテゴリーで実装すればいいんだよ~”ってのが書いてある。ふむふむ

カテゴリーね。なんかObjective-Cの入門書にチラっと紹介されてたな。Objective-C言語特有の拡張仕様だったな、たしか。なんか雰囲気的にJavascriptのprototypeっぽそう。まぁ、詳しくはObjective-C プログラミング言語(PDF)あたりを読むなりグーグル先生に教えてもらうなりしてくれ

そしていつもハマるのが、このコードの断片。どこに置くんだよ!!そこまで丁寧に教えてくれるとstackoverflow最高~って思えるんだけど。とりあえず、カテゴリーの定義ファイルの命名規則はクラス+カテゴリって感じらしい

[cpp]

@interface UILabel (UISS)
– (void)setTextAttributes:(NSDictionary *)numberTextAttributes UI_APPEARANCE_SELECTOR;
@end

@implementation UILabel (UISS)
– (void)setTextAttributes:(NSDictionary *)numberTextAttributes;
{
UIFont *font = [numberTextAttributes objectForKey:UITextAttributeFont];
if (font) {
self.font = font;
}
UIColor *textColor = [numberTextAttributes objectForKey:UITextAttributeTextColor];
if (textColor) {
self.textColor = textColor;
}
UIColor *textShadowColor = [numberTextAttributes objectForKey:UITextAttributeTextShadowColor];
if (textShadowColor) {
self.shadowColor = textShadowColor;
}
NSValue *shadowOffsetValue = [numberTextAttributes objectForKey:UITextAttributeTextShadowOffset];
if (shadowOffsetValue) {
UIOffset shadowOffset = [shadowOffsetValue UIOffsetValue];
self.shadowOffset = CGSizeMake(shadowOffset.horizontal, shadowOffset.vertical);
}
}
@end

[/cpp]

なのでこれをきっとUILabel+UISS.h、UILabel+UISS.mというファイルに書けばいいのだな

あとはsetFontの変わりに、これを使えば、上手くいくはず、、、、上手く行ってなかったらこのブログの投稿も存在しないだろう、とキレの良いみなさんは既に気づいているはずですね

[cpp]
#import "UILabel+UISS.h"

@implementation AppDelegate

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

[[UILabel appearance] setTextAttributes:[NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Meiryo" size:16.0f] forKey:UITextAttributeFont]];

return YES;
}
[/cpp]

恐る恐る⌘-Rしてみると、、、変わったー!!!

と、分かりきった結果ではありますが、これで無事アプリ内のUILabelが全てメイリオ(Meiryo)フォントになりました~


投稿日

カテゴリー:

投稿者:

タグ:

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です