iOS-Font(轉)

來源:互聯網
上載者:User

標籤:

.使用系統預設提供的字型

系統預設提供的字型主要是指UIFont中提供的字型,其使用代碼為:

fontLabel.font = [UIFont fontWithName:@"Marion" size:17];

或者是通過字型詳細字典對字型屬性進行設定

/*     UIFontDescriptorFamilyAttribute:設定字型家族名     UIFontDescriptorNameAttribute  :設定字型的字型名     UIFontDescriptorSizeAttribute  :設定字型尺寸     UIFontDescriptorMatrixAttribute:設定字型形變     */    UIFontDescriptor *attributeFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:                                                 @{UIFontDescriptorFamilyAttribute: @"Marion",                                                   UIFontDescriptorNameAttribute:@"Marion-Regular",                                                   UIFontDescriptorSizeAttribute: @40.0,                                                   UIFontDescriptorMatrixAttribute:[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_1_PI*1.5)                                                                                    ]}];    fnotLabel.font = [UIFont fontWithDescriptor:attributeFontDescriptor size:0.0];

其中的字型家族名和字型名可以通過以下方法擷取

NSLog(@"familyNames:%@",[UIFont familyNames]);

以上兩種方法均可以為label設定字型,但是全部是只針對英文數字,對中文無效。要想改變中文字型還需要使用後面兩種辦法

2.動態下載字型

iOS6以後蘋果就開始支援動態下載中文字型已供應用中展示個性字型的需求,由於下載的時候需要使用的名字是PostScript名稱,需要使用Mac內內建的應用“字型冊“來獲得相應字型的PostScript名稱。如下顯示了從”字型冊“中擷取《娃娃體-繁 常規體》字型的PostScript名稱的

具體代碼就不一一介紹了,大家可以參考蘋果提供的有關文檔:https://developer.apple.com/library/ios/samplecode/DownloadFont/Listings/DownloadFont_ViewController_m.html#//apple_ref/doc/uid/DTS40013404-DownloadFont_ViewController_m-DontLinkElementID_6

或者也可以參考唐巧先生的部落格有比較詳細的介紹:http://blog.devtang.com/blog/2013/08/11/ios-asian-font-download-introduction/

下面是我研究後的測試demo,提供給大家參考:

- (void)asynchronouslySetFontName:(NSString *)fontName{    UIFont* aFont = [UIFont fontWithName:fontName size:24];    // 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.        _fLabelView.text = @"歡迎查看我的部落格";        _fLabelView.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                NSLog(@"Begin Matching");            });        } else if (state == kCTFontDescriptorMatchingDidFinish) {            dispatch_async( dispatch_get_main_queue(), ^ {                // Remove the activity indicator                                // Display the sample text for the newly downloaded font                _fLabelView.text = @"歡迎查看我的部落格";                _fLabelView.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                             NSLog(@"Begin Downloading");            });        } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {            dispatch_async( dispatch_get_main_queue(), ^ {                // Remove the progress bar                NSLog(@"Finish downloading");            });        } else if (state == kCTFontDescriptorMatchingDownloading) {            dispatch_async( dispatch_get_main_queue(), ^ {                // Use the progress bar to indicate the progress of the downloading                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(), ^ {                NSLog(@"Download error: %@", _errorMessage);            });        }        return (bool)YES;    });   }

 只要在相應地方調用就可以了:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    _fLabelView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 250, 100)];    [self.view addSubview:_fLabelView];        [self asynchronouslySetFontName:@"HanziPenSC-W3"];    }

下面是運行後的結果:

3.引入外部字型

現在網上不管是windows字型,還是Android字型只要是ttf格式的,或者是蘋果提供的ttc、otf格式,一般iOS程式都支援內嵌。具體做法:

先將需要下載的字型拖到項目中

在info檔案中添加相應欄位

然後就可以使用上面提供的方法[UIFont fontWithName:@"迷你簡咪咪" size:17]方法給英文、數字或者中文設定上這種字型。可以輸出一下[UIFont familyNames]檢測是否已經添加

也可以在xib中為label設定這種字型了

網上下載的字型也不一定都是可以使用,下面提供大家一些常用字型供大家下載:

連結: http://pan.baidu.com/s/1kTVX8qF 密碼: vdwa

要想擷取更加全面的字型還可以使用蘋果自己提供的各種字型格式,還是可以通過Mac應用“字型側”擷取,例如:

用法跟下載的字型一樣

iOS-Font(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.