好程序员学习笔记IOS 利用core text对文字进行排版_ios程序员笔试题
好程序员学习笔记IOS 利用core text对文字进行排版由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“ios程序员笔试题”。
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 “rn” withString “n”];具体的代码实现如下: importimport importimport @interface TextLayoutLabel : UILabel { @private [size=1em] [color=white!important][size=1em]? long linesSpacing_;//行间距 } @property(nonatomic,aign)CGFloat characterSpacing;@propery(nonatomic,aign)long linesSpacing;@end import “TextLayoutLabel.h”import @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 “rn” 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 = ¶graphSpacing;//创建设置数组
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);}