原贴地址:http://hi.baidu.com/jwq359699768/blog/item/5df305c893413d0a7e3e6f7b.html

core text 这个包默认是没有的,要自己手动添加进来。

在IOS中利用core text对文本进行排版的几个关键点如下:

字间距:kCTKernAttributeName

行间距:kCTParagraphStyleSpecifierLineSpacingAdjustment 或 kCTParagraphStyleSpecifierLineSpacing(不推荐使用)

段间距:kCTParagraphStyleSpecifierParagraphSpacing

文本对齐方式:kCTParagraphStyleSpecifierAlignment;

还有一点就是core text显示出来的字是颠倒的,使用时要翻转下:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetTextMatrix(context,CGAffineTransformIdentity);

CGContextTranslateCTM(context,0,self.bounds.size.height);

CGContextScaleCTM(context,1.0,-1.0);

最后一点要注意的是Mac下的回车和Windows的是不一样的,Windows下的回车是由\r \n组成的而Mac下只有一个\n,所以如果没有去掉的话在每一段的最后都会多出一个空行来,去掉的方法如下:

NSString *myString = [labelString stringByReplacingOccurrencesOfString:"\r\n" withString:"\n"];

具体的代码实现如下:

#import<Foundation/Foundation.h>

#import<UIKit/UIKit.h>

@interface TextLayoutLabel : UILabel

{

@private

CGFloat characterSpacing_;       //字间距

@private

long linesSpacing_;   //行间距

}

@property(nonatomic,assign) CGFloat characterSpacing;

@propery(nonatomic,assign)long linesSpacing;

@end

#import "TextLayoutLabel.h"

#import<CoreText/CoreText.h>

@implementation TextLayoutLabel

@synthesize characterSpacing = characterSpacing_;

@synthesize linesSpacing = linesSpacing_;

-(id) initWithFrame:(CGRect)frame

{//初始化字间距、行间距

if(self =[super initWithFrame:frame])

{

self.characterSpacing = 2.0f;

self.linesSpacing = 5.0f;

}

return self;

}

-(void)setCharacterSpacing:(CGFloat)characterSpacing //外部调用设置字间距

{

characterSpacing_ = characterSpacing;

[self setNeedsDisplay];

}

-(void)setLinesSpacing:(long)linesSpacing  //外部调用设置行间距

{

linesSpacing_ = linesSpacing;

[self setNeedsDisplay];

}

-(void) drawTextInRect:(CGRect)requestedRect

{

//去掉空行

NSString *labelString = self.text;

NSString *myString = [labelString stringByReplacingOccurrencesOfString:@"\r\n" withString:"\n"];

//创建AttributeString

NSMutableAttributedString *string =[[NSMutableAttributedString alloc]initWithString:self.text];

//设置字体及大小

CTFontRef helveticaBold = CTFontCreateWithName((CFStringRef)self.font.fontName,self.font.pointSize,NULL);

[string addAttribute:(id)kCTFontAttributeName value:(id)helveticaBold range:NSMakeRange(0,[string length])];

//设置字间距

if(self.characterSpacing)

{

long number = self.characterSpacing;

CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);

[string addAttribute:(id)kCTkernAttributeName value:(id)num rang:NSMakeRange(0,[string length])];

CFRelease(num);

}

//设置字体颜色

[string addAttribute:(id)kCTForegroundColorAttributeName value:(id)(self.textColor.CGColor) range:NSMakeRange(0,[string length])];

//创建文本对齐方式

CTTextAlignment alignment = kCTLeftTextAlignment;

if(self.textAlignment == UITextAlignmentCenter)

{

alignment = kCTCenterTextAlignment;

}

if(self.textAlignment == UITextAlignmentRight)

{

alignment = kCTRightTextAlignment;

}

CTParagraphStyleSetting alignmentStyle;

alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment;

alignmentStyle.valueSize = sizeof(alignment);

alignmentStyle.value = &alignment;

//设置文本行间距

CGFloat lineSpace = self.linesSpacing;

CTParagraphStyleSetting lineSpaceStyle;

lineSpaceStyle.spec = kCTparagraphStyleSpecifierLineSpacingAdjustment;

lineSpaceStyle.valueSize = sizeof(lineSpace);

lineSpaceStyle.value =&lineSpace;

//设置文本段间距

CGFloat paragraphSpacing = 5.0;

CTparagraphStyleSetting paragraphSpaceStyle;

paragraphSpaceStyle.spec = kCTparagraphStyleSpecifierParagraphSpacing;

paragraphSpaceStyle.valueSize = sizeof(CGFloat);

paragraphSpaceStyle.value = &paragraphSpacing;

//创建设置数组

CTParagraphStyleSetting settings[ ] ={alignmentStyle,lineSpaceStyle,paragraphSpaceStyle};

