//
// PHCLabel.m
// TextKit
//
// Created by phc on 16/8/27.
// Copyright © 2016年 phc. All rights reserved.
//
#import "PHCLabel.h"
#import <CoreText/CoreText.h>
@implementation PHCLabel
-(instancetype)initWithFrame:(CGRect)frame{
if ([super initWithFrame:frame]) {
//默认的设置
self.textColor = [UIColor blackColor];
self.textFont = [UIFont systemFontOfSize:15.0];
}
return self;
}
-(void)drawRect:(CGRect)rect{
/**
CTFrame
CTLine
CTRun
*/
NSDictionary *dic = @{
NSForegroundColorAttributeName :[UIColor redColor]
};
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:self.text attributes:dic];
[attribute addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:[self.text rangeOfString:@"ddd"]];
//设置器
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) attribute);
//获取路径
CGMutablePathRef path = CGPathCreateMutable();
//绘制具体的路径
CGPathAddRect(path, NULL, self.bounds);
//CTFrame
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
//获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//改变坐标系统
CGContextTranslateCTM(context, 0, CGRectGetHeight(self.bounds));
CGContextScaleCTM(context, 1, -1);
//绘制
CTFrameDraw(frame, context);
}
@end
网友评论