IOS CoreText.framework --- 基本用法

來源:互聯網
上載者:User

IOS CoreText.framework --- 基本用法

 

CoreText 架構中最常用的幾個類:

 

  1. CTFont
  2. CTFontCollection
  3. CTFontDescriptor
  4. CTFrame
  5. CTFramesetter
  6. CTGlyphInfo
  7. CTLine
  8. CTParagraphStyle
  9. CTRun
  10. CTTextTab
  11. CTTypesetter

     

    先來瞭解一下該架構的整體視窗組合圖:

    CTFrame 作為一個整體的畫布(Canvas),其中由行(CTLine)組成,而每行可以分為一個或多個小方塊(CTRun)。

    注意:你不需要自己建立CTRun,Core Text將根據NSAttributedString的屬性來自動建立CTRun。每個CTRun對象對應不同的屬性,正因此,你可以自由的控制字型、顏色、字間距等等資訊。

    通常處理步聚:

    1.使用core text就是先有一個要顯示的string,然後定義這個string每個部分的樣式->attributedString -> 產生 CTFramesetter -> 得到CTFrame -> 繪製(CTFrameDraw)
    其中可以更詳細的設定換行方式,對齊,繪製地區的大小等。
    2.繪製只是顯示,點擊事件就需要一個判斷了。
    CTFrame 包含了多個CTLine,並且可以得到各個line的其實位置與大小。判斷點擊處在不在某個line上。CTLine 又可以判斷這個點(相對於ctline的座標)處的文字範圍。然後遍曆這個string的所有NSTextCheckingResult,根據result的rang判斷點擊處在不在這個rang上,從而得到點擊的連結與位置。

     

    字型的基本知識:

     

    字型(Font):是一系列字型大小、樣式和磅值相同的字元(例如:10磅黑體Palatino)。現多被視為字樣的同義字

    字面(Face):是所有字型大小的磅值和格式的綜合

    字型集(Font family):是一組相關字型(例如:Franklin family包括Franklin Gothic、Fran-klinHeavy和Franklin Compressed)

    磅值(Weight):用於描述字型粗度。典型的磅值,從最粗到最細,有極細、細、book、中等、半粗、粗、較粗、極粗

    樣式(Style):字形有三種形式:Roman type是直體;oblique type是斜體;utakuc type是斜體兼曲線(比Roman type更像書法體)。

    x高度(X height):指小寫字母的平均高度(以x為基準)。磅值相同的兩字母,x高度越大的字母看起來比x高度小的字母要大

    Cap高度(Cap height):與x高度相似。指大寫字母的平均高度(以C為基準)

    下行字母(Descender):例如在字母q中,基準以下的字母部分叫下伸部分

    上行字母(Ascender):x高度以上的部分(比如字母b)叫做上伸部分

    基準(Baseline):通常在x、v、b、m下的那條線

    描邊(Stroke):組成字元的線或曲線。可以加粗或改變字元形狀

    襯線(Serif):用來使字元更可視的一條水平線。如字母左上方和下部的水平線。

    無襯線(Sans Serif):可以讓排字員不使用襯線裝飾。

    方形字(Block):這種字型的筆畫使字元看起來比無襯線字更顯眼,但還不到常見的襯線字的程度。例如Lubalin Graph就是方形字,這種字看起來好像是木頭塊刻的一樣

    手寫體指令碼(Calligraphic script):是一種仿效手寫體的字型。例如Murray Hill或者Fraktur字型

    藝術字(Decorative):像繪畫般的字型

    Pi符號(Pisymbol):非標準的字母數字字元的特殊符號。例如Wingdings和Mathematical Pi

    連寫(Ligature):是一系列連寫字母如fi、fl、ffi或ffl。由於字些字母形狀的原因經常被連寫,故排字員已習慣將它們連寫。


     

    字元屬性名稱:

     

    const CFStringRef kCTCharacterShapeAttributeName;              
    //字型形狀屬性  必須是CFNumberRef對象預設為0,非0則對應相應的字元形狀定義,如1表示傳統字元形狀
    const CFStringRef kCTFontAttributeName;  
    //字型屬性   必須是CTFont對象const CFStringRef kCTKernAttributeName;                        
    //字元間隔屬性 必須是CFNumberRef對象const CFStringRef kCTLigatureAttributeName;                 
    //設定是否使用連字屬性,設定為0,表示不使用連字屬性。標準的英文連字有FI,FL.預設值為1,既是使用標準連字。也就是當搜尋到f時候,會把fl當成一個文字。必須是CFNumberRef 預設為1,可取0,1,2const CFStringRef kCTForegroundColorAttributeName;             
    //字型顏色屬性  必須是CGColor對象,預設為blackconst CFStringRef kCTForegroundColorFromContextAttributeName; 
     //內容相關的字型顏色屬性 必須為CFBooleanRef 預設為False,const CFStringRef kCTParagraphStyleAttributeName;              
    //段落樣式屬性 必須是CTParagraphStyle對象 預設為NILconst CFStringRef kCTStrokeWidthAttributeName;              
    //筆畫線條寬度 必須是CFNumberRef對象,默為0.0f,標準為3.0fconst CFStringRef kCTStrokeColorAttributeName;              
    //筆畫的顏色屬性 必須是CGColorRef 對象,預設為前景色彩const CFStringRef kCTSuperscriptAttributeName;              
    //設定字型的上下標屬性 必須是CFNumberRef對象 預設為0,可為-1為下標,1為上標,需要字型支援才行。如排列組合的樣式Cn1const CFStringRef kCTUnderlineColorAttributeName;           
    //字型底線顏色屬性 必須是CGColorRef對象,預設為前景色彩const CFStringRef kCTUnderlineStyleAttributeName;           
    //字型底線樣式屬性 必須是CFNumberRef對象,默為kCTUnderlineStyleNone 可以通過CTUnderlineStypleModifiers 進行修改底線風格const CFStringRef kCTVerticalFormsAttributeName;
    //文字的字形方向屬性 必須是CFBooleanRef 預設為false,false表示水平方向,true表示豎直方向const CFStringRef kCTGlyphInfoAttributeName;
    //字型資訊屬性 必須是CTGlyphInfo對象const CFStringRef kCTRunDelegateAttributeName
    //CTRun 委託屬性 必須是CTRunDelegate對象
    舉例說明:

     

     

    [cpp] view plaincopy
    1. NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:@This is a test of characterAttribute. 中文字元];

       

       

      [cpp] view plaincopy
      1. //設定字型屬性
      2. CTFontRef font = CTFontCreateWithName(CFSTR(Georgia), 40, NULL);
      3. [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
        [cpp] view plaincopy
        1. //設定斜體字
        2. CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);
        3. [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
          [cpp] view plaincopy
          1. //底線
          2. [mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];
            [cpp] view plaincopy
            1. //底線顏色
            2. [mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];

              [cpp] view plaincopy
              1. //設定字型簡隔 eg:test
              2. long number = 10;
              3. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
              4. [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];
                [cpp] view plaincopy
                1. //設定連字
                2. long number = 1;
                3. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                4. [mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])]; 連字還不會使用,未看到效果。
                  [cpp] view plaincopy
                  1. //設定字型顏色
                  2. [mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];
                    [cpp] view plaincopy
                    1. //設定字型顏色為前影色
                    2. CFBooleanRef flag = kCFBooleanTrue;
                    3. [mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)]; 無明顯效果。

                       

                       

                      [cpp] view plaincopy
                      1. //設定空心字
                      2. long number = 2;
                      3. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                      4. [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
                        [cpp] view plaincopy
                        1. //設定空心字
                        2. long number = 2;
                        3. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                        4. [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
                        5.  
                        6. //設定空心字顏色
                        7. [mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];
                          在設定空心字顏色時,必須先將字型高為空白心,否則設定顏色是沒有效果的。

                           

                          [cpp] view plaincopy
                          1. //對同一段字型進行多屬性設定
                          2. //紅色
                          3. NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];
                          4. //斜體
                          5. CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);
                          6. [attributes setObject:(id)font forKey:(id)kCTFontAttributeName];
                          7. //底線
                          8. [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];
                          9.  
                          10. [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];

                             

                            最後是draw了。

                             

                            [cpp] view plaincopy
                            1. -(void)characterAttribute
                            2. {
                            3. NSString *str = @This is a test of characterAttribute. 中文字元;
                            4. NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:str];
                            5.  
                            6. [mabstring beginEditing];
                            7. /*
                            8. long number = 1;
                            9. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                            10. [mabstring addAttribute:(id)kCTCharacterShapeAttributeName value:(id)num range:NSMakeRange(0, 4)];
                            11. */
                            12. /*
                            13. //設定字型屬性
                            14. CTFontRef font = CTFontCreateWithName(CFSTR(Georgia), 40, NULL);
                            15. [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
                            16. */
                            17. /*
                            18. //設定字型簡隔 eg:test
                            19. long number = 10;
                            20. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                            21. [mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];
                            22. */
                            23.  
                            24. /*
                            25. long number = 1;
                            26. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                            27. [mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])];
                            28. */
                            29. /*
                            30. //設定字型顏色
                            31. [mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];
                            32. */
                            33. /*
                            34. //設定字型顏色為前影色
                            35. CFBooleanRef flag = kCFBooleanTrue;
                            36. [mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)];
                            37. */
                            38.  
                            39. /*
                            40. //設定空心字
                            41. long number = 2;
                            42. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                            43. [mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
                            44.  
                            45. //設定空心字顏色
                            46. [mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];
                            47. */
                            48.  
                            49. /*
                            50. long number = 1;
                            51. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
                            52. [mabstring addAttribute:(id)kCTSuperscriptAttributeName value:(id)num range:NSMakeRange(3, 1)];
                            53. */
                            54.  
                            55. /*
                            56. //設定斜體字
                            57. CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);
                            58. [mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
                            59. */
                            60.  
                            61. /*
                            62. //底線
                            63. [mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];
                            64. //底線顏色
                            65. [mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];
                            66. */
                            67.  
                            68.  
                            69.  
                            70. //對同一段字型進行多屬性設定
                            71. //紅色
                            72. NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];
                            73. //斜體
                            74. CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);
                            75. [attributes setObject:(id)font forKey:(id)kCTFontAttributeName];
                            76. //底線
                            77. [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];
                            78.  
                            79. [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];
                            80.  
                            81.  
                            82.  
                            83. NSRange kk = NSMakeRange(0, 4);
                            84.  
                            85. NSDictionary * dc = [mabstring attributesAtIndex:0 effectiveRange:&kk];
                            86.  
                            87. [mabstring endEditing];
                            88.  
                            89. NSLog(@value = %@,dc);
                            90.  
                            91.  
                            92.  
                            93. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring);
                            94.  
                            95. CGMutablePathRef Path = CGPathCreateMutable();
                            96.  
                            97. CGPathAddRect(Path, NULL ,CGRectMake(10 , 0 ,self.bounds.size.width-10 , self.bounds.size.height-10));
                            98.  
                            99. CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);
                            100.  
                            101. //擷取當前(View)上下文以便於之後的繪畫,這個是一個離屏。
                            102. CGContextRef context = UIGraphicsGetCurrentContext();
                            103.  
                            104. CGContextSetTextMatrix(context , CGAffineTransformIdentity);
                            105.  
                            106. //壓棧,壓入圖形狀態棧中.每個圖形上下文維護一個圖形狀態棧,並不是所有的當前繪畫環境的圖形狀態的元素都被儲存。圖形狀態中不考慮當前路徑,所以不儲存
                            107. //儲存現在得上下文圖形狀態。不管後續對context上繪製什麼都不會影響真正得螢幕。
                            108. CGContextSaveGState(context);
                            109.  
                            110. //x,y軸方向移動
                            111. CGContextTranslateCTM(context , 0 ,self.bounds.size.height);
                            112.  
                            113. //縮放x,y軸方向縮放,-1.0為反向1.0倍,座標系轉換,沿x軸翻轉180度
                            114. CGContextScaleCTM(context, 1.0 ,-1.0);
                            115.  
                            116. CTFrameDraw(frame,context);
                            117.  
                            118. CGPathRelease(Path);
                            119. CFRelease(framesetter);
                            120. }
                              [cpp] view plaincopy
                              1. - (void)drawRect:(CGRect)rect
                              2. {
                              3. [self characterAttribute];
                              4. }

                                CORETEXT架構圖

                                另對於Context的瞭解可以參考:http://www.padovo.com/blog/2013/01/31/study-coretext/

                                 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.