美文网首页
iOS动态下载系统的中文字体

iOS动态下载系统的中文字体

作者: silasjs | 来源:发表于2018-03-06 16:18 被阅读23次

苹果提供了动态下载代码的demo工程苹果官方demo

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) NSArray *fontNames;
@property (strong, nonatomic) NSArray *fontSamples;
@property (weak, nonatomic) IBOutlet UITableView *fTableView;
@property (weak, nonatomic) IBOutlet UITextView *fTextView;
@property (weak, nonatomic) IBOutlet UIProgressView *fProgressView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *fActivityIndicatorView;

@end
#import "ViewController.h"

#import <CoreText/CoreText.h>

@interface ViewController ()

@property (strong, nonatomic) NSString *errorMessage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.fontNames = [[NSArray alloc] initWithObjects:
                      @"STXingkai-SC-Light",
                      @"DFWaWaSC-W5",
                      @"FZLTXHK--GBK1-0",
                      @"STLibian-SC-Regular",
                      @"LiHeiPro",
                      @"HiraginoSansGB-W3",
                      nil];

    self.fontSamples = [[NSArray alloc] initWithObjects:
                        @"汉体书写信息技术标准相",
                        @"容档案下载使用界面简单",
                        @"支援服务升级资讯专业制",
                        @"作创意空间快速无线上网",
                        @"兙兛兞兝兡兣嗧瓩糎",
                        @"㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩",
                        nil];
}

- (void)asynchronouslySetFontName:(NSString *)fontName {
    UIFont* aFont = [UIFont fontWithName:fontName size:12.];

    // If the font is already downloaded
    if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) {
        // Go ahead and display the sample text.
        NSUInteger sampleIndex = [_fontNames indexOfObject:fontName];
        _fTextView.text = [_fontSamples objectAtIndex:sampleIndex];
        _fTextView.font = [UIFont fontWithName:fontName size:24.];
        return;
    }

    // Create a dictionary with the font's PostScript name.
    NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil];

    // Create a new font descriptor reference from the attributes dictionary.
    CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
    NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0];
    [descs addObject:(__bridge id)desc];
    CFRelease(desc);

    __block BOOL errorDuringDownload = NO;

    // Start processing the font descriptor..
    // This function returns immediately, but can potentially take long time to process.
    // The progress is notified via the callback block of CTFontDescriptorProgressHandler type.
    // See CTFontDescriptor.h for the list of progress states and keys for progressParameter dictionary.
    CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL,  ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) {
        //NSLog( @"state %d - %@", state, progressParameter);
        double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];

        if (state == kCTFontDescriptorMatchingDidBegin) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                // Show an activity indicator
                [_fActivityIndicatorView startAnimating];
                _fActivityIndicatorView.hidden = NO;

                // Show something in the text view to indicate that we are downloading
                _fTextView.text= [NSString stringWithFormat:@"Downloading %@", fontName];
                _fTextView.font = [UIFont systemFontOfSize:14.];
                NSLog(@"Begin Matching");
            });
        } else if (state == kCTFontDescriptorMatchingDidFinish) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                // Remove the activity indicator
                [_fActivityIndicatorView stopAnimating];
                _fActivityIndicatorView.hidden = YES;

                // Display the sample text for the newly downloaded font
                NSUInteger sampleIndex = [_fontNames indexOfObject:fontName];
                _fTextView.text = [_fontSamples objectAtIndex:sampleIndex];
                _fTextView.font = [UIFont fontWithName:fontName size:24.];

                // Log the font URL in the console
                CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)fontName, 0., NULL);
                CFStringRef fontURL = CTFontCopyAttribute(fontRef, kCTFontURLAttribute);

                NSLog(@"%@", (__bridge NSURL*)(fontURL));
                CFRelease(fontURL);
                CFRelease(fontRef);

                if (!errorDuringDownload) {
                    NSLog(@"%@ downloaded", fontName);
                }
            });
        } else if (state == kCTFontDescriptorMatchingWillBeginDownloading) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                // Show a progress bar
                _fProgressView.progress = 0.0;
                _fProgressView.hidden = NO;
                NSLog(@"Begin Downloading");
            });
        } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                // Remove the progress bar
                _fProgressView.hidden = YES;
                NSLog(@"Finish downloading");
            });
        } else if (state == kCTFontDescriptorMatchingDownloading) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                // Use the progress bar to indicate the progress of the downloading
                [_fProgressView setProgress:progressValue / 100.0 animated:YES];
                NSLog(@"Downloading %.0f%% complete", progressValue);
            });
        } else if (state == kCTFontDescriptorMatchingDidFailWithError) {
            // An error has occurred.
            // Get the error message
            NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];

            if (error != nil) {
                _errorMessage = [error description];
            } else {
                _errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!";
            }

            // Set our flag
            errorDuringDownload = YES;

            dispatch_async( dispatch_get_main_queue(), ^ {
                _fProgressView.hidden = YES;
                NSLog(@"Download error: %@", _errorMessage);
            });
        }
        return (bool)YES;
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_fontNames count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil) {
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }

    // Set up the cell.
    cell.textLabel.text = _fontNames[indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self asynchronouslySetFontName:_fontNames[indexPath.row]];

    // Dismiss the keyboard in the text view if it is currently displayed
    if ([self.fTextView isFirstResponder])
    [self.fTextView resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

相关文章

  • 多种中文字体

    功能介绍 使用动态下载中文字体的 API 可以动态地向 iOS 系统中添加字体文件,这些字体文件都是下载到系统的目...

  • iOS-动态下载中文字体

    从iOS6开始,苹果开始支持动态下载官方提供的中文字体到系统中。使用苹果官方提供的中文字体,既可以避免版权问题,又...

  • iOS字体下载

    iOS6之后的系统,苹果官方支持动态下载官方提供的中文字体。这样不仅可以避免版权问题,也可以减少项目的体积。下面就...

  • iOS动态下载系统的中文字体

    苹果提供了动态下载代码的demo工程苹果官方demo。

  • iOS-apple字体下载

    苹果提供的动态下载代码的 Demo 工程 链接在这里。 动态下载苹果提供的多种中文字体 字体下载网

  • iOS 字体下载

    参考文章:原文? iOS可以动态的为系统下载字体,这些字体都下载到了系统的/private/var/mobile/...

  • iOS 动态下载系统提供的多种中文字体

    一、功能简介 1、前言 为了实现更好的字体效果,有些应用在自己的应用资源包中加入了字体文件。但自己打包字体文件比较...

  • iOS 使用自定义字体

    1. 动态下载系统提供的中文字体 为了实现更好的字体效果,在应用中加入字体包问题: 字体文件比较大,会造成应用体积...

  • 动态下载系统提供的中文字体

    这个功能的实现,是自己看了唐巧大神的博客,学习的,目的是分享。 至于为什么要下载中文字体,有两个原因:1、字体文件...

  • 2019-01-15 关于iOS系统字体

    中文字体: iOS 9 以后中文字体是"苹方"。 iOS 8 中文字体是"常州华文的黑体-简"。 英文字体:iOS...

网友评论

      本文标题:iOS动态下载系统的中文字体

      本文链接:https://www.haomeiwen.com/subject/cckrfftx.html