Core Text框架詳細(xì)解析(五) —— Core Text字體操作

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.10.22

前言

Core Text框架主要用來做文字處理讽营,是的iOS3.2+OSX10.5+中的文本引擎蝗砾,讓您精細(xì)的控制文本布局和格式踢俄。它位于在UIKit中和CoreGraphics/Quartz之間的最佳點(diǎn)但金。接下來這幾篇我們就主要解析該框架褂微。感興趣的可以前面幾篇友题。
1. Core Text框架詳細(xì)解析(一) —— 基本概覽
2. Core Text框架詳細(xì)解析(二) —— 關(guān)于Core Text
3. Core Text框架詳細(xì)解析(三) —— Core Text總體概覽
4. Core Text框架詳細(xì)解析(四) —— Core Text文本布局操作

字體操作

本章介紹一些常見的字體處理操作嗤堰,并顯示如何使用Core Text進(jìn)行編碼。 這些操作在iOS和OS X上是相同的。本章包含以下代碼列表操作:


Creating Font Descriptors - 創(chuàng)建字體描述符

Listing 3-1中的示例函數(shù)從指定PostScript字體名稱和點(diǎn)大小的參數(shù)值創(chuàng)建一個(gè)字體描述符踢匣。

// Listing 3-1  Creating a font descriptor from a name and point size

CTFontDescriptorRef CreateFontDescriptorFromName(CFStringRef postScriptName,
                                                  CGFloat size)
{
    return CTFontDescriptorCreateWithNameAndSize(postScriptName, size);
}

Listing 3-2中的示例函數(shù)從字體系列名稱和字體特征中創(chuàng)建一個(gè)字體描述符告匠。

// Listing 3-2  Creating a font descriptor from a family and traits

NSString* familyName = @"Papyrus";
CTFontSymbolicTraits symbolicTraits = kCTFontTraitCondensed;
CGFloat size = 24.0;
 
NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
[attributes setObject:familyName forKey:(id)kCTFontFamilyNameAttribute];
 
// The attributes dictionary contains another dictionary, the traits dictionary,
// which in this example specifies only the symbolic traits.
NSMutableDictionary* traits = [NSMutableDictionary dictionary];
[traits setObject:[NSNumber numberWithUnsignedInt:symbolicTraits]
                                           forKey:(id)kCTFontSymbolicTrait];
 
[attributes setObject:traits forKey:(id)kCTFontTraitsAttribute];
[attributes setObject:[NSNumber numberWithFloat:size]
                                         forKey:(id)kCTFontSizeAttribute];
 
CTFontDescriptorRef descriptor =
             CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes);
CFRelease(descriptor);

Creating a Font from a Font Descriptor - 從字體描述符創(chuàng)建字體

Listing 3-3 顯示了如何創(chuàng)建一個(gè)字體描述符并使用它來創(chuàng)建一個(gè)字體。 當(dāng)您調(diào)用CTFontCreateWithFontDescriptor時(shí)离唬,通常會(huì)為矩陣參數(shù)傳遞NULL后专,以指定默認(rèn)(identity)矩陣。 CTFontCreateWithFontDescriptor的大小和矩陣(第二和第三個(gè))參數(shù)將覆蓋字體描述符中指定的任何內(nèi)容输莺,除非它們未指定(大小為0.0戚哎,矩陣為NULL)。

// Listing 3-3  Creating a font from a font descriptor

NSDictionary *fontAttributes =
                  [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Courier", (NSString *)kCTFontFamilyNameAttribute,
                          @"Bold", (NSString *)kCTFontStyleNameAttribute,
                          [NSNumber numberWithFloat:16.0],
                          (NSString *)kCTFontSizeAttribute,
                          nil];
// Create a descriptor.
CTFontDescriptorRef descriptor =
          CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes);
 
// Create a font using the descriptor.
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0.0, NULL);
CFRelease(descriptor);

Creating Related Fonts - 創(chuàng)建相關(guān)字體

將現(xiàn)有字體轉(zhuǎn)換為相關(guān)或相似的字體通常很有用嫂用。 Listing 3-4 中的示例函數(shù)顯示了如何使用函數(shù)調(diào)用傳遞的布爾參數(shù)的值使粗體或粗體字體粗體顯示型凳。 如果當(dāng)前的字體系列沒有請(qǐng)求的樣式,則該函數(shù)返回NULL嘱函。

