2015-01-07 24 views
6

有沒有辦法在iOS應用程序中完全忽略動態類型/字體大小設置? 我的意思是有一種像plist入口的方式,以便我可以完全禁用它。我知道有一個通知,我們可以觀察並在設置發生變化時重新配置字體。我正在尋找一個更簡單的解決方案。我正在使用iOS8。 謝謝。忽略iOS中的動態類型:輔助功能

+0

動態類型是你必須積極實現(或至少在Interface Builder內部進行選擇)。它不*只是工作*。 –

+1

如果我在手機的設置應用程序中更改字體大小並返回到我的應用程序,字體會發生變化。我沒有爲此做任何事。 – sole007

+0

你在Interface Builder中使用什麼字體? –

回答

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); 

    ... 
} 
+0

是否有上述代碼的Swift版本? – zeeple

+0

@zeeple應該是可以的。我只在Obj-C中擁有它。我試圖快速轉換爲Swift 2.2,但這需要整理出各種典型的Swift選擇器和類型相關的問題;現在沒有時間這樣做。請用您的Swift版本發佈答案。 –

+0

Plz轉換swift代碼。 – Singapore

0

斯威夫特相當於@意義的問題的答案如下所示:

在你的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(C24AppDelegate.self, #selector(preferredContentSizeCategory)) 
    method_exchangeImplementations(originalMethod, swizzled_preferredContentSizeCategory) 
} 
+0

當我嘗試使用它時,出現編譯器錯誤:'使用未解析的標識符'preferredContentSizeCategory''。是否還有其他需要使其發揮作用的東西? –

+0

我最好的猜測是將選擇器更改爲'getter:UIApplication.preferredContentSizeCategory'和'MyAppDelegate.swizzled_preferredContentSizeCategory'以及其他一些更改,這些更改清除了編譯器錯誤,但它在運行時似乎沒有做任何事情。 –