有没有一种方法完全忽略iOS应用程序的动态类型/字体大小的设置?我的意思是有像的plist条目的方式,这样我可以完全禁用它。我知道有,我们可以观察到,每当有在设置的变化重新配置字体的通知。我正在寻找一个简单的解决方案。我使用iOS8上。谢谢。
有没有必要调酒UIApplication
。只要继承UIApplication
,并提供自己的实现preferredContentSizeCategory
的:
迅速:
class MyApplication: UIApplication {
override var preferredContentSizeCategory: UIContentSizeCategory {
get { return UIContentSizeCategory.large }
}
}
UIApplicationMain(
CommandLine.argc,
CommandLine.unsafeArgv,
NSStringFromClass(MyApplication.self),
NSStringFromClass(AppDelegate.self)
)
Objective-C的:
@interface MyApplication : UIApplication
@end
@implementation MyApplication
- (UIContentSizeCategory) preferredContentSizeCategory
{
return UIContentSizeCategoryLarge;
}
@end
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([AppDelegate class]));
}
}
斯威夫特相当于@意义,问题的答案如下所示:
在你的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)
}
在您的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);
...
}
只需提供斯威夫特4修复程序@gebirgsbärbel答案。 “preferredContentSizeCategory”在Objective-C是一种方法,但在夫特它是只读变量。因此,在您的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
}
}