快速为整个 iOS 应用程序使用自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28180449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using custom font for entire iOS app swift
提问by tawheed
I know that to set a custom font of an element on the screen I can simply dosomeLabel.font=UIFont(name: "Exo 2.0", size: 15)
.
我知道在屏幕上设置元素的自定义字体我可以简单地做someLabel.font=UIFont(name: "Exo 2.0", size: 15)
。
I was wondering how one could set it for an entire app using swift. (A hack would be to do what I know for every single element of the app but that is just going to be a maintainability nightmare)
我想知道如何使用 swift 为整个应用程序设置它。(黑客将是对应用程序的每个元素都做我所知道的,但这只会是可维护性的噩梦)
I see that this question has already been asked with an objective-C tag, How to set a custom font for entire iOS app without specifying sizebut I'm not familiar enough with objective-C enough to port the code to swift
我看到这个问题已经用 Objective-C 标签问过了,如何在不指定大小的情况下为整个 iOS 应用程序设置自定义字体,但我对 Objective-C 不够熟悉,无法将代码移植到 swift
回答by Christian
You can set the appearance of the UILabel and other UIViews:
您可以设置 UILabel 和其他 UIViews 的外观:
UILabel.appearance().font = UIFont(name: "yourFont", size: yourSize)
More General:
更一般:
AnyUIView.appearance().font = UIFont(name: "yourFont", size: yourSize)
回答by Mostafa Aghajani
As a more detailed answer and with these benefits I recommend using extensions:
作为更详细的答案和这些好处,我建议使用扩展:
- No size override (whatever you set in designer will be used)
- No style override (Bold, Light, Medium, UltraLight is implemented in my code but you can customize it as you need)
- 无尺寸覆盖(将使用您在设计器中设置的任何内容)
- 无样式覆盖(我的代码中实现了 Bold、Light、Medium、UltraLight,但您可以根据需要对其进行自定义)
import UIKit
extension UILabel {
public var substituteFontName : String {
get {
return self.font.fontName;
}
set {
let fontNameToTest = self.font.fontName.lowercased();
var fontName = newValue;
if fontNameToTest.range(of: "bold") != nil {
fontName += "-Bold";
} else if fontNameToTest.range(of: "medium") != nil {
fontName += "-Medium";
} else if fontNameToTest.range(of: "light") != nil {
fontName += "-Light";
} else if fontNameToTest.range(of: "ultralight") != nil {
fontName += "-UltraLight";
}
self.font = UIFont(name: fontName, size: self.font.pointSize)
}
}
}
extension UITextView {
public var substituteFontName : String {
get {
return self.font?.fontName ?? "";
}
set {
let fontNameToTest = self.font?.fontName.lowercased() ?? "";
var fontName = newValue;
if fontNameToTest.range(of: "bold") != nil {
fontName += "-Bold";
} else if fontNameToTest.range(of: "medium") != nil {
fontName += "-Medium";
} else if fontNameToTest.range(of: "light") != nil {
fontName += "-Light";
} else if fontNameToTest.range(of: "ultralight") != nil {
fontName += "-UltraLight";
}
self.font = UIFont(name: fontName, size: self.font?.pointSize ?? 17)
}
}
}
extension UITextField {
public var substituteFontName : String {
get {
return self.font?.fontName ?? "";
}
set {
let fontNameToTest = self.font?.fontName.lowercased() ?? "";
var fontName = newValue;
if fontNameToTest.range(of: "bold") != nil {
fontName += "-Bold";
} else if fontNameToTest.range(of: "medium") != nil {
fontName += "-Medium";
} else if fontNameToTest.range(of: "light") != nil {
fontName += "-Light";
} else if fontNameToTest.range(of: "ultralight") != nil {
fontName += "-UltraLight";
}
self.font = UIFont(name: fontName, size: self.font?.pointSize ?? 17)
}
}
}
Samples for using Extensions:
使用扩展的示例:
e.g. put these lines in your starting controller viewDidLoad
例如,将这些行放在您的起始控制器 viewDidLoad 中
UILabel.appearance().substituteFontName = "IRANSans"; // USE YOUR FONT NAME INSTEAD
UITextView.appearance().substituteFontName = "IRANSans"; // USE YOUR FONT NAME INSTEAD
UITextField.appearance().substituteFontName = "IRANSans"; // USE YOUR FONT NAME INSTEAD
P.S. as @Christian mentioned, you can write your own extensions for almost AnyUIView
PS 正如@Christian 提到的,您几乎可以为 AnyUIView 编写自己的扩展
回答by Rmalmoe
FINALLY figured this out. Cleanest way I could find. (Swift 4) Solution doesn't require you to set font sizes and won't override all font sizes.
终于想通了。我能找到的最干净的方式。(Swift 4) 解决方案不需要您设置字体大小,也不会覆盖所有字体大小。
UILabel.appearance().font = UIFont.preferredFont(forTextStyle: UIFontTextStyle(rawValue: "Roboto"))
For those looking for where to place this code. I put it in my AppDelegate.swift file inside of
对于那些正在寻找放置此代码的位置的人。我把它放在我的 AppDelegate.swift 文件里面
func application(_ application: UIApplication,
didFinishLaunchingWithOptions...
Swift 4.2
斯威夫特 4.2
UILabel.appearance().font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle(rawValue: "Roboto"))
回答by Surjeet
I found out a way to do same in Swift 4 with iOS 11
我找到了一种在Swift 4 和 iOS 11 中做同样事情的方法
Only need to add @objc
keyword before variable name.
So variable declaration will be like this
只需要@objc
在变量名前加上关键字。所以变量声明会像这样
@objc public var substituteFontName : String {
get {}
set {}
}
Hope this helps others who are facing this issue.
希望这可以帮助面临此问题的其他人。
回答by MihaiL
The answer is a combination of the answers offered already, but I had to try a combination of things to get it working in iOS 13:
答案是已经提供的答案的组合,但我必须尝试多种方法才能使其在 iOS 13 中运行:
This is all placed in a Table View Controller, which is made to conform to the UIFontPickerViewControllerDelegate in order to offer a view for the user to pick the font. This will update all the UILabels with the new font, but keep the other attributes.
这一切都被放置在一个 Table View Controller 中,该控制器符合 UIFontPickerViewControllerDelegate 以便为用户提供一个选择字体的视图。这将使用新字体更新所有 UILabels,但保留其他属性。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if cell === fontCell {
if #available(iOS 13, *) {
let configuration = UIFontPickerViewController.Configuration()
configuration.includeFaces = true
let fontVC = UIFontPickerViewController(configuration: configuration)
fontVC.delegate = self
present(fontVC, animated: true)
} else {
let ac = UIAlertController(title: "iOS 13 is required", message: "Custom fonts are only supported by devices running iOS 13 or above.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
}
}
@available(iOS 13.0, *)
extension AppearanceTableViewController: UIFontPickerViewControllerDelegate {
func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
// attempt to read the selected font descriptor, but exit quietly if that fails
guard let descriptor = viewController.selectedFontDescriptor else { return }
let font = UIFont(descriptor: descriptor, size: 20)
UILabel.appearance().substituteFontName = font.fontName
}
}
extension UILabel {
@objc public var substituteFontName : String {
get {
return self.font.fontName;
}
set {
let fontNameToTest = self.font.fontName.lowercased()
var fontName = newValue
if fontNameToTest.range(of: "bold") != nil {
fontName += "-Bold"
} else if fontNameToTest.range(of: "medium") != nil {
fontName += "-Medium"
} else if fontNameToTest.range(of: "light") != nil {
fontName += "-Light"
} else if fontNameToTest.range(of: "ultralight") != nil {
fontName += "-UltraLight"
}
self.font = UIFont(name: fontName, size: self.font.pointSize)
}
}
}