IOS利用Core Text对文字进行排版 - 转

原贴地址: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

posted @ 2015-10-08 12:49  MokeyChan  阅读(494)  评论(0编辑  收藏  举报