有没有办法完全忽略iOS应用程序中的动态字体/字体大小设置?我的意思是,有没有像plist条目这样的方法,这样我就可以完全禁用它。我知道当设置发生变化时,我们可以观察和重新配置字体的通知。我正在寻找一种更简单的解决方案。我正在使用iOS8。谢谢。
发布于 2019-02-12 03:55:28
没有必要对UIApplication信口开河。只需将UIApplication子类化并提供您自己的preferredContentSizeCategory实现
Swift:
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]));
}
}发布于 2016-05-31 23:29:00
在您的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);
...
}发布于 2019-01-11 12:07:16
只需向@gebirgsbärbel answer提供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
}
}https://stackoverflow.com/questions/27814783
复制相似问题