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

2021-06-05 10:27:36 字數 3810 閱讀 4195

2011-12-08 11:10

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

#import

@inte***ce textlayoutlabel : uilabel

@property(nonatomic,assign) cgfloat characterspacing;

@propery(nonatomic,assign)long linesspacing;

@end

#import "textlayoutlabel.h"

#import

@implementation textlayoutlabel

@synthesize characterspacing = characterspacing_;

@synthesize linesspacing = linesspacing_;

-(id) initwithframe:(cgrect)frame

return self;

}-(void)setcharacterspacing:(cgfloat)characterspacing //外部呼叫設定字間距

-(void)setlinesspacing:(long)linesspacing  //外部呼叫設定行間距

-(void) drawtextinrect:(cgrect)requestedrect

//設定字型顏色

[string addattribute:(id)kctforegroundcolorattributename value:(id)(self.textcolor.cgcolor) range:nsmakerange(0,[string length])];

//建立文字對齊方式

cttextalignment alignment = kctlefttextalignment;

if(self.textalignment == uitextalignmentcenter)

if(self.textalignment == uitextalignmentright)

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 = ¶graphspacing;

//建立設定陣列

ctparagraphstylesetting settings[ ] =;

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

利用FILE結構對檔案進行操作

利用file結構對檔案進行操作 輸入若干個學生的資料 包括學號 姓名和成績 將資料存放在student.txt檔案中 從student.txt檔案中讀取所有資料並顯示出來 include include include struct student void main file write,read...

Linux下利用openssl對檔案進行加密和解密

建立檔案test.txt,特意寫入中英文 cd tmp echo test測試 test.txt 開始加密,使用aes 128 cbc演算法,也可以使用其他演算法,通過檢視openssl的幫助可獲知 openssl aes 128 cbc salt in test.txt out test.txt....

iOS 利用pod trunk發布程式

註冊如 pod trunk register qq.com hans description hans 隨後出現下面提示,到郵件驗證 the verification email that has been sent to qq.com 檢視個人資訊 name hans email qq.com s...