简体   繁体   中英

Fix error setting custom ios font

I'm trying to set a custom font with an appearance proxy.

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

I've added the ttf to my project, and confirmed that the font name is correct.

XCode gives me this error:

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

What am I doing wrong?

You are using -setTitleTextAttributes with label while it is UINavigationBar function

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

thanks

弄清楚了,它是setFont而不是setTitleTextAttributes

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM