IOS利用Core Text对文字进行排版 - 转
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 = ¶graphSpacing;
//创建设置数组
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对文字进行排版 - 转的更多相关文章
- Core Text概述
本文是我翻译的苹果官方文档<Core Text Overview> Core Text框架是高级的底层文字布局和处理字体的技术.它在Mac OS X v10.5 and iOS 3.2开始 ...
- Core Text
Core Text 本文所涉及的代码你可以在这里下载到 https://github.com/kejinlu/CTTest,包含两个项目,一个Mac的NSTextView的测试项目,一个iOS的Cor ...
- Core Text 入门
本文所涉及的代码你可以在这里下载到 https://github.com/kejinlu/CTTest,包含两个项目,一个Mac的NSTextView的测试项目,一个iOS的Core Text的测试项 ...
- iOS圆角view的Swift实现(利用Core Graphics绘制)
iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...
- CoreText学习(一)Base Objects of Core Text
最近要做一个读入Word,PDF格式等的文件并且加以编辑的程序,本来以为使用Text Kit结合Text View来打开doc文件是完全没问题的,结果用了各种方法打开要么是数据是nil,要么打开的文字 ...
- iOS 11: CORE ML—浅析
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/OWD5UEiVu5JpYArcd2H9ig 作者:l ...
- 基于Core Text实现的TXT电子书阅读器
本篇文章的项目地址基于Core Text实现的TXT电子书阅读器. 最近花了一点时间学习了iOS的底层文字处理的框架Core Text.在网上也参考很多资料,具体的资料在文章最后列了出来,有兴趣的可参 ...
- [iOS] 利用 NSAttributedString 进行富文本处理
/iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...
- Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面
Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...
随机推荐
- BZOJ 1091([SCOI2003]分割多边形-分割直线)
1091: [SCOI2003]分割多边形 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 223 Solved: 82 [Submit][id=10 ...
- Windows 8.1更新变化
在上个月微软公布了Windows 8.1更新(KB2919355),假设大家使用的是Windows 8.1的系统,而且启用了自己主动更新,那这个更新就会被自己主动安装.伴随着这个更新,微软同一时 ...
- 【转载】TCP和TCP/IP的区别
TCP/IP协议(Transmission Control Protocol/Internet Protocol)叫做传输控制/网际协议, 又叫网络通讯协议,这个协议是Internet国际互联网络的基 ...
- HDOJ 4455 Substrings 递推+树状数组
pre[i]第i位数往前走多少位碰到和它同样的数 dp[i]表示长度为i的子串,dp[i]能够由dp[i-1]加上从i到n的pre[i]>i-1的数减去最后一段长度为i-1的断中的不同的数得到. ...
- Unable to find a team with the given Team ID或者Failed to code sign的问题解决
Unable to find a team with the given Team ID或者Failed to code sign的问题解决 1:问题描述(注:这种情况一般是下载并打开别人项目时) F ...
- oracle的日期蛋
一切都是扯鸡巴蛋. 在网上查oracle的日期函数用法,得到一大堆语法,林林总总,都是扯鸡巴蛋,没能解决我的问题. 其实,我想写这么一条语句:查找某个日期(不含时分秒)产生或有关的记录.咋写? SQL ...
- 检查 统计 异常 通信 time_wait
[root@hadoop1 conf]# netstat -n | grep -v 127.0.0.1 | grep -v :3306 | grep TIME_WAIT | sort -k 5n | ...
- CXF拦截器(Interceptor)LoggingInInterceptor
Interceptor是CXF架构中一个重要的功能.你可以在不对核心模块进行修改的情况下,动态添加很多功能(你可以想象Struts2拦截器的优点).这对于CXF这个以处理消息为中心的服务框架来说是非常 ...
- JSON: Circular Dependency Errors
If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directiona ...
- 内部消息 微软中国云计算 内測Azure免费账号 赶紧申请 错过不再有
内部消息 微软中国云计算 顶级内測Azure免费账号 火热申请 过期不再有! 微软MSDN俱乐部 29754721, [一大波Azure免费账号来袭]Windows Azure再次开启示放免费试用账 ...