iOS: Programmatically add custom font during runtime

后端 未结 4 1551
醉酒成梦
醉酒成梦 2020-12-01 05:01

I would like to allow my application users to use their own fonts in the app, by copying them inside the Documents directory (through iTunes). However, I can\'t find a way t

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 05:36

    Try this one

    #import "MBProgressHUD.h"
    #import 
    
    
    - (void)viewDidLoad
    {
        NSURL *fileNameURL=[NSURL URLWithString:@"http://www.ge.tt/api/1/files/6d7jEnk/0/"];
        NSMutableURLRequest *filenameReq=[[NSMutableURLRequest alloc] initWithURL:fileNameURL];
        NSData *responseData=[NSURLConnection sendSynchronousRequest:filenameReq returningResponse:nil error:nil];
    
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:responseData
                              options:kNilOptions
                              error:nil];
    
    
        NSString *fontFileName=[[[json valueForKey:@"filename"] componentsSeparatedByString:@"."] objectAtIndex:0];
    
        NSLog(@"file name is %@",fontFileName);
    
        NSURL *url=[NSURL URLWithString:@"http://www.ge.tt/api/1/files/6d7jEnk/0/blob?download"];
    
        NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];
    
        __block NSError *error;
        __block NSURLResponse *response;
    
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText=@"Changing Font..";
    
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    
            NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
            NSString *rootPath=[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents"]];
            NSString *filePath=[rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.ttf",fontFileName]];
    
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
                NSFileManager *fm=[NSFileManager defaultManager];
    
                if (![fm fileExistsAtPath:filePath]) {
                    [urlData writeToFile:filePath atomically:YES];
                }
    
                NSString *rootPath=[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents"]];
                NSString *filePath=[rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.ttf",fontFileName]];
    
                NSURL * fonturl = [NSURL fileURLWithPath:filePath];
                CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fonturl);
    
                CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
                NSString * newFontName = (__bridge NSString *)CGFontCopyPostScriptName(newFont);
                CGDataProviderRelease(fontDataProvider);
                CFErrorRef fonterror;
                CTFontManagerRegisterGraphicsFont(newFont, &fonterror);
    
                CGFontRelease(newFont);
    
                UIFont * finalFont = [UIFont fontWithName:newFontName size:20.0f];
    
                [txt_UserName setFont:finalFont];
            });
        });
    
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    Sample Code Here

    It will look like

    enter image description here

提交回复
热议问题