【问题标题】:How to change full ios app font programmatically如何以编程方式更改完整的 ios 应用程序字体
【发布时间】:2016-11-03 05:47:55
【问题描述】:

在我的应用程序中,我为用户提供了更改整个应用程序字体的选项。为此,我为用户提供了一个选项列表(如粗体、半粗体、常规、细等)。一旦用户选择了上述任何选项,我就会将该值保存在 NSUserDefaults 中。

现在用户已经选择了他的选项,所以我应该更改整个应用程序的字体。同样,我创建了一个类别

#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

但要在我的应用程序中进行这些更改,我必须以编程方式重新启动我的应用程序,而苹果不支持。在阅读了几个答案后,我知道我可以使用 exit(0) 强制终止我的应用程序。

现在,一旦用户选择该选项,我就会如前所述将所选值保存在本地并退出(0)。现在用户必须自行重启应用。

这种方法是否正确。如果没有,请建议我提供更好的解决方案。

【问题讨论】:

  • exit(0) 将导致您的应用被 Apple 拒绝

标签: ios objective-c fonts uilabel objective-c-category


【解决方案1】:

在每个 VC 的通知下方注册到 viewDidLoad: 的末尾。最好将其添加到所有 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

【讨论】:

    【解决方案2】:

    使用下面的代码你可以实现你的目标。

    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 的实例调用。

    这是用户定义的方法,因此您需要在用户选择发生时调用。

    【讨论】:

      【解决方案3】:

      您可以执行以下操作。您会看到,当用户选择字体时,您可以简单地触发 NSNotification。您可以在每个有UILabels 的UIViewController 中添加一个观察者。然后,您可以在该方法中简单地更改那些UILabel 字体。

      【讨论】:

        【解决方案4】:
            //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.
        

        【讨论】:

          猜你喜欢
          • 2012-09-24
          • 2020-12-31
          • 2022-12-03
          • 2017-08-31
          • 2016-01-28
          • 2016-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多