CTParagraphStyleRef style = CTParagraphStyleCreate(settings , sizeof(settings));

//给文本添加设置

[string addAttribute:(id)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0 , [string length])];

//排版

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);

CGMutablePathRef leftColumnPath = CGPathCreateMutable();

CGPathAddRect(leftColumnPath, NULL ,CGRectMake(0 , 0 ,self.bounds.size.width , self.bounds.size.height));

CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0), leftColumnPath , NULL);

//翻转坐标系统(文本原来是倒的要翻转下)

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetTextMatrix(context , CGAffineTransformIdentity);

CGContextTranslateCTM(context , 0 ,self.bounds.size.height);

CGContextScaleCTM(context, 1.0 ,-1.0);

//画出文本

CTFrameDraw(leftFrame,context);

//释放

CGPathRelease(leftColumnPath);

CFReleale(framesetter);

CFRelease(helveticaBold);

[string release];

UIGraphicsPushContext(context);

}

@end

IOS利用Core Text对文字进行排版 - 转的更多相关文章

  1. Core Text概述

    本文是我翻译的苹果官方文档<Core Text Overview> Core Text框架是高级的底层文字布局和处理字体的技术.它在Mac OS X v10.5 and iOS 3.2开始 ...

  2. Core Text

    Core Text 本文所涉及的代码你可以在这里下载到 https://github.com/kejinlu/CTTest,包含两个项目,一个Mac的NSTextView的测试项目,一个iOS的Cor ...

  3. Core Text 入门

    本文所涉及的代码你可以在这里下载到 https://github.com/kejinlu/CTTest,包含两个项目,一个Mac的NSTextView的测试项目,一个iOS的Core Text的测试项 ...

  4. iOS圆角view的Swift实现(利用Core Graphics绘制)

    iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...

  5. CoreText学习(一)Base Objects of Core Text

    最近要做一个读入Word,PDF格式等的文件并且加以编辑的程序,本来以为使用Text Kit结合Text View来打开doc文件是完全没问题的,结果用了各种方法打开要么是数据是nil,要么打开的文字 ...

  6. iOS 11: CORE ML—浅析

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/OWD5UEiVu5JpYArcd2H9ig 作者:l ...

  7. 基于Core Text实现的TXT电子书阅读器

    本篇文章的项目地址基于Core Text实现的TXT电子书阅读器. 最近花了一点时间学习了iOS的底层文字处理的框架Core Text.在网上也参考很多资料,具体的资料在文章最后列了出来,有兴趣的可参 ...

  8. [iOS] 利用 NSAttributedString 进行富文本处理

    /iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...

  9. Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面

    Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...

随机推荐

  1. LoadRunner 中调用c函数生成随机字符串

    Action() { int itera_num,rand_num,i; ]=""; char StrTable[]="abcdefghijklmnopqrstuvwxy ...

  2. Ubuntu14.04 64bit下Caffe + CUDA 6.5安装详细步骤

    不多说,直接上干货! 笔者花了很长时间才装完,主要是cuda安装和opencv安装比较费劲,cuda找不到32位的安装包只好重装64位的ubuntu系统,opencv 也是尝试了很久才解决,这里建议用 ...

  3. 常见网络摄像机默认使用的端口,RTSP地址

    品牌 默认IP地址 WEB RTSP HTTPS 数据 ONVIF   海康威视 192.168.1.64/DHCP用户名admin 密码自己设 80 554 443 8000 80   大华 192 ...

  4. 判断一个包是否可以安装是一个NP-complete问题

    1 checking whether a single package P can be installed, given a repository R,is NP-complete

  5. mysql 转换编码方式

    进入mysql 的安装文件夹找到 “ my.ini” 文件  (mysql配置文件) 一.编辑MySql的配置文件 vim /etc/my.cnf 在 [mysqld] 标签下加上三行 default ...

  6. 迭代器-iteration

    class CoffrrIterator implements Iterator<Coffee> { int cunt = size; public boolean hasNext() { ...

  7. java元组-泛型

    需要组合对象的时候使用元组可以简化代码,不需要每当需要组合类的时候都去创建一个新的对象.单元素就是常见的泛型,可以两个三个到多个元素:元组可以继承:java泛型不能使用基本类型如int long 必须 ...

  8. HDU1069 Monkey and Banana —— DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS ...

  9. YTU 2864: 分跑道。

    2864: 分跑道. 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 19 题目描述 有N个人参加100米短跑比赛.跑道为8条.程序的任务是按照尽量使每组的人数相差最少的原 ...

  10. [Usaco2017 Dec] A Pie for a Pie

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5140 [算法] 最短路 时间复杂度 : O(N^2) [代码] #include&l ...