如何在ios应用程序中将自定义字体系列设置为系统字体 [英] How to set custom font family as system font in ios application

查看:338
本文介绍了如何在ios应用程序中将自定义字体系列设置为系统字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个ios应用程序,在这个应用程序中我必须使用用户界面的自定义字体。
我知道如何在应用程序中集成新的自定义字体。为此,我有


  1. 下载带.ttf扩展名的字体系列文件。
  2. 将它们添加到资源
  3. 在info.plist文件中添加应用程序提供的键字体。

自定义字体显示效果。但是我想要做的是,我想把它们设置为systemFont。所以我不必在所有UI元素中设置它们。

我想要类似于

  [[UIApplication sharedApplication] systemFont:@Arial]; 

这可能吗?任何人都可以帮助我吗?

解决方案

为整个应用程序设置一个自定义的字体,即使是不允许自定义字体的控件(例如,选择器)。
在网络和Twitter上的一些研究之后,我解决了使用Method Swizzling是交换方法实现的一个实践。注意:如果不小心使用,这种方法可能是危险的!阅读关于这个讨论:方法Swizzling的危险然而,这是做什么的:


  1. 创建一个UIFont类别,如UIFont + CustomSystemFont。

  2. 在.m文件中导入< objc / runtime.h>
  3. 将.h文件保留为未修改状态,并将此代码添加到.m中:




<$ (UIFont *)regularFontWithSize:(CGFloat)size
{
return [UIFont fontWithName:@Your Font Name Heresize:size];

$ b +(UIFont *)boldFontWithSize:(CGFloat)size
{
return [UIFont fontWithName:@Your Bold Font Name Heresize:size] ;

b




$ b $ p $ // $方法Swizzling

  +(void)load 
{
SEL original = @selector(systemFontOfSize :);
SEL modified = @selector(regularFontWithSize :);
SEL originalBold = @selector(boldSystemFontOfSize :);
SEL modifiedBold = @selector(boldFontWithSize :);

方法originalMethod = class_getClassMethod(self,original);
Method modifiedMethod = class_getClassMethod(self,modified);
method_exchangeImplementations(originalMethod,modifiedMethod);

方法originalBoldMethod = class_getClassMethod(self,originalBold);
方法modifiedBoldMethod = class_getClassMethod(self,modifiedBold);
method_exchangeImplementations(originalBoldMethod,modifiedBoldMethod);
}


I am working on an ios application in which i have to use custom fonts for UIs. I know how to integrate new custom fonts in application. For that i have

  1. Download font family files with .ttf extention.
  2. Add them to resource bundle.
  3. In info.plist file add with key Fonts provided by application.

This custom fonts shows effect. But what i want to do is, I want to set them as a systemFont. So i do not have to set them in all UI elements.

I want something like

[[UIApplication sharedApplication] systemFont:@"Arial"];

Is this possible? Can any one help me with this?

解决方案

I had your same problem when updating my iOS app for iOS 7. I wanted to set a custom font for the entire application, even for controls you are not allowed to customize their font (e.g. pickers).
After a bit of research on the web and on Twitter, I solved using Method Swizzling which is a practice that consists in exchanging method implementations.

NOTE: This method might be dangerous if not used carefully! Read this discussion on SO: Dangers of Method Swizzling

However, this is what to do:

  1. Create a UIFont category, like UIFont+CustomSystemFont.
  2. Import <objc/runtime.h> in your .m file.
  3. Leave .h file unmodified and add this code to the .m:

+(UIFont *)regularFontWithSize:(CGFloat)size
{
  return [UIFont fontWithName:@"Your Font Name Here" size:size];
}

+(UIFont *)boldFontWithSize:(CGFloat)size
{
  return [UIFont fontWithName:@"Your Bold Font Name Here" size:size];
}

// Method Swizzling

+(void)load
{
    SEL original = @selector(systemFontOfSize:);
    SEL modified = @selector(regularFontWithSize:);
    SEL originalBold = @selector(boldSystemFontOfSize:);
    SEL modifiedBold = @selector(boldFontWithSize:);

    Method originalMethod = class_getClassMethod(self, original);
    Method modifiedMethod = class_getClassMethod(self, modified);
    method_exchangeImplementations(originalMethod, modifiedMethod);

    Method originalBoldMethod = class_getClassMethod(self, originalBold);
    Method modifiedBoldMethod = class_getClassMethod(self, modifiedBold);
    method_exchangeImplementations(originalBoldMethod, modifiedBoldMethod);
}

这篇关于如何在ios应用程序中将自定义字体系列设置为系统字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