我有一个自定义字体,我希望将其用于在应用程序,标签,文本视图等中显示文本的所有内容。
有没有办法为整个应用程序设置默认字体(默认情况下,标签使用SystemFont)?
IBDesignable
这些天您肯定必须使用。希望能帮助到你。还请考虑以下有趣的质量检查:stackoverflow.com/a/38000466/294884
我有一个自定义字体,我希望将其用于在应用程序,标签,文本视图等中显示文本的所有内容。
有没有办法为整个应用程序设置默认字体(默认情况下,标签使用SystemFont)?
IBDesignable
这些天您肯定必须使用。希望能帮助到你。还请考虑以下有趣的质量检查:stackoverflow.com/a/38000466/294884
Answers:
在iOS 5中,使用UIAppearance代理似乎是可能的。
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17.0]];
这会将字体设置为应用程序中所有UILabel的自定义字体。您需要为每个控件(UIButton,UILabel等)重复该操作。
请记住,您需要将UIAppFonts值放在info.plist中,并包括要包含的字体的名称。
setFont:
方法已弃用
UILabel
。不UIButton
建议使用它,但它使用titleLabel
属性的字体代替UILabel
,所以只需使用外观代理设置字体就UILabel
可以了。
基于FábioOliveira的答案(https://stackoverflow.com/a/23042694/2082851),我做出了自己的迅速4。
总之,这个扩展交流的默认功能init(coder:)
,systemFont(ofSize:)
,boldSystemFont(ofSize:)
,italicSystemFont(ofSize:)
与我的自定义方法。
请注意,它尚未完全实现,但是您可以根据我的实现交换更多方法。
import UIKit
struct AppFontName {
static let regular = "CourierNewPSMT"
static let bold = "CourierNewPS-BoldMT"
static let italic = "CourierNewPS-ItalicMT"
}
extension UIFontDescriptor.AttributeName {
static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
}
extension UIFont {
static var isOverrided: Bool = false
@objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.regular, size: size)!
}
@objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.bold, size: size)!
}
@objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.italic, size: size)!
}
@objc convenience init(myCoder aDecoder: NSCoder) {
guard
let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
self.init(myCoder: aDecoder)
return
}
var fontName = ""
switch fontAttribute {
case "CTFontRegularUsage":
fontName = AppFontName.regular
case "CTFontEmphasizedUsage", "CTFontBoldUsage":
fontName = AppFontName.bold
case "CTFontObliqueUsage":
fontName = AppFontName.italic
default:
fontName = AppFontName.regular
}
self.init(name: fontName, size: fontDescriptor.pointSize)!
}
class func overrideInitialize() {
guard self == UIFont.self, !isOverrided else { return }
// Avoid method swizzling run twice and revert to original initialize function
isOverrided = true
if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
}
if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
}
if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
}
if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))), // Trick to get over the lack of UIFont.init(coder:))
let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
}
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
// Avoid warning of Swift
// Method 'initialize()' defines Objective-C class method 'initialize', which is not guaranteed to be invoked by Swift and will be disallowed in future versions
override init() {
super.init()
UIFont.overrideInitialize()
}
...
}
if let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String {
帮我吧。
UILabel
用于设置文本样式(粗体,标题,标题等)的s ...仅适用于具有特定大小和系统字体集的字体。@ nahung89
还有另一个解决方案将是覆盖systemFont。
只需创建一个类别
UIFont + SystemFontOverride.h
#import <UIKit/UIKit.h>
@interface UIFont (SystemFontOverride)
@end
UIFont + SystemFontOverride.m
@implementation UIFont (SystemFontOverride)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"fontName" size:fontSize];
}
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"fontName" size:fontSize];
}
#pragma clang diagnostic pop
@end
这将替换默认实现,并且大多数UIControls使用systemFont。
如果您使用的是Swift,则可以创建UILabel扩展:
extension UILabel {
@objc var substituteFontName : String {
get { return self.font.fontName }
set { self.font = UIFont(name: newValue, size: self.font.pointSize) }
}
}
然后在哪里进行外观代理:
UILabel.appearance().substituteFontName = applicationFont
UI_APPEARANCE_SELECTOR
在名称为的属性上使用了等效的Objective-C代码substituteFontName
。
加成
对于要分别设置粗体和常规字体的情况:
extension UILabel {
@objc var substituteFontName : String {
get { return self.font.fontName }
set {
if self.font.fontName.range(of:"Medium") == nil {
self.font = UIFont(name: newValue, size: self.font.pointSize)
}
}
}
@objc var substituteFontNameBold : String {
get { return self.font.fontName }
set {
if self.font.fontName.range(of:"Medium") != nil {
self.font = UIFont(name: newValue, size: self.font.pointSize)
}
}
}
}
然后为您的UIAppearance代理:
UILabel.appearance().substituteFontName = applicationFont
UILabel.appearance().substituteFontNameBold = applicationFontBold
注意:如果发现粗体替换无效,则默认字体名称可能不包含“中”。根据需要将字符串切换为另一个匹配项(感谢下面的注释中的Mason)。
UILabel
扩展名以使用其他字体名称来显示粗体。然后,set
在字体名称中检查“粗体”是否存在,在一种情况下忽略该设置,在另一种情况下使用它。我将编辑并添加一个示例。
rangeOfString("Medium")
,并且有效。
从Hugues BR答案开发,但是使用方法混淆,我已经找到了一种解决方案,可以成功将所有字体更改为我的应用程序中所需的字体。
您应该在iOS 7上使用动态类型方法。以下解决方案未使用动态类型。
笔记:
- initWithCoder:
覆盖。但是,这并不涵盖所有情况。该解决方案使用两种不同的方法来获得最终结果。第一个是重写UIFont类方法,+ systemFontWithSize:
并且与使用我的替代方法的方法类似(此处使用“ Zapfino”毫无疑问,替换成功)。
另一种方法是重写- initWithCoder:
UIFont上的方法,以用CTFontRegularUsage
我的替代方法替换出现的任何类似事件。最后一种方法是必需的,因为我发现UILabel
用NIB文件编码的对象不会检查+ systemFontWithSize:
获取其系统字体的方法,而是将其编码为UICTFontDescriptor
对象。我尝试覆盖- awakeAfterUsingCoder:
它,但不知何故它被情节提要中的每个编码对象调用,并导致崩溃。覆盖- awakeFromNib
不允许我读取NSCoder
对象。
#import <objc/runtime.h>
NSString *const FORegularFontName = @"Zapfino";
NSString *const FOBoldFontName = @"Zapfino";
NSString *const FOItalicFontName = @"Zapfino";
#pragma mark - UIFont category
@implementation UIFont (CustomFonts)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (void)replaceClassSelector:(SEL)originalSelector withSelector:(SEL)modifiedSelector {
Method originalMethod = class_getClassMethod(self, originalSelector);
Method modifiedMethod = class_getClassMethod(self, modifiedSelector);
method_exchangeImplementations(originalMethod, modifiedMethod);
}
+ (void)replaceInstanceSelector:(SEL)originalSelector withSelector:(SEL)modifiedSelector {
Method originalDecoderMethod = class_getInstanceMethod(self, originalSelector);
Method modifiedDecoderMethod = class_getInstanceMethod(self, modifiedSelector);
method_exchangeImplementations(originalDecoderMethod, modifiedDecoderMethod);
}
+ (UIFont *)regularFontWithSize:(CGFloat)size
{
return [UIFont fontWithName:FORegularFontName size:size];
}
+ (UIFont *)boldFontWithSize:(CGFloat)size
{
return [UIFont fontWithName:FOBoldFontName size:size];
}
+ (UIFont *)italicFontOfSize:(CGFloat)fontSize
{
return [UIFont fontWithName:FOItalicFontName size:fontSize];
}
- (id)initCustomWithCoder:(NSCoder *)aDecoder {
BOOL result = [aDecoder containsValueForKey:@"UIFontDescriptor"];
if (result) {
UIFontDescriptor *descriptor = [aDecoder decodeObjectForKey:@"UIFontDescriptor"];
NSString *fontName;
if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontRegularUsage"]) {
fontName = FORegularFontName;
}
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontEmphasizedUsage"]) {
fontName = FOBoldFontName;
}
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontObliqueUsage"]) {
fontName = FOItalicFontName;
}
else {
fontName = descriptor.fontAttributes[@"NSFontNameAttribute"];
}
return [UIFont fontWithName:fontName size:descriptor.pointSize];
}
self = [self initCustomWithCoder:aDecoder];
return self;
}
+ (void)load
{
[self replaceClassSelector:@selector(systemFontOfSize:) withSelector:@selector(regularFontWithSize:)];
[self replaceClassSelector:@selector(boldSystemFontOfSize:) withSelector:@selector(boldFontWithSize:)];
[self replaceClassSelector:@selector(italicSystemFontOfSize:) withSelector:@selector(italicFontOfSize:)];
[self replaceInstanceSelector:@selector(initWithCoder:) withSelector:@selector(initCustomWithCoder:)];
}
#pragma clang diagnostic pop
@end
UIAlertController
)不会更改字体。
为了完成Sandy Chapman的答案,这是Objective-C的解决方案(将此类别放在您要更改的任何位置UILabel
Appearance
):
@implementation UILabel (FontOverride)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize];
}
@end
接口文件应该公开声明此方法,以便以后在您的应用程序委托等地方使用:
@interface UILabel (FontOverride)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR;
@end
然后,您可以使用更改Appearance
:
[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Light"];
SWIFT 3.0和SWIFT警告的评论
您可以删除以下警告消息:
let initCoderMethod = class_getInstanceMethod(self, Selector("initWithCoder:"))
通过将其替换为:
let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:)))
对于Swift 5
以上所有答案都是正确的,但是根据设备的大小,我所做的操作几乎没有什么不同。在这里,我在ATFontManager类中设置了默认字体大小,该字体大小在该类的顶部定义为defaultFontSize,这是iphone plus的字体大小, 您可以根据需要进行更改。
class ATFontManager: UIFont{
class func setFont( _ iPhone7PlusFontSize: CGFloat? = nil,andFontName fontN : String = FontName.helveticaNeue) -> UIFont{
let defaultFontSize : CGFloat = 16
switch ATDeviceDetector().screenType {
case .iPhone4:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize - 5)!
}
return UIFont(name: fontN, size: defaultFontSize - 5)!
case .iPhone5:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize - 3)!
}
return UIFont(name: fontN, size: defaultFontSize - 3)!
case .iPhone6AndIphone7, .iPhoneUnknownSmallSize:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize - 2)!
}
return UIFont(name: fontN, size: defaultFontSize - 2)!
case .iPhone6PAndIPhone7P, .iPhoneUnknownBigSize:
return UIFont(name: fontN, size: iPhone7PlusFontSize ?? defaultFontSize)!
case .iPhoneX, .iPhoneXsMax:
return UIFont(name: fontN, size: iPhone7PlusFontSize ?? defaultFontSize)!
case .iPadMini:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize + 2)!
}
return UIFont(name: fontN, size: defaultFontSize + 2)!
case .iPadPro10Inch:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize + 4)!
}
return UIFont(name: fontN, size: defaultFontSize + 4)!
case .iPadPro:
if let fontSize = iPhone7PlusFontSize{
return UIFont(name: fontN, size: fontSize + 6)!
}
return UIFont(name: fontN, size: defaultFontSize + 6)!
case .iPadUnknownSmallSize:
return UIFont(name: fontN, size: defaultFontSize + 2)!
case .iPadUnknownBigSize:
return UIFont(name: fontN, size: defaultFontSize + 4)!
default:
return UIFont(name: fontN, size: iPhone7PlusFontSize ?? 16)!
}
}
}
我已经添加了某些字体名称,要添加更多的字体名称,请在此处输入。
enum FontName : String {
case HelveticaNeue = "HelveticaNeue"
case HelveticaNeueUltraLight = "HelveticaNeue-UltraLight"
case HelveticaNeueBold = "HelveticaNeue-Bold"
case HelveticaNeueBoldItalic = "HelveticaNeue-BoldItalic"
case HelveticaNeueMedium = "HelveticaNeue-Medium"
case AvenirBlack = "Avenir-Black"
case ArialBoldMT = "Arial-BoldMT"
case HoeflerTextBlack = "HoeflerText-Black"
case AMCAPEternal = "AMCAPEternal"
}
此类涉及设备检测器,以便根据设备提供适当的字体大小。
class ATDeviceDetector {
var iPhone: Bool {
return UIDevice().userInterfaceIdiom == .phone
}
var ipad : Bool{
return UIDevice().userInterfaceIdiom == .pad
}
let isRetina = UIScreen.main.scale >= 2.0
enum ScreenType: String {
case iPhone4
case iPhone5
case iPhone6AndIphone7
case iPhone6PAndIPhone7P
case iPhoneX
case iPadMini
case iPadPro
case iPadPro10Inch
case iPhoneOrIPadSmallSizeUnknown
case iPadUnknown
case unknown
}
struct ScreenSize{
static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH,ScreenSize.SCREEN_HEIGHT)
static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH,ScreenSize.SCREEN_HEIGHT)
}
var screenType: ScreenType {
switch ScreenSize.SCREEN_MAX_LENGTH {
case 0..<568.0:
return .iPhone4
case 568.0:
return .iPhone5
case 667.0:
return .iPhone6AndIphone7
case 736.0:
return .iPhone6PAndIPhone7P
case 812.0:
return .iPhoneX
case 568.0..<812.0:
return .iPhoneOrIPadSmallSizeUnknown
case 1112.0:
return .iPadPro10Inch
case 1024.0:
return .iPadMini
case 1366.0:
return .iPadPro
case 812.0..<1366.0:
return .iPadUnknown
default:
return .unknown
}
}
}
如何使用。希望它会有所帮助。
//for default
label.font = ATFontManager.setFont()
//if you want to provide as your demand. Here **iPhone7PlusFontSize** variable is denoted as font size for *iphone 7plus and iphone 6 plus*, and it **ATFontManager** class automatically handle.
label.font = ATFontManager.setFont(iPhone7PlusFontSize: 15, andFontName: FontName.HelveticaNeue.rawValue)
字体类型总是在代码和笔尖/故事板上设置。
对于代码,就像Hugues BR所说的那样,在类别中执行它可以解决问题。
对于笔尖/故事板,我们可以使用方法Swizzling awakeFromNib更改字体类型,因为笔尖/故事板的UI元素始终在屏幕上显示之前调用它。
我想您知道Aspects。这是一个基于Method Swizzling的AOP编程库。我们为UILabel,UIButton,UITextView创建类别以实现它。
UILabel:
#import "UILabel+OverrideBaseFont.h"
#import "Aspects.h"
@implementation UILabel (OverrideBaseFont)
+ (void)load {
[[self class]aspect_hookSelector:@selector(awakeFromNib) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) {
UILabel* instance = [aspectInfo instance];
UIFont* font = [UIFont fontWithName:@"HelveticaNeue-light" size:instance.font.pointSize];
instance.font = font;
}error:nil];
}
@end
UIButton:
#import "UIButton+OverrideBaseFont.h"
#import "Aspects.h"
@implementation UIButton (OverrideBaseFont)
+ (void)load {
[[self class]aspect_hookSelector:@selector(awakeFromNib) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) {
UIButton* instance = [aspectInfo instance];
UILabel* label = instance.titleLabel;
UIFont* font = [UIFont fontWithName:@"HelveticaNeue-light" size:label.font.pointSize];
instance.titleLabel.font = font;
}error:nil];
}
@end
UITextField:
#import "UITextField+OverrideBaseFont.h"
#import "Aspects.h"
@implementation UITextField (OverrideBaseFont)
+ (void)load {
[[self class]aspect_hookSelector:@selector(awakeFromNib) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) {
UITextField* instance = [aspectInfo instance];
UIFont* font = [UIFont fontWithName:@"HelveticaNeue-light" size:instance.font.pointSize];
instance.font = font;
}error:nil];
}
@end
UITextView:
#import "UITextView+OverrideBaseFont.h"
#import "Aspects.h"
@implementation UITextView (OverrideBaseFont)
+ (void)load {
[[self class]aspect_hookSelector:@selector(awakeFromNib) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) {
UITextView* instance = [aspectInfo instance];
UIFont* font = [UIFont fontWithName:@"HelveticaNeue-light" size:instance.font.pointSize];
instance.font = font;
}error:nil];
}
@end
就是这样,您可以使用您的字体名称将HelveticaNeue-light更改为宏。
在回顾了几篇文章后,我为Swift 4创建了自己的字体转换,它涵盖了大多数情况,例如:
1st将字体添加到项目结构和.plist文件(具有相同名称):
<key>UIAppFonts</key>
<array>
<string>Typo-Light.ttf</string>
<string>Typo-Regular.ttf</string>
<string>Typo-Semibold.ttf</string>
<string>Typo-LightItalic.ttf</string>
</array>
然后
struct Resources {
struct Fonts {
//struct is extended in Fonts
}
}
extension Resources.Fonts {
enum Weight: String {
case light = "Typo-Light"
case regular = "Typo-Regular"
case semibold = "Typo-Semibold"
case italic = "Typo-LightItalic"
}
}
extension UIFontDescriptor.AttributeName {
static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
}
extension UIFont {
@objc class func mySystemFont(ofSize: CGFloat, weight: UIFont.Weight) -> UIFont {
switch weight {
case .semibold, .bold, .heavy, .black:
return UIFont(name: Resources.Fonts.Weight.semibold.rawValue, size: ofSize)!
case .medium, .regular:
return UIFont(name: Resources.Fonts.Weight.regular.rawValue, size: ofSize)!
default:
return UIFont(name: Resources.Fonts.Weight.light.rawValue, size: ofSize)!
}
}
@objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: Resources.Fonts.Weight.light.rawValue, size: size)!
}
@objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: Resources.Fonts.Weight.semibold.rawValue, size: size)!
}
@objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: Resources.Fonts.Weight.italic.rawValue, size: size)!
}
@objc convenience init(myCoder aDecoder: NSCoder) {
guard
let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
self.init(myCoder: aDecoder)
return
}
var fontName = ""
switch fontAttribute {
case "CTFontRegularUsage", "CTFontMediumUsage":
fontName = Resources.Fonts.Weight.regular.rawValue
case "CTFontEmphasizedUsage", "CTFontBoldUsage", "CTFontSemiboldUsage","CTFontHeavyUsage", "CTFontBlackUsage":
fontName = Resources.Fonts.Weight.semibold.rawValue
case "CTFontObliqueUsage":
fontName = Resources.Fonts.Weight.italic.rawValue
default:
fontName = Resources.Fonts.Weight.light.rawValue
}
self.init(name: fontName, size: fontDescriptor.pointSize)!
}
class func overrideDefaultTypography() {
guard self == UIFont.self else { return }
if let systemFontMethodWithWeight = class_getClassMethod(self, #selector(systemFont(ofSize: weight:))),
let mySystemFontMethodWithWeight = class_getClassMethod(self, #selector(mySystemFont(ofSize: weight:))) {
method_exchangeImplementations(systemFontMethodWithWeight, mySystemFontMethodWithWeight)
}
if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
}
if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
}
if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
}
if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))),
let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
}
}
}
最后Appdelegate
在下一个调用创建的方法:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
UIFont.overrideDefaultTypography()
return true
}
}
可能不是,您可能会自己在控件上设置字体,但是您可以通过集中从哪里获取字体类型来简化此过程,例如让应用程序委托或某些其他公共类拥有返回该字体的方法。字体,任何需要设置字体的方法都可以调用该方法,这将在您需要更改字体的情况下提供帮助,您可以在一个位置而不是在设置字体的任何地方进行更改...另一种选择可以是使您的UI元素,该元素将自动设置字体,但可能会过大。
我正在迅速使用这种类型的字体类。使用字体扩展类。
enum FontName: String {
case regular = "Roboto-Regular"
}
//MARK: - Set Font Size
enum FontSize: CGFloat {
case size = 10
}
extension UIFont {
//MARK: - Bold Font
class var regularFont10: UIFont {
return UIFont(name: FontName.regular.rawValue, size:FontSize.size.rawValue )!
}
}
在Swift -Xcode 7.2中,我们已经使用父视图控制器和子视图控制器(继承)实现了相同的功能。
文件-新建-Cocoa Touch类-ParentViewController。
import UIKit
import Foundation
class ParentViewController: UIViewController {
var appUIColor:UIColor = UIColor.redColor()
var appFont:UIFont = UIFont(name: "Copperplate", size: 20)!
override func viewDidLoad() {
super.viewDidLoad()
}
func addStatusBar()
{
let view = UIView(frame:
CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)
)
view.backgroundColor = appUIColor
self.view.addSubview(view)
}
}
使子视图控制器并与StoryBoard VC关联,添加一个textLabel。
import UIKit
class FontTestController: ParentViewController {
@IBOutlet var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
testLabel.font = appFont
testLabel.textColor = appUIColor
}
或制作自定义UILabel类(子分类方法),并将所需的标签与其关联。
import Foundation
import UIKit
class CustomFontLabel: UILabel {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
backgroundColor = ParentViewController().appUIColor
font = ParentViewController().appFont
textColor = UIColor.blackColor()
}
}
注意:Parent VC中声明的Font和color在CustomFontLabel中实现。优点是我们可以通过对父VC进行一些简单更改来一起更改uilabel /任何视图的属性。
2)“ for”循环UIView的子视图。它仅适用于特定的VC。
override func viewWillLayoutSubviews() {
for view in self.view.subviews {
if view.isKindOfClass(UITextField) {
UITextField.appearance().font = UIFont(name: "Copperplate", size: 20)
}
if view.isKindOfClass(UILabel) {
UILabel.appearance().font = UIFont(name: "Copperplate", size: 20)
}
}
}