// Listing 3-4  Changing traits of a font

CTFontRef CreateBoldFont(CTFontRef font, Boolean makeBold)
{
    CTFontSymbolicTraits desiredTrait = 0;
    CTFontSymbolicTraits traitMask;
 
    // If requesting that the font be bold, set the desired trait
    // to be bold.
    if (makeBold) desiredTrait = kCTFontBoldTrait;
 
    // Mask off the bold trait to indicate that it is the only trait
    // to be modified. As CTFontSymbolicTraits is a bit field,
    // could change multiple traits if desired.
    traitMask = kCTFontBoldTrait;
 
    // Create a copy of the original font with the masked trait set to the
    // desired value. If the font family does not have the appropriate style,
    // returns NULL.
 
    return CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, desiredTrait, traitMask);
}

Listing 3-5中的示例函數(shù)將給定字體轉(zhuǎn)換為另一個(gè)字體系列中的類似字體甘畅,如果可能,將保留特征往弓。 它可能返回NULL疏唾。 傳遞大小參數(shù)為0.0和矩陣參數(shù)為NULL以保留原始字體的大小。

// Listing 3-5  Converting a font to another family

CTFontRef CreateFontConvertedToFamily(CTFontRef font, CFStringRef family)
{
    // Create a copy of the original font with the new family. This call
    // attempts to preserve traits, and may return NULL if that is not possible.
    // Pass in 0.0 and NULL for size and matrix to preserve the values from
    // the original font.
 
    return CTFontCreateCopyWithFamily(font, 0.0, NULL, family);
}

Serializing a Font - 序列化字體

Listing 3-6中的示例函數(shù)顯示了如何創(chuàng)建XML數(shù)據(jù)以序列化可以嵌入到文檔中的字體函似。 或者荸实,并且優(yōu)選地,可以使用NSArchiver缴淋。 這只是完成此任務(wù)的一種方法,但它會(huì)保留以后重新創(chuàng)建確切字體所需字體的所有數(shù)據(jù)泄朴。

// Listing 3-6  Serializing a font

CFDataRef CreateFlattenedFontData(CTFontRef font)
{
    CFDataRef           result = NULL;
    CTFontDescriptorRef descriptor;
    CFDictionaryRef     attributes;
 
    // Get the font descriptor for the font.
    descriptor = CTFontCopyFontDescriptor(font);
 
    if (descriptor != NULL) {
        // Get the font attributes from the descriptor. This should be enough
        // information to recreate the descriptor and the font later.
        attributes = CTFontDescriptorCopyAttributes(descriptor);
 
        if (attributes != NULL) {
            // If attributes are a valid property list, directly flatten
            // the property list. Otherwise we may need to analyze the attributes
            // and remove or manually convert them to serializable forms.
            // This is left as an exercise for the reader.
           if (CFPropertyListIsValid(attributes, kCFPropertyListXMLFormat_v1_0)) {
                result = CFPropertyListCreateXMLData(kCFAllocatorDefault, attributes);
            }
        }
    }
    return result;
}

Creating a Font from Serialized Data - 從序列化數(shù)據(jù)中創(chuàng)建字體

Listing 3-7中的示例函數(shù)顯示了如何從展平的XML數(shù)據(jù)創(chuàng)建字體引用重抖。 它顯示如何折疊字體屬性并利用這些屬性創(chuàng)建字體。

// Listing 3-7  Creating a font from serialized data

CTFontRef CreateFontFromFlattenedFontData(CFDataRef iData)
{
    CTFontRef           font = NULL;
    CFDictionaryRef     attributes;
    CTFontDescriptorRef descriptor;
 
    // Create our font attributes from the property list.
    // For simplicity, this example creates an immutable object.
    // If you needed to massage or convert certain attributes
    // from their serializable form to the Core Text usable form,
    // do it here.
    attributes =
          (CFDictionaryRef)CFPropertyListCreateFromXMLData(
                               kCFAllocatorDefault,
                               iData, kCFPropertyListImmutable, NULL);
    if (attributes != NULL) {
        // Create the font descriptor from the attributes.
        descriptor = CTFontDescriptorCreateWithAttributes(attributes);
        if (descriptor != NULL) {
            // Create the font from the font descriptor. This sample uses
            // 0.0 and NULL for the size and matrix parameters. This
            // causes the font to be created with the size and/or matrix
            // that exist in the descriptor, if present. Otherwise default
            // values are used.
            font = CTFontCreateWithFontDescriptor(descriptor, 0.0, NULL);
        }
    }
    return font;
}

