- 第一步,引入CoreText.framework框架。
- //CoreText 跨平台
- 以lable为例
- NSDictionary *attributes = @{
- NSFontAttributeName: [UIFont systemFontOfSize:14],
- NSForegroundColorAttributeName: [UIColor blackColor],
- };
- NSMutableAttributedString *attrtext = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
10.//设置文本属性
- [attrtext addAttribute:NSUnderlineStyleAttributeName
- value:@2
- range:NSMakeRange(0, 3)];
15.//Foundation ---> CoreFoundation
- NSString *s = @"tt";
- CFStringRef s2 = (__bridge CFStringRef)s;
18.//1.通过NSMutableAttributedString 文本属性对象 构造--->CTFramesetterRef
- CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrtext);
20.//2.构造CGPath
- CGMutablePathRef path = CGPathCreateMutable();
22.// CGRectInset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)
- CGPathAddRect(path, NULL, self.bounds);
24.//3.通过CTFramesetterRef和CGPath 构造出一个CTFrameRef
- CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
26.//4.取得绘制的上下文
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- //坐标系统向下平移当前视图的高度
- CGContextTranslateCTM(ctx, 0, CGRectGetHeight(self.bounds));
- //缩放倍数为-1,使用坐标指向反方向
- CGContextScaleCTM(ctx, 1, -1.0);
32.//5.绘制frame
- CTFrameDraw(frame, ctx);
- //取得frmae中所有的行
- CFArrayRef lines = CTFrameGetLines(frame);
- //绘制一行
38.// CTLineDraw(<#CTLineRef line#>, <#CGContextRef context#>)
- CTLineRef line;
- CFArrayRef runs = CTLineGetGlyphRuns(line);
42.// CTRunDraw(<#CTRunRef run#>, <#CGContextRef context#>, <#CFRange range#>)
45.常用开源框架
46.FTCoreText
47.DTCoreText