博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS 利用core text对文字进行排版
阅读量:7174 次
发布时间:2019-06-29

本文共 4571 字,大约阅读时间需要 15 分钟。

  hot3.png

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 ,3);

//给文本添加设置

[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

转载于:https://my.oschina.net/anyson/blog/102947

你可能感兴趣的文章
SQL Server 合并复制的Article可以指定单个对象的更新方向
查看>>
hreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
查看>>
npm常用命令
查看>>
String,StringBuffer和StringBuilder三者的讲解
查看>>
Understanding Digital Raw Capture
查看>>
(轉貼) 康乃爾筆記法(Cornell Method) (雜項)
查看>>
(原創) unnamed object的多型只能使用reference (C/C++)
查看>>
10种有用的CSS技巧
查看>>
linux命令tree用法详解
查看>>
matlab练习程序(TV模型图像修复)
查看>>
用户接口(UI)设计的 20 条原则
查看>>
Windows Azure HandBook (3) 浅谈Azure安全性
查看>>
div+css布局入门
查看>>
Linux 下Apache和Resin的安装
查看>>
HDU 2710 Max Factorv (素数模板 & 多种解法)
查看>>
Linux 启动流程
查看>>
获得汉字字符串的首个拼音字母的缩写
查看>>
RegularExpressionValidator控件与常用验证正则表达式大全小结
查看>>
Zookeeper集群的安装和使用
查看>>
remobject 簡介
查看>>