首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略iOS: Accessibility中的动态类型

忽略iOS: Accessibility中的动态类型
EN

Stack Overflow用户
提问于 2015-01-07 15:56:29
回答 4查看 4.5K关注 0票数 12

有没有办法完全忽略iOS应用程序中的动态字体/字体大小设置?我的意思是,有没有像plist条目这样的方法,这样我就可以完全禁用它。我知道当设置发生变化时,我们可以观察和重新配置字体的通知。我正在寻找一种更简单的解决方案。我正在使用iOS8。谢谢。

EN

回答 4

Stack Overflow用户

发布于 2019-02-12 03:55:28

没有必要对UIApplication信口开河。只需将UIApplication子类化并提供您自己的preferredContentSizeCategory实现

Swift:

代码语言:javascript
复制
class MyApplication: UIApplication {

    override var preferredContentSizeCategory: UIContentSizeCategory {
        get { return UIContentSizeCategory.large }
    }
}

UIApplicationMain(
    CommandLine.argc,
    CommandLine.unsafeArgv,
    NSStringFromClass(MyApplication.self),
    NSStringFromClass(AppDelegate.self)
)

Objective-C:

代码语言:javascript
复制
@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]));
    }
}
票数 9
EN

Stack Overflow用户

发布于 2016-05-31 23:29:00

在您的AppDelegate中添加:

代码语言:javascript
复制
#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
EN

Stack Overflow用户

发布于 2019-01-11 12:07:16

只需向@gebirgsbärbel answer提供Swift 4修复即可。Objective-C中的"preferredContentSizeCategory“是一个方法,但在Swift中它是一个只读变量。所以在你的AppDelegate中是这样的:

代码语言:javascript
复制
@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
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27814783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档