文本绘制在开发客户端程序中是一个比较常用的功能,可分为采用控件和直接绘制两种方式。
采用控件的方式比较简便,添加一个比如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 绘制不规则图形,并且添加点击事件 的两种方法
ios 富文本(图文混排):支持图片,文字,GIF表情
ios 简单绘制文本,线条,图片,动画,按钮
ios中富文本制作,使用系统的coretext库
iOS富文本编辑
IOS滚动文本通知
iOS 动态绘制折线图,需要给定图标x、y轴坐标及最大、小值,以及各折点坐标,即可动态绘制折线图。
iOS中几种截取屏幕图片的方式,从网上搜集的不错的方法
iOS 富文本点击事件,可以添加点击事件,颜色,大小
利用YYLabel准确计算富文本的高度,可用于带表情的评论
iOS富文本编辑器 RichTextEditor ,RichTextEditor 是支持 iPhone 和 iPad 的一个富文本编辑控件。 特性:...
iOS图形绘制 UIBezierPath 绘制折线图、柱状图、饼形图
各种图标的绘制,曲线,折线,图形,饼状图等绘制
1. 渐变色蒙版; 2. 可根据数据设置刻度值; 3.动画绘制; 4.实现有平滑曲线以及及变色蒙版填充;动画圆环绘制;动画绘制折线图和柱状图;
ios MKMapView通过google map api http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@ 绘制地图路径
iOS原生代码实现折线渐变填充绘制动效:从一条直线从下向上变成折线,直线与折线之间是渐变的
ios - 文本重绘
ArcGIS for iOS 2.3开发 绘制点线面
iOS 富文本类库RTLabel
iOS 绘制不规则图形,并且添加点击事件