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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM