iOS:CoreText的常用语法

CoreText的关键语法

 

一、坐标旋转

-(void)drawRect:(CGRect)rect
{

    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
 

    //在上下文中,创建一个标准坐标系
    CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
 

    //将上下文中的原点,进行平行移动,下面是沿着y轴方向移动,从而转换成屏幕坐标系
    CGContextTranslateCTM(ctx, 0, self.bounds.size.height);
 

    //翻转y轴正方
    CGContextScaleCTM(ctx, 1.0, -1.0);
    

    //绘制尺寸(stringModel是字符串模型,通过它提前算好了绘制尺寸),直接进行绘制
    CTFrameRef ctFrame = self.stringModel.ctFrame;
    CTFrameDraw(ctFrame, ctx);
}

 

二、主要键名

//1、设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
NSFontAttributeName            

 
//2、设置字体颜色,取值为 UIColor对象,默认值为黑色          
NSForegroundColorAttributeName

       
//3、设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
NSBackgroundColorAttributeName     

 
//4、设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
NSLigatureAttributeName                


//5、设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
NSKernAttributeName                     

 
//6、设置删除线,取值为 NSNumber 对象(整数)
NSStrikethroughStyleAttributeName  

 
//7、设置删除线颜色,取值为 UIColor 对象,默认值为黑色
NSStrikethroughColorAttributeName  

 
//8、设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
NSUnderlineStyleAttributeName        

 
//9、设置下划线颜色,取值为 UIColor 对象,默认值为黑色
NSUnderlineColorAttributeName        

 
//10、设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
NSStrokeWidthAttributeName           

 
//11、填充部分颜色,不是字体颜色,取值为 UIColor 对象
NSStrokeColorAttributeName           

 
//12、设置阴影属性,取值为 NSShadow 对象
NSShadowAttributeName                

 
//13、设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
NSTextEffectAttributeName             

 
//14、设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
NSBaselineOffsetAttributeName        

 
//15、设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
NSObliquenessAttributeName            

 
//16、设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
NSExpansionAttributeName              

 
//17、设置文字书写方向,从左向右书写或者从右向左书写
NSWritingDirectionAttributeName      

 
//18、设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
NSVerticalGlyphFormAttributeName   

 
//19、设置链接属性,点击后调用浏览器打开指定URL地址
NSLinkAttributeName                      

 
//20、设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
NSAttachmentAttributeName            

 
//21、设置文本段落排版格式,取值为 NSParagraphStyle 对象
NSParagraphStyleAttributeName      

 

三、主要函数

//1、传入CTFrame,返回一个装有多个CTLine对象的数组。
CFArrayRef CTFrameGetLines( CTFrameRef frame ) CT_AVAILABLE(10_5, 3_2);


//2、获取数组中的元素个数
CFIndex CFArrayGetCount(CFArrayRef theArray);

 
//3、获取数组中第idx个元素
const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx);


//4、 获取所有CTLineRef的基础原点,传入CTFrame,CFRange,和一个CGPoint的结构体数组指针,该函数会把每一个CTLine的origin坐标写到数组里。
void CTFrameGetLineOrigins(CTFrameRef frame, CFRange range, CGPoint origins[] ) CT_AVAILABLE(10_5, 3_2);


//5、获取CTLine中文字在整段文字中的Range
CFRange CTLineGetStringRange( CTLineRef line ) CT_AVAILABLE(10_5, 3_2);


//6、获取CTLine中的CTRun的数组
CFArrayRef CTLineGetGlyphRuns( CTLineRef line ) CT_AVAILABLE(10_5, 3_2);
 

//7、获取CTRun在整段文字中的Range
CFRange CTRunGetStringRange( CTRunRef run ) CT_AVAILABLE(10_5, 3_2);
 

//8、 获取点击处position文字在整段文字中的index
CFIndex CTLineGetStringIndexForPosition(CTLineRef line, CGPoint position ) CT_AVAILABLE(10_5, 3_2);
 

//9、获取整段文字中charIndex位置的字符相对line的原点的x值
CGFloat CTLineGetOffsetForStringIndex(CTLineRef line,CFIndex charIndex,CGFloat * __nullable secondaryOffset ) CT_AVAILABLE(10_5, 3_2);


//10、获取数组中字形个个数
CFIndex CTLineGetGlyphCount( CTLineRef line ) CT_AVAILABLE(10_5, 3_2);
 

//11、设置CoreText绘制前的坐标。设置基线位置
CG_EXTERN void CGContextSetTextPosition(CGContextRef __nullable c,
    CGFloat x, CGFloat y)
    CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

 
//12、绘制CTLine。
void CTLineDraw( CTLineRef line, CGContextRef context ) CT_AVAILABLE(10_5, 3_2);
 

//13、获取CTLine的上行高度,下行高度,行距
double CTLineGetTypographicBounds(
    CTLineRef line,
    CGFloat * __nullable ascent,
    CGFloat * __nullable descent,
    CGFloat * __nullable leading ) CT_AVAILABLE(10_5, 3_2);
 

//14、设置当前文本矩阵
CG_EXTERN void CGContextSetTextMatrix(
    CGContextRef __nullable c,
    CGAffineTransform t)
    CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
 

//15、获取一行文字的范围, 就是指把这一行文字点有的像素矩阵作为一个image图片,来得到整个矩形区域。相对于每一行基线原点的偏移量和宽高(例如:{{1.2, -2.57227}, {208.025, 19.2523}},就是相对于本身的基线原点向右偏移1.2个单位,向下偏移2.57227个单位,后面是宽高)
CGRect CTLineGetImageBounds(
    CTLineRef line,
    CGContextRef __nullable context ) CT_AVAILABLE(10_5, 3_2);

 

 

posted @ 2017-09-05 11:29  XYQ全哥  阅读(707)  评论(0编辑  收藏  举报