ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • TTF폰트 파일 로딩하여 사용하기
    iOS/Mac 2013. 5. 29. 20:13


    Build Pharse > Link Bianry With Library 에  CoreText.framework 추가 필요.



    #import <CoreText/CoreText.h>



    // ttf 파일 경로 구한다.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docDir = [paths objectAtIndex:0];

    NSString* fpath = [docDir stringByAppendingPathComponent:@"saxmono.ttf"];


    // 폰트 등록

    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fpath UTF8String]);

    CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);

    CGDataProviderRelease(fontDataProvider);

    CFErrorRef error;

    CTFontManagerRegisterGraphicsFont(customFont, &error);

    CGFontRelease(customFont);


    // 폰트 사용

    UIFont* uifont = [UIFont fontWithName:@"saxmono" size:16];

    _label.font = uifont;




    _________________

    - CTFontManagerRegisterGraphicsFont 가 iOS 4.1이상부터 지원함.

    - 매번 등록할 필요없다. 한 번 등록되면 실행중에는 UIFont의 fontWithName으로 nil이 리턴되는지 확인하고 nil인 경우에만 폰트 등록을 하면 된다. 완전종료되면 다시 등록해야한다.




    반응형

    댓글

Designed by Tistory.