//
// 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
自定义绘制label
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 如果遇到要另外添加的字体文件,要用自定义字体的话: 用ttf文件或者是otf文件 将这个文件放进去你的项目里面,然...
- 最近一直没有更新简书是因为在开发和测试阶段,有任务,没有进行学习,不过在做任务的时候也遇到了一些技术点,在这里总结...
- 自定义View根据你App的不同需要可以简单,也可以很复杂。 重写onDraw() 自定义View需要重写 onD...
- MPAndroidChart是Android平台上一款强大易用的图表库,支持线状图、柱状图、散点图等八种图表类型和...
