2012-11-27 36 views
1

我試圖設置自定義字體與外觀代理。修復錯誤設置自定義ios字體

[[UILabel appearance] setTitleTextAttributes:@{ 
    UITextAttributeFont:[UIFont fontWithName:@"PT Sans Narrow" size:10.0] 
}]; 

我已經添加了TTF到我的項目,並證實該字體名稱是否正確。

的XCode給我這個錯誤:

2012-11-27 02:04:00.002 dashboard-mock[17093:11303] -[_UIAppearance setTitleTextAttributes:]: unrecognized selector sent to instance 0x712a1c0 
2012-11-27 02:04:00.002 dashboard-mock[17093:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAppearance setTitleTextAttributes:]: unrecognized selector sent to instance 0x712a1c0' 
*** First throw call stack: 
(0x1c92012 0x10cfe7e 0x1d1d4bd 0x1c81bbc 0x1c8194e 0x2aec 0x147b7 0x14da7 0x15fab 0x27315 0x2824b 0x19cf8 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x157da 0x1765c 0x28cd 0x27f5) 
libc++abi.dylib: terminate called throwing an exception 

我在做什麼錯?

回答

1

您正在使用-setTitleTextAttributes與標籤,而這是UINavigationBar功能

if([[UINavigationBar appearance] respondsToSelector:@selector(setTitleTextAttributes:)]) 
    [[UINavigationBar appearance] setTitleTextAttributes:@{ 
         UITextAttributeFont:[UIFont systemFontOfSize:12], 
        UITextAttributeTextColor:[UIColor redColor] 
}]; 

感謝

+0

不,這是一個UILabel,而不是UINavigationBar – k107

+0

這是可移植的任何控制 - 黃金。謝謝! – capikaw

0

想通了,它是setFont代替setTitleTextAttributes

[[UILabel appearance] setFont:[UIFont fontWithName:@"PT Sans Narrow" size:10.0]];