IOS利用Core Text對文字進行排版 - 轉

來源:互聯網
上載者:User

標籤:

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

IOS利用Core Text對文字進行排版 - 轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.