iOS上文本繪製的幾種方法

來源:互聯網
上載者:User

標籤:

文本繪製在開發用戶端程式中是一個比較常用的功能,可分為採用控制項和直接繪製兩種方式。

採用控制項的方式比較簡便,添加一個比如UILabel對象,然後設定相關屬性就好了。但這種方式局限性也比較大。

直接繪製相對比較自由,但也分為使用NSString和Quartz 2D兩種方式。

NSString有一組繪製文本的函數,drawAtPoint是其中一個。使用方式如下:

1 NSString* text = @"This is English text(NSString).";2 [text drawAtPoint:CGPointMake(0, 0) withFont:[UIFont systemFontOfSize:20]];

介面還是比較簡單的,也可以畫中文。

1 text = @"這是中文文本(NSString)。";2 [text drawAtPoint:CGPointMake(0, 50) withFont:[UIFont systemFontOfSize:20]];

Quartz 2D中文本繪製稍微複雜一點,因為它提供的介面是C形式的,而不是OC的。先來看看如何畫英文:

1 CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));2 CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);3 const char* str = "This is English text(Quartz 2D).";4 CGContextShowTextAtPoint(context, 0, 100, str, strlen(str));

CGContextSetTextMatrix是調整座標系,防止文字倒立。
我們用同樣的方法嘗試繪製中文。

1 const char* str1 = "這是中文文本(Quartz 2D)。";2 CGContextShowTextAtPoint(context, 0, 150, str1, strlen(str1));

但螢幕上顯示的是亂碼。為什麼呢?

Quartz 2D Programming Guide中有這樣一段說明:

To set the font to a text encoding other than MacRoman, you can use the functions CGContextSetFont and CGContextSetFontSize. You must supply a CGFont object to the function CGContextSetFont. You call the function CGFontCreateWithPlatformFont to obtain a CGFont object from an ATS font. When you are ready to draw the text, you use the function CGContextShowGlyphsAtPoint rather than CGContextShowTextAtPoint.

人家說了,如果編碼超出MacRoman的範圍,你要使用CGContextShowGlyphsAtPoint來繪製。這個函數和CGContextShowTextAtPoint類似,也是5個參數,而且只有第四個參數不同,是字形數組(可能描述的不準確)CGGlyph glyphs[],這個東西如何得到呢?在CoreText frameork(support iOS3.2 and later)提供了這樣的介面。代碼如下:

 1 UniChar *characters; 2 CGGlyph *glyphs; 3 CFIndex count; 4  5 CTFontRef ctFont = CTFontCreateWithName(CFSTR("STHeitiSC-Light"), 20.0, NULL); 6 CTFontDescriptorRef ctFontDesRef = CTFontCopyFontDescriptor(ctFont); 7 CGFontRef cgFont = CTFontCopyGraphicsFont(ctFont,&ctFontDesRef );  8 CGContextSetFont(context, cgFont); 9 CFNumberRef pointSizeRef = (CFNumberRef)CTFontDescriptorCopyAttribute(ctFontDesRef,kCTFontSizeAttribute);10 CGFloat fontSize;11 CFNumberGetValue(pointSizeRef, kCFNumberCGFloatType,&fontSize);12 CGContextSetFontSize(context, fontSize);13 NSString* str2 = @"這是中文文本(Quartz 2D)。";14 count = CFStringGetLength((CFStringRef)str2);15 characters = (UniChar *)malloc(sizeof(UniChar) * count);16 glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * count);17 CFStringGetCharacters((CFStringRef)str2, CFRangeMake(0, count), characters);18 CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, count);19 CGContextShowGlyphsAtPoint(context, 0, 200, glyphs, str2.length);20 21 free(characters);22 free(glyphs);

STHeitiSC-Light是系統內建的一種中文字型。

這樣寫的話中文就能正常繪製出來了。

是顯示效果,分別對應上面的5個樣本。

iOS上文本繪製的幾種方法

聯繫我們

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