2016-11-03 6 views
1

私のアプリでは、ユーザーにアプリ全体のフォントを変更するオプションを与えました。そうするために、私はオプションのリスト(太字、半綴じ、普通、薄いなど)をユーザに与えています。ユーザーが上記のオプションのいずれかを選択するとすぐに、その値をNSUserDefaultsに保存します。プログラムで完全なios appフォントを変更する方法

ユーザーが自分のオプションを選択したので、アプリのフォント全体を変更する必要があります。同じものについてはカテゴリを作成しました

#import "UILabel+Helper.h" 

@implementation UILabel (Helper) 

- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR { 

if ([name isEqual: kAppFontSFTextBold]) { 
    self.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; 
} 
else if ([name isEqual:kAppFontSFTextSemiBold]) { 
    self.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]; 
} 
else if ([name isEqual: kAppFontSFTextMedium]) { 
    self.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 
} 
else if ([name isEqual: kAppFontSFTextRegular]) { 
    self.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]; 
} 
else if ([name isEqual: kAppFontSFTextLight]) { 
    self.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]; 
} 

// self.font = [UIFont fontWithName:name size:self.font.pointSize]; 
} 

@end 

私のアプリでこれらの変更を行うには、プログラムを再起動する必要があり、Appleはそれをサポートしていません。いくつかの回答を読んだ後、私は強制的にexit(0)を使ってアプリケーションを終了できることを知りました。

ユーザーがこのオプションを選択するとすぐに、私は前述のように選択した値をローカルに保存して終了(0)します。今すぐユーザーは、独自のアプリケーションを再起動する必要があります。

このアプローチは正しいかどうかですか。そうでない場合は、より良い解決策を提案してください。

+2

出口は(0)あなたのアプリケーションの拒絶からにつながる見ます林檎 –

答えて

0

以下のことができます。ユーザーがフォントを選択すると、単にNSNotificationを発射することができます。 UIViewControllerにオブザーバを追加することができます。ここにはUILabelがあります。次に、その方法でフォントをUILabelに変更するだけです。

0

次のコードを使用すると、目標を達成できます。

1. -(void)setFonts{ 
     NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"langkey"]; ; 
     UIFont *font ; 
     if ([name isEqual: kAppFontSFTextBold]) { 
      font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; 
     } 
     else if ([name isEqual:kAppFontSFTextSemiBold]) { 
      font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]; 
     } 
     else if ([name isEqual: kAppFontSFTextMedium]) { 
      font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 
     } 
     else if ([name isEqual: kAppFontSFTextRegular]) { 
      font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]; 
     } 
     else if ([name isEqual: kAppFontSFTextLight]) { 
      font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]; 
     } 

     [[UILabel appearance] setFont:font]; 

     [[UITextField appearance] setFont:font]; 

     [[UITextView appearance] setFont:font]; 
    } 

この方法では、appdelegateに書き込み、create appdelegateのインスタンスを使用して呼び出すことができます。

これはユーザー定義のメソッドなので、ユーザーの選択中に呼び出す必要があります。

2

viewDidLoadの終わりまで各VCの通知を下記に登録してください:。すべてのVCまたはVCカテゴリの基本クラスに追加するとよいでしょう。

[[NSNotificationCenter defaultCenter] 
          addObserver:self 
          selector:@selector(preferredContentSizeChanged:) 
           name:UIContentSizeCategoryDidChangeNotification 
           object:nil]; 

そして、セレクタ方式では、以下の方法を使用してフォントを更新します。詳細については

- (void)preferredContentSizeChanged:(NSNotification *)notification { 
    self.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 
} 

Core Text

0
//Hope this helps : 

    AnyUIView.appearance().font = UIFont(name: "yourFont", size: yourSize) 

//code this inside AppDelegate in application:didFinishLaunchingWithOptions: method. and also , Be sure to add your chosen font to your projects target if you're using a custom font. 
関連する問題