coreText的一些基础用法
来源:互联网 发布:java知识分享 编辑:程序博客网 时间:2024/05/22 08:18
API接口文档。
https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_Framework_Ref/_index.html
CTFrame 作为一个整体的画布(Canvas),其中由行(CTLine)组成,而每行可以分为一个或多个小方块(CTRun)。
注意:你不需要自己创建CTRun,Core Text将根据NSAttributedString的属性来自动创建CTRun。每个CTRun对象对应不同的属性,正因此,你可以自由的控制字体、颜色、字间距等等信息。
通常处理步聚:
1.使用core text就是先有一个要显示的string,然后定义这个string每个部分的样式->attributedString -> 生成 CTFramesetter -> 得到CTFrame -> 绘制(CTFrameDraw)
其中可以更详细的设置换行方式,对齐方式,绘制区域的大小等。
2.绘制只是显示,点击事件就需要一个判断了。
CTFrame 包含了多个CTLine,并且可以得到各个line的其实位置与大小。判断点击处在不在某个line上。CTLine 又可以判断这个点(相对于ctline的坐标)处的文字范围。然后遍历这个string的所有NSTextCheckingResult,根据result的rang判断点击处在不在这个rang上,从而得到点击的链接与位置。
1 添加CoreText.framework
2 自定义类
- (void)drawRect:(CGRect)rect
{
NSString *text = self.mutableText;
NSMutableAttributedString *mabstring = [[NSMutableAttributedStringalloc]initWithString:text];
//设置字体属性
CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"),40,NULL);
[mabstringaddAttribute:(id)kCTFontAttributeNamevalue:(__bridgeid)font range:NSMakeRange(0,4)];
//设置斜体
CTFontRef font2 =CTFontCreateWithName((CFStringRef)[UIFontitalicSystemFontOfSize:18].fontName,14, NULL);
[mabstringaddAttribute:(id)kCTFontAttributeNamevalue:(__bridgeid)font2 range:NSMakeRange(5,6)];
//设置下划线
[mabstring addAttribute:(id)kCTUnderlineStyleAttributeNamevalue:(id)[NSNumbernumberWithInt:kCTUnderlineStyleThick]range:NSMakeRange(11,5)];
//设置下划线颜色
[mabstring addAttribute:(id)kCTUnderlineColorAttributeNamevalue:(id)[UIColorredColor].CGColorrange:NSMakeRange(11,5)];
//设置字体简隔
long num = 15;
CFNumberRef num1 =CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&num);
[mabstringaddAttribute:(id)kCTKernAttributeNamevalue:(__bridgeid)num1 range:NSMakeRange(20,4)];
//设置空心字
long num2 = 2;
CFNumberRef num3 =CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type, &num2);
[mabstringaddAttribute:(id)kCTStrokeWidthAttributeNamevalue:(__bridgeid)num3 range:NSMakeRange(25,4)];
[mabstring addAttribute:(id)kCTStrokeColorAttributeNamevalue:(id)[UIColorblueColor].CGColorrange:NSMakeRange(25,4)];
//多属性设置
NSMutableDictionary *attributes = [NSMutableDictionarydictionaryWithObject:(id)[UIColorredColor] forKey:(id)kCTForegroundColorAttributeName];
CTFontRef font3 =CTFontCreateWithName((CFStringRef)[UIFontitalicSystemFontOfSize:25].fontName,19,NULL);
[attributessetObject:(__bridgeid)font3 forKey:(id)kCTFontAttributeName];
[attributes setObject:(id)[NSNumbernumberWithInt:kCTUnderlineStyleDouble]forKey:(id)kCTUnderlineStyleAttributeName];
[mabstringaddAttributes:attributes range:NSMakeRange(mabstring.length-6,5)];
//url 设置字体和颜色
//判断是否是url的正则表达式
NSString *regulaStr =@"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:regulaStr options:NSRegularExpressionCaseInsensitiveerror:nil];
//text中符合正则表达式的结果
NSArray * arrayOfAllMatches = [regex matchesInString:text options:0range:NSMakeRange(0, [textlength])];
//设置字体, __bridge关键字来实现id类型与void*类型的相互转换
CTFontRef fontRef =CTFontCreateWithName((__bridgeCFStringRef)[NSStringstringWithFormat:@"Arial"],17, NULL);
//开始设置字体及显示字体的颜色
[arrayOfAllMatchesenumerateObjectsUsingBlock:^(NSTextCheckingResult * obj,NSUInteger idx, BOOL *stop) {
[mabstringaddAttribute:(NSString *)kCTFontAttributeNamevalue:(__bridgeid)fontRef range:obj.range];
[mabstring addAttribute:(NSString *)kCTForegroundColorAttributeNamevalue:(__bridgeid)[UIColorblueColor].CGColorrange:obj.range];
}];
CGContextRef cgc =UIGraphicsGetCurrentContext();
CGContextSaveGState(cgc);
//图像方向转换,在iOS的2D绘图中采用的就是我们熟知的直角坐标系,即原点在左下方,右上为正轴,这里要注意的是和我们在视图(UIView)中布局的坐标系是不一样的,他的圆点在左上,右下为正轴。当我们在视图的drawRect中工作的时候拿到的画板已经是左上坐标的了,那这时候你要去把一个有自己坐标体系的内容直接绘制,就会出现坐标不一致问题,例如直接绘制图片就会倒立
CGContextConcatCTM(cgc,CGAffineTransformScale(CGAffineTransformMakeTranslation(0,self.bounds.size.height),1.f, -1.f));
//读取NSMutableAttributedString
CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString((__bridgeCFAttributedStringRef)mabstring);
CGRect drawingRect = self.bounds;
// 创建一个Path句柄
CGMutablePathRef path =CGPathCreateMutable();
//在此句柄的基础上画出此矩形
CGPathAddRect(path, NULL, drawingRect);
//获取所画text的frame
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path,NULL);
CGPathRelease(path);
CFRelease(framesetter);
//draw begin
CTFrameDraw(textFrame, cgc);
CGContextRestoreGState(cgc);
}
- coreText的一些基础用法
- CoreText ios 上的用法
- CoreText用法
- CoreText 基础
- CoreText -- 基础
- CoreText在ios上的用法
- 基于 CoreText 的排版引擎:基础
- 基于 CoreText 的排版引擎:基础
- 基于CoreText的排版引擎:基础
- 基于 CoreText 的排版引擎:基础
- 基于CoreText的基础排版引擎
- 基于 CoreText 的排版引擎:基础
- 一些基础语句的用法
- HTML的一些基础用法
- DataTable的一些基础用法
- 字典的一些基础用法
- CoreText详细用法
- CoreText --- 基本用法
- 22. 标签管理
- mySql的缓冲区分析
- iOS8 Handoff实现在不同iOS设备上继续做同一件事
- openstack模块之oslo_config
- Access denied for user 'mysql'@'localhost' (using password: NO)
- coreText的一些基础用法
- 23. 操作标签
- 安卓模仿电子词典
- Jcef应用浅析
- python学习笔记二
- 发现一个不错的学习网站
- 1035. Password (20)
- JAVA获取当前时间的三种方法
- 绝对详细!Nginx基本配置、性能优化指南