Changing Kerning - 改變字距

默認(rèn)情況下啟用連接和字距調(diào)整祖灰。 如果要禁用钟沛,請(qǐng)將kCTKernAttributeName屬性設(shè)置為0。Listing 3-8將前幾個(gè)字符的kern大小設(shè)置為很大的數(shù)字局扶。

// Listing 3-8  Setting kerning

 // Set the color of the first 13 characters to red
 // using a previously defined red CGColor object.
 CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 13),
                                      kCTForegroundColorAttributeName, red);
 
 // Set kerning between the first 18 chars to be 20
 CGFloat otherNum = 20;
 CFNumberRef otherCFNum = CFNumberCreate(NULL, kCFNumberCGFloatType, &otherNum);
 CFAttributedStringSetAttribute(attrString, CFRangeMake(0,18),
                                           kCTKernAttributeName, otherCFNum);

Getting Glyphs for Characters - 獲取字符的字形

Listing 3-9顯示了如何使用單個(gè)字體獲取字符串中的字符的字形恨统。 大多數(shù)時(shí)候你應(yīng)該只使用一個(gè)CTLine對(duì)象獲取這個(gè)信息,因?yàn)橐粋€(gè)字體可能不會(huì)對(duì)整個(gè)字符串進(jìn)行編碼三妈。 此外畜埋,簡(jiǎn)單的字符到字形映射將無法獲得復(fù)雜腳本的正確外觀。 如果您嘗試為字體顯示特定的Unicode字符畴蒲,則這種簡(jiǎn)單的字形映射可能是適當(dāng)?shù)摹?/p>

Listing 3-9  Getting glyphs for characters
void GetGlyphsForCharacters(CTFontRef font, CFStringRef string)
{
    // Get the string length.
    CFIndex count = CFStringGetLength(string);
 
    // Allocate our buffers for characters and glyphs.
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar) * count);
    CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * count);
 
    // Get the characters from the string.
    CFStringGetCharacters(string, CFRangeMake(0, count), characters);
 
    // Get the glyphs for the characters.
    CTFontGetGlyphsForCharacters(font, characters, glyphs, count);
 
    // Do something with the glyphs here. Characters not mapped by this font will be zero.
    // ...
 
    // Free the buffers
    free(characters);
    free(glyphs);
}

后記

未完悠鞍,待續(xù)~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市模燥,隨后出現(xiàn)的幾起案子咖祭,更是在濱河造成了極大的恐慌掩宜,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件么翰,死亡現(xiàn)場(chǎng)離奇詭異牺汤,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)浩嫌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門檐迟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人固该,你說我怎么就攤上這事锅减。” “怎么了伐坏?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵怔匣,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我桦沉,道長(zhǎng)每瞒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任纯露,我火速辦了婚禮剿骨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘埠褪。我一直安慰自己浓利,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布钞速。 她就那樣靜靜地躺著贷掖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪渴语。 梳的紋絲不亂的頭發(fā)上苹威,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音驾凶,去河邊找鬼牙甫。 笑死,一個(gè)胖子當(dāng)著我的面吹牛调违,可吹牛的內(nèi)容都是我干的窟哺。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼翰萨,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼脏答!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤殖告,失蹤者是張志新(化名)和其女友劉穎阿蝶,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體黄绩,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡羡洁,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了爽丹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片筑煮。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖粤蝎,靈堂內(nèi)的尸體忽然破棺而出真仲,到底是詐尸還是另有隱情,我是刑警寧澤初澎,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布秸应,位于F島的核電站,受9級(jí)特大地震影響碑宴,放射性物質(zhì)發(fā)生泄漏软啼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一延柠、第九天 我趴在偏房一處隱蔽的房頂上張望祸挪。 院中可真熱鬧,春花似錦贞间、人聲如沸贿条。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽闪唆。三九已至,卻和暖如春钓葫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背票顾。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國打工础浮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人奠骄。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓豆同,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親含鳞。 傳聞我的和親對(duì)象是個(gè)殘疾皇子影锈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容