How to include ttf fonts in iPhone app
Categories: iPhone on Jul.30, 2009
Хороший способ подключения шрифтов.
Кидаем в ресурсы наш шрифт. Вставляем и выполняем функцию.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | NSUInteger loadFonts() { NSUInteger newFontCount = 0; NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"]; const char *frameworkPath = [[frameworkBundle executablePath] UTF8String]; if (frameworkPath) { void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY); if (graphicsServices) { BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile"); if (GSFontAddFromFile) for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"TTF" inDirectory:nil]) newFontCount += GSFontAddFromFile([fontFile UTF8String]); } } return newFontCount; } |
Смотрим что у нас появилось
1 | NSLog(@"%@",[UIFont familyNames]); |
Similar posts:


(3 votes, average: 3.67 out of 5)
August 7th, 2009 on 10:07 am
Наверное стоит упомянуть, что для работы dlopen нужно подключить dlfcn.h
August 7th, 2009 on 1:08 pm
сорри….
Вы правы :)
September 29th, 2009 on 4:50 pm
>void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
как к такому ходу отнеслись цензоры AppStore?
September 29th, 2009 on 7:44 pm
В основном на ваш страх и риск. В следующей версии может и не работать это!
December 22nd, 2009 on 7:30 pm
Еще такой нюанс.
В файле .mm не закомпилилось – победил так:
это
BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, “GSFontAddFromFile”);
заменил на
BOOL (*GSFontAddFromFile)(const char*);
(void*)GSFontAddFromFile = dlsym(graphicsServices, “GSFontAddFromFile”);
December 1st, 2010 on 8:58 pm
У меня компильнулось без проблем, но шрифт не подключился (
December 1st, 2010 on 9:26 pm
@Another84 Вставьте после подключения шрифта и посмотрите в логе как называется ваш шрифт
NSLog(@”familyNames %@”, [UIFont familyNames]);
April 14th, 2011 on 1:42 pm
Более легальный способ, работает с версии 3.2
http://www.developers-life.com/how-to-include-ttf-fonts-to-ios-app.html