IOS 利用core text對文字進行排版

core text 這個包默認是沒有的,要本身手動添加進來。數組

在IOS中利用core text對文本進行排版的幾個關鍵點以下:字體

字間距:kCTKernAttributeName

 行間距:kCTParagraphStyleSpecifierLineSpacingAdjustment 或 kCTParagraphStyleSpecifierLineSpacing(不推薦使用)

段間距:kCTParagraphStyleSpecifierParagraphSpacing

文本對齊方式:kCTParagraphStyleSpecifierAlignment;atom

還有一點就是core text顯示出來的字是顛倒的,使用時要翻轉下:code

CGContextRef context = UIGraphicsGetCurrentContext();orm

CGContextSetTextMatrix(context,CGAffineTransformIdentity);ci

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

CGContextScaleCTM(context,1.0,-1.0);it

最後一點要注意的是Mac下的回車和Windows的是不同的,Windows下的回車是由\r \n組成的而Mac下只有一個\n,因此若是沒有去掉的話在每一段的最後都會多出一個空行來,去掉的方法以下:io

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

具體的代碼實現以下:

#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

相關文章
相關標籤/搜索