我知道要在屏幕上设置元素的自定义字体,我只需someLabel.font=UIFont(name: "Exo 2.0", size: 15)
即可。
我想知道如何使用swift为整个应用设置它。 (对于应用程序的每个元素,黑客都会做我所知道的,但这只是一个可维护性的噩梦)
我看到这个问题已经被一个Objective-C标签How to set a custom font for entire iOS app without specifying size询问了,但我对Objective-C不够熟悉,足以将代码移植到swift
答案 0 :(得分:30)
您可以设置UILabel和其他UIViews的外观:
UILabel.appearance().font = UIFont(name: "yourFont", size: yourSize)
更多一般:
AnyUIView.appearance().font = UIFont(name: "yourFont", size: yourSize)
答案 1 :(得分:12)
作为更详细的答案,我建议使用扩展程序:
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)
}
}
}
使用扩展程序的示例:
e.g。将这些行放在起始控制器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。正如@Christian所提到的,你可以为几乎AnyUIView编写自己的扩展
答案 2 :(得分:2)
最终弄清楚了这一点。我能找到的最干净的方法。 (快速4) 解决方案不需要您设置字体大小,也不会覆盖所有字体大小。
UILabel.appearance().font = UIFont.preferredFont(forTextStyle: UIFontTextStyle(rawValue: "Roboto"))
适合那些寻找将代码放置在何处的人。我将其放在
内的AppDelegate.swift文件中 func application(_ application: UIApplication,
didFinishLaunchingWithOptions...
Swift 4.2
UILabel.appearance().font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle(rawValue: "Roboto"))
答案 3 :(得分:1)
我在 Swift 4 with iOS 11
中找到了一种方法只需在变量名称前添加 'admin' => [
'identityClass' => 'app\models\adminUsers',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendUser', // unique for backend
'path' => '/advanced/backend/web' // correct path for backend app.
]
],
关键字。
所以变量声明就像这样
@objc
希望这可以帮助那些面临这个问题的人。
答案 4 :(得分:0)
答案是已经提供的答案的组合,但是我不得不尝试多种方法才能使其在iOS 13中运行:
所有这些都放置在Table View Controller中,该控件与UIFontPickerViewControllerDelegate兼容,以便为用户提供选择字体的视图。 这将使用新字体更新所有UILabel,但保留其他属性。
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)
}
}
}