设为首页 加入收藏

TOP

NSAttributedString使用介绍(一)
2014-11-23 21:53:53 来源: 作者: 【 】 浏览:28
Tags:NSAttributedString 使用 介绍
首先导入CoreText.framework,并在需要使用的文件中导入:
#import
创建一个NSMutableAttributedString:
NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"]
autorelease];
非常常规的创建方式,接下来我们给它配置属性:
//把this的字体颜色变为红色
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[UIColor redColor].CGColor
range:NSMakeRange(0, 4)];
//把is变为黄色
[attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[UIColor yellowColor].CGColor
range:NSMakeRange(5, 2)];
//改变this的字体,value必须是一个CTFontRef
[attriString addAttribute:(NSString *)kCTFontAttributeName
value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,
14,
NULL)
range:NSMakeRange(0, 4)];
//给this加上下划线,value可以在指定的枚举中选择
[attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]
range:NSMakeRange(0, 4)];
return attriString;
这样就算是配置好了,但是我们可以发现NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。写一个UIView的子类(假设命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:
-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
NSAttributedString *attriString = getAttributedString();
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(path);
CFRelease(framesetter);
CTFrameDraw(frame, ctx);
CFRelease(frame);
}
在代码中我们调整了CTM(current transformation matrix),这是因为Quartz 2D的坐标 系统不同,比如(10, 10)到(20, 20)的直线坐标:
坐标类似于数学中的坐标,可以先不调整CTM,看它是什么样子的,下面两种调整方法是完全一样的:
CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
==
CGContextTranslateCTM(ctx, 0, rect.size.height);
CGContextScaleCTM(ctx, 1, -1);
CTFramesetter是CTFrame的创建工厂,NSAttributedString需要通过CTFrame绘制到界面上,得到CTFramesetter后,创建path(绘制路径),然后得到CTFrame,最后通过CTFrameDraw方法绘制到界面上。
如果想要计算NSAttributedString所要的size,就需要用到这个API:
CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行时会算不准的,因为在CoreText里,行间距也是你来控制的。
设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为很多子
属性,其中就包括
kCTLineBreakByCharWrapping
kCTParagraphStyleSpecifierLineSpacingAdjustment
设置如下:
//段落
//line break
CTParagraphStyleSetting lineBreakMode;
CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping;
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言/C++中怎样产生随机数 下一篇C语言常见问题之字符串数组和字符..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: