我正在开发一个 ios 应用程序,其中我必须为 UI 使用自定义字体。
我知道如何在应用程序中集成新的自定义字体。为此我有
- 下载扩展名为 .ttf 的字体系列文件。
- 将它们添加到资源包中。
- 在 info.plist 文件中添加应用程序提供的关键字体。
这样自定义字体就显示效果了。但我想做的是,我想将它们设置为系统字体。所以我不必在所有 UI 元素中设置它们。
我想要类似的东西
[[UIApplication sharedApplication] systemFont:@"Arial"];
这可能吗?谁能帮我这个?
我在为 iOS 7 更新 iOS 应用程序时遇到了同样的问题。我想为整个应用程序设置自定义字体,即使对于控件,您也不允许自定义其字体(例如选择器)。
经过在网络和 Twitter 上进行一些研究后,我使用 Method Swizzling 解决了这个问题,这是一种交换方法实现的实践。
注意:如果不小心使用,此方法可能会很危险!阅读关于 SO 的讨论:方法混合的危险
然而,这是要做的:
- 创建一个UIFont类别,如UIFont+CustomSystemFont。
- Import
<objc/runtime.h>
在你的 .m 文件中。
- 保留 .h 文件不变并将以下代码添加到 .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];
}
// 方法调配
+(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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)