有没有办法完全忽略iOS应用中的动态类型/字体大小设置? 我的意思是有一种方式像plist条目,所以我可以完全禁用它。据我所知,只要设置发生变化,我们就可以观察并重新配置字体。我正在寻找一个更简单的解决方案。我正在使用iOS8。 感谢。
答案 0 :(得分:2)
快速等同于@含义的回答如下:
在AppDelegate中:
@objc func swizzled_preferredContentSizeCategory() -> UIContentSizeCategory {
return UIContentSizeCategory.small
}
open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let originalMethod = class_getInstanceMethod(UIApplication.self, #selector(preferredContentSizeCategory))
let swizzledMethod = class_getInstanceMethod(UIApplication.self, #selector(swizzled_preferredContentSizeCategory))
method_exchangeImplementations(originalMethod, swizzled_preferredContentSizeCategory)
}
答案 1 :(得分:1)
在AppDelegate
添加:
#import <objc/runtime.h>
@implementation AppDelegate
NSString* swizzled_preferredContentSizeCategory(id self, SEL _cmd)
{
return UIContentSizeCategoryLarge; // Set category you prefer, Large being iOS' default.
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
Method method = class_getInstanceMethod([UIApplication class], @selector(preferredContentSizeCategory));
method_setImplementation(method, (IMP)swizzled_preferredContentSizeCategory);
...
}
答案 2 :(得分:1)
无需麻烦![test](/src/images/2.jpg)
![test2](/src/images/3.jpg)
[]()
。只是子类UIApplication
并提供您自己的UIApplication
的实现:
迅速:
preferredContentSizeCategory
Objective-C:
class MyApplication: UIApplication {
override var preferredContentSizeCategory: UIContentSizeCategory {
get { return UIContentSizeCategory.large }
}
}
UIApplicationMain(
CommandLine.argc,
CommandLine.unsafeArgv,
NSStringFromClass(MyApplication.self),
NSStringFromClass(AppDelegate.self)
)
答案 3 :(得分:0)
只需提供对@gebirgsbärbel答案的Swift 4修复即可。 Objective-C中的“ preferredContentSizeCategory”是一种方法,但是在Swift中,它是一个只读变量。所以在您的AppDelegate中是这样的:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// MARK: - UIApplicationDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UIApplication.classInit
self.window = UIWindow(frame: UIScreen.main.bounds)
...
self.window?.makeKeyAndVisible()
return true
}
}
// MARK: - Fix Dynamic Type
extension UIApplication {
static let classInit: Void = {
method_exchangeImplementations(
class_getInstanceMethod(UIApplication.self, #selector(getter: fixedPreferredContentSizeCategory))!,
class_getInstanceMethod(UIApplication.self, #selector(getter: preferredContentSizeCategory))!
)
}()
@objc
var fixedPreferredContentSizeCategory: UIContentSizeCategory {
return .large
}
}