ios-CoreText-核心文本

<pre name="code" class="objc">在iOS中怎样才能将一个字符串绘制到屏幕上呢?

简单来说,是通过控件来完成的,而这些控件都封装在UIKit框架中(对于Mac OS X是AppKit框架),在UIKit中常用来在屏幕上显示字符串的控件有3个:
        UILabel
        UITextField
        UITextView
    然而这些控件本身对文本的展现方式很单一,通常仅仅能够控制字体样式、大小、颜色、加粗、斜体等等,而对于行距控制,字距控制,段落控制等高级功能却无能为力。

此时不免要提起一个非常强大的文本排版框架CoreText.framework。
    CoreText框架是基于 iOS 3.2+ 和 OSX 10.5+ 的一种能够对文本格式和文本布局进行精细控制的文本引擎。它良好的结合了 UIKit 和 Core Graphics/Quartz.


 UIKit 的 UILabel 允许你通过在 IB 中简单的拖曳添加文本,但你不能改变文本的颜色和其中的单词。

    Core Graphics/Quartz几乎允许你做任何系统允许的事情,但你需要为每个字形计算位置,并画在屏幕上。

    CoreText正结合了这两者!你自己可以完全控制位置、布局、类似文本大小和颜色这样的属性,CoreText将帮你完善其它的东西——类似文本换行、字体呈现等等。


#import "CoreTextView.h"#import <CoreText/CoreText.h>#import <CoreText/CTFont.h>@implementation CoreTextView//展示字符串的特征属性-(void)characterAttribute{ NSString *str = @"This is a test of characterAttribute. 中文字符"; //富文本字符串 NSMutableAttributedString *mabsting = [[NSMutableAttributedString alloc]initWithString:str]; //设置字体属性 有错误!!! //这里不使用指针 /* CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);// CTFontRef *font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL); [mabsting addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, 4)];*/ //设置字体的间隔// long number = 10;// CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);// // [mabsting addAttribute:(id)kCTKernAttributeName value:(__bridge id)(num) range:NSMakeRange(10, 4)]; /* //设置字体颜色 [mabsting addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)]; //设置空心字 long number = 10; CFNumberRef num= CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number); [mabsting addAttribute:(id)kCTStrokeWidthAttributeName value:(__bridge id)(num) range:NSMakeRange(0, [str length])]; //设置空心字的颜色 [mabsting addAttribute:(id)kCTStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, [str length])]; */ /* //设置斜体 CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 50, NULL); [mabsting addAttribute:(id)kCTFontAttributeName value:(__bridge id)(font) range:NSMakeRange(0, 4)]; */ /* //下滑线 [mabsting addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)]; //下划线颜色 [mabsting addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)]; */ #warning 有问题 //对同一段字体进行多属性的设置 NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName]; //斜体 CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL); [attributes setObject:(__bridge id)(font) forKey:(id)kCTFontAttributeName]; //下划线 [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName]; [mabsting addAttributes:attributes range:NSMakeRange(0, 4)]; NSRange kk = NSMakeRange(0, 4); NSDictionary *dc = [mabsting attributesAtIndex:0 effectiveRange:&kk]; NSLog(@"value: = %@",dc); //结束编辑状态 [mabsting endEditing]; /**下面的代码将文字绘制在屏幕上**/ //引入核心文本frame对象 CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabsting); //给个路径对象 CGMutablePathRef path = CGPathCreateMutable() ; //修改文字显示的坐标范围 CGPathAddRect(path, NULL, CGRectMake(10, 0, self.bounds.size.width -10, self.bounds.size.height -10)); //建立画布 CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); //获取当前的(view)的上下文 便于绘画 CGContextRef context = UIGraphicsGetCurrentContext(); //用上面创建的变换来设置文本矩阵 CGContextSetTextMatrix(context, CGAffineTransformIdentity); //保存现在上下文的图形状态 ,后续的绘制不会影响到真正的屏幕 CGContextSaveGState(context); //x,y轴方向移动 CGContextTranslateCTM(context, 0, self.bounds.size.height); //x,y 轴缩放 -1.0 就是反向的1.0倍 沿x轴旋转180‘ CGContextScaleCTM(context, 1.0, -1.0); //绘制到屏幕上 CTFrameDraw(frame, context);#warning -核心类的arc 不管释放- //释放 CGPathRelease(path); CFRelease(framesetter); }// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code [self characterAttribute];}@end


你可能感兴趣的:(框架,mac,OS,控件,X,UIKit,核心文本)