| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- //
- // TextDrawable.m
- // pdftest
- //
- // Created by Ray on 10/12/16.
- // Copyright © 2016 United Software Applications, Inc. All rights reserved.
- //
- #import "TextDrawable.h"
- #import <CoreText/CoreText.h>
- @implementation TextDrawable
- -(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
- {
-
- [super setDrawableTemplate:drawableTemplate];
-
-
- NSString* align= drawableTemplate[@"align"];
- if(align.length==0 || [align isEqualToString:@"left"])
- {
- self.alignment= kCTTextAlignmentLeft;
- }
- else if([align isEqualToString:@"right"])
- {
- self.alignment= kCTTextAlignmentRight;
- }
- else if([align isEqualToString:@"center"])
- {
- self.alignment= kCTTextAlignmentCenter;
- }
- else
- {
- self.alignment= kCTTextAlignmentLeft;
- }
-
- NSString* colorvalue=drawableTemplate[@"color"];
- if(colorvalue.length>0 )
- {
- unsigned long color = strtoul([colorvalue UTF8String],0,16);
- self.textColor= UIColorFromRGB(color);
- }
- else
- {
- self.textColor= [UIColor blackColor];
- }
-
-
- // kCTTextAlignmentJustified CT_ENUM_AVAILABLE(10_8, 6_0) = 3,
- // kCTTextAlignmentNatural CT_ENUM_AVAILABLE(10_8, 6_0) = 4,
-
- // _drawableTemplate = drawableTemplate;
- // self.margin_left=[self.drawableTemplate[@"margin_left"] doubleValue];
- // self.margin_right=[self.drawableTemplate[@"margin_right"] doubleValue];
- // self.margin_top=[self.drawableTemplate[@"margin_top"] doubleValue];
- // self.margin_bottom=[self.drawableTemplate[@"margin_bottom"] doubleValue];
- // self.rect = CGRectMake(0, 0, [self.drawableTemplate[@"width"] doubleValue], [self.drawableTemplate[@"height"] doubleValue]);
- }
- -(void) Draw3:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
- {
- CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
-
-
- CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
-
-
-
- NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.text] ;
-
- //创建字体以及字体大小
- CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
- CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
- //字体,把helvetica 样式加到整个,string上
- [string addAttribute:(id)kCTFontAttributeName
- value:CFBridgingRelease(helvetica)
- range:NSMakeRange(0, [string length])];
-
- //字体样式 ,把helveticaBold 样式加到整个,string上
- [string addAttribute:(id)kCTFontAttributeName
- value:CFBridgingRelease(helveticaBold)
- range:NSMakeRange(0, [string length])];
-
- //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
- [string addAttribute:(id)kCTForegroundColorAttributeName
- value:(id)self.textColor.CGColor
- range:NSMakeRange(0, [string length])];
- //设置字体间距
- long number = self.characterSpace;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
- [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
- CFRelease(num);
-
-
-
-
- //创建文本对齐方式
- CTTextAlignment alignment = self.alignment;//对齐方
- CTParagraphStyleSetting alignmentStyle;
- alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
- alignmentStyle.valueSize=sizeof(alignment);
- alignmentStyle.value=&alignment;
-
- //创建文本, 行间距
- CGFloat lineSpace=self.lineSpace;//间距数据
- CTParagraphStyleSetting lineSpaceStyle;
- lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
- lineSpaceStyle.valueSize=sizeof(lineSpace);
- lineSpaceStyle.value=&lineSpace;
-
-
- //设置 段落间距
- CGFloat paragraph = self.paragraphSpace;
- CTParagraphStyleSetting paragraphStyle;
- paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
- paragraphStyle.valueSize = sizeof(CGFloat);
- paragraphStyle.value = ¶graph;
-
-
-
-
- //创建样式数组
- CTParagraphStyleSetting settings[]={
- alignmentStyle,lineSpaceStyle,paragraphStyle
- };
-
- //设置样式
- CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
-
- //给字符串添加样式attribute
- // [string addAttribute:(id)kCTParagraphStyleAttributeName
- // value:(id)paragraphStyle1
- // range:NSMakeRange(0, [string length])];
-
-
- // [super flipContext:context ContextHeight:flip_height];
-
-
-
- // 切换上下文 Core Graphics 和 UIKit 混用
- UIGraphicsPushContext(context);
-
-
- // NSMutableDictionary* attribute = [[NSMutableDictionary alloc]init];
- //
- //
- // attribute[NSFontAttributeName]=[UIFont systemFontOfSize:fontsize];
- //
- //
- //
- // attribute[NSForegroundColorAttributeName]=self.textColor;
-
-
- //
- //
- // UIKIT_EXTERN NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSParagraphStyle, default defaultParagraphStyle
- // UIKIT_EXTERN NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: no background
- // UIKIT_EXTERN NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
- // UIKIT_EXTERN NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
- // UIKIT_EXTERN NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no strikethrough
- // UIKIT_EXTERN NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no underline
- // UIKIT_EXTERN NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: same as foreground color
- // UIKIT_EXTERN NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
- // UIKIT_EXTERN NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0); // NSShadow, default nil: no shadow
- // UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE(10_10, 7_0); // NSString, default nil: no text effect
- //
- // UIKIT_EXTERN NSString * const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0); // NSTextAttachment, default nil
- // UIKIT_EXTERN NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0); // NSURL (preferred) or NSString
- // UIKIT_EXTERN NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value, in points; offset from baseline, default 0
- // UIKIT_EXTERN NSString * const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
- // UIKIT_EXTERN NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
- // UIKIT_EXTERN NSString * const NSObliquenessAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
- // UIKIT_EXTERN NSString * const NSExpansionAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
- //
- // UIKIT_EXTERN NSString * const NSWritingDirectionAttributeName NS_AVAILABLE(10_6, 7_0); // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values. LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
- //
- // UIKIT_EXTERN NSString * const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0); // An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it's always horizontal. The behavior for any other value is undefined.
- //
- //
-
-
- // 使用UIKit绘制内容
- // [self.text drawInRect:pdfrect withAttributes:attribute];
-
- [string drawInRect:pdfrect];
-
- // 绘制完位图后,再恢复上下文
- UIGraphicsPopContext();
-
- // [super restoreFlip:context ContextHeight:flip_height];
-
- }
- -(CGRect) Query_Rect:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
- {
- CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
-
- return parentrect;
- }
- -(CGRect) Draw:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
- {
-
-
-
-
-
- NSString * source=self.drawableTemplate[@"source_type"];
- if(source==nil)
- source=@"const";
- if([source isEqualToString:@"const"])
- {
- self.text=self.drawableTemplate[@"text"];
-
- }
- else
- {
-
- // not impl;
-
- self.text=data[self.drawableTemplate[@"source"]];
- }
-
- // [super flipContext:context ContextHeight:flip_height];
- // super flipContext:context ContextHeight:<#(double)#>
- CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
-
-
- //创建AttributeStringfdsa
- NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
- initWithString:self.text];
- //创建字体以及字体大小
- CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
- CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
- //字体,把helvetica 样式加到整个,string上
- [string addAttribute:(id)kCTFontAttributeName
- value:CFBridgingRelease(helvetica)
- range:NSMakeRange(0, [string length])];
-
- //字体样式 ,把helveticaBold 样式加到整个,string上
- [string addAttribute:(id)kCTFontAttributeName
- value:CFBridgingRelease(helveticaBold)
- range:NSMakeRange(0, [string length])];
-
- //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
- [string addAttribute:(id)kCTForegroundColorAttributeName
- value:(id)self.textColor.CGColor
- range:NSMakeRange(0, [string length])];
-
-
-
- //设置字体间距
- long number = self.characterSpace;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
- [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
- CFRelease(num);
-
-
- //创建文本对齐方式
- CTTextAlignment alignment = self.alignment;//对齐方
- CTParagraphStyleSetting alignmentStyle;
- alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
- alignmentStyle.valueSize=sizeof(alignment);
- alignmentStyle.value=&alignment;
-
- //创建文本, 行间距
- CGFloat lineSpace=self.lineSpace;//间距数据
- CTParagraphStyleSetting lineSpaceStyle;
- lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
- lineSpaceStyle.valueSize=sizeof(lineSpace);
- lineSpaceStyle.value=&lineSpace;
-
-
- //设置 段落间距
- CGFloat paragraph = self.paragraphSpace;
- CTParagraphStyleSetting paragraphStyle;
- paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
- paragraphStyle.valueSize = sizeof(CGFloat);
- paragraphStyle.value = ¶graph;
-
-
-
-
- //创建样式数组
- CTParagraphStyleSetting settings[]={
- alignmentStyle,lineSpaceStyle,paragraphStyle
- };
-
- //设置样式
- CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
-
- //给字符串添加样式attribute
- [string addAttribute:(id)kCTParagraphStyleAttributeName
- value:(id)paragraphStyle1
- range:NSMakeRange(0, [string length])];
- // CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
- // [string drawInRect:pdfrect];
-
-
- // [self.text drawInRect:pdfrect withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:40],NSForegroundColorAttributeName : [UIColor redColor]}];
-
- // layout master
- CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
- //计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
- CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.rect.size.width, MAXFLOAT), NULL);
- //创建textBoxSize以设置view的frame
- CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
- // NSLog(@"textBoxSize0 == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);
- // self.frame = CGRectMake(0, 0, textBoxSize.width , textBoxSize.height);
- // [string release];
-
- //- (void)drawRect:(CGRect)rect;代码
-
-
-
-
- CGMutablePathRef leftColumnPath = CGPathCreateMutable();
-
- CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
-
- CGRect pdfrect = [self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];
- // pdfrect=CGRectMake(pdfrect.origin.x, flip_height-pdfrect.origin.y, pdfrect.size.width, pdfrect.size.height);
- CGPathAddRect(leftColumnPath, NULL,
- pdfrect);
-
- CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
- CFRangeMake(0, 0),
- leftColumnPath, NULL);
-
- // NSLog(@"textBoxSize1 == %f,%f",self.frame.size.width,self.frame.size.height);
- // flip the coordinate system
- // CGContextRef context = UIGraphicsGetCurrentContext();
-
-
- // CGContextClearRect(context, self.rect);
- // CGContextSetFillColorWithColor(context, [[UIColor whiteColor]CGColor]);
- // CGContextFillRect(context, CGRectMake(0, 0, self.rect.size.width, self.rect.size.height));
-
-
- CGContextSetTextMatrix(context, CGAffineTransformIdentity);
- // CGContextTranslateCTM(context, 0, self.rect.size.height);
- // CGContextScaleCTM(context, 1.0, -1.0);
-
- // draw
- CTFrameDraw(leftFrame, context);
-
- // cleanup
-
- CGPathRelease(leftColumnPath);
- CFRelease(framesetter);
-
- [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
- return parentrect;
- // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
-
- // [super restoreFlip:context ContextHeight:flip_height];
- }
- -(void) Draw1:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y
- {
-
- // self.attributedText.length
- //
- // if (context == NULL ||
- // attributedText == nil ||
- // attributedText.length == 0 ||
- // CGRectIsEmpty(rect) ||
- // textRange.length == 0) return;
-
-
- // NSAttributedString* attributedText = [[NSAttributedString alloc]initWithString:self.text];
-
- CGContextSaveGState(context);
- {
- // Prepare font
- CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
- NSString* font = self.drawableTemplate[@"font"] ;
-
- if(font==nil)
- {
- font=@"STHeitiSC-Medium";
- }
-
-
- CTFontRef ctfont = CTFontCreateWithName( (__bridge CFStringRef)font, fontsize, NULL);
-
- // text color
- CGColorRef ctColor = [[UIColor greenColor] CGColor];
-
-
- //text align
- CTTextAlignment alignment = kCTRightTextAlignment; //文本对齐方式
- CTParagraphStyleSetting alignmentStyle;
-
- alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定为对齐属性
-
- alignmentStyle.valueSize=sizeof(alignment);
-
- alignmentStyle.value=&alignment;
-
- CTParagraphStyleSetting settings[]={
-
- alignmentStyle
-
- };//设置样式
-
- CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings));
-
-
-
- CGContextSetShouldSmoothFonts(context, YES);
- CGContextSetShouldAntialias(context, YES);
-
-
- // Create an attributed string
- CFStringRef keys[] = { kCTFontAttributeName,kCTForegroundColorAttributeName,kCTParagraphStyleAttributeName };
- CFTypeRef values[] = { ctfont,ctColor,paragraphStyle};
- CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
- sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-
-
- CFStringRef ctStr = CFStringCreateWithCString(nil, [self.text UTF8String], kCFStringEncodingUTF8);
- CFAttributedStringRef attrString = CFAttributedStringCreate(NULL,ctStr, attr);
-
- // //给字符串添加样式attribute
- //
- // [resultAttributedStringaddAttribute:(id)kCTParagraphStyleAttributeName
- // value:(id)paragraphStyle
- //
- // range:NSMakeRange(0, [resultAttributedString length])];
-
-
-
- CTLineRef line = CTLineCreateWithAttributedString(attrString);
- CGContextSetTextMatrix(context, CGAffineTransformIdentity);
- //CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(3.14));
- //CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); //Use this one if the view's coordinates are flipped
- CGContextSetTextPosition(context, fontsize, fontsize);
- CTLineDraw(line, context);
-
-
-
- // CFRelease(line);
- // CFRelease(attrString);
- // CFRelease(ctStr);
- //
- // CFStringRef ctStr2 = CFStringCreateWithCString(nil, [sY UTF8String], kCFStringEncodingUTF8);
- // CFAttributedStringRef attrString2 = CFAttributedStringCreate(NULL,ctStr2, attr);
- // CTLineRef line2 = CTLineCreateWithAttributedString(attrString2);
- // CGContextSetTextMatrix(context, CGAffineTransformIdentity);
- // CGContextSetTextPosition(context, s, 2*s);
- // CTLineDraw(line2, context);
- //
- // CFRelease(line2);
- // CFRelease(attrString2);
- // CFRelease(ctStr2);
- //
- // CFStringRef ctStr3 = CFStringCreateWithCString(nil, [sZ UTF8String], kCFStringEncodingUTF8);
- // CFAttributedStringRef attrString3 = CFAttributedStringCreate(NULL,ctStr3, attr);
- // CTLineRef line3 = CTLineCreateWithAttributedString(attrString3);
- // CGContextSetTextMatrix(context, CGAffineTransformIdentity);
- // CGContextSetTextPosition(context, s, 3*s);
- // CTLineDraw(line3, context);
- //
- // CFRelease(line3);
- // CFRelease(attrString3);
- // CFRelease(ctStr3);
- //
- // // Clean up
- // CFRelease(attr);
- // CFRelease(ctfont);
- }
- /* Restore the current graphics state from the one on the top of the
- graphics state stack, popping the graphics state stack in the process. */
- CGContextRestoreGState(context);
- }
- -(CGSize)height1:(NSString *)text Font:(CGFloat)font Character:(CGFloat)character Line:(CGFloat)line Pragraph:(CGFloat)pragraph
- {
-
- //创建AttributeStringfdsa
- NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
- initWithString:text];
- //创建字体以及字体大小
- CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), font, NULL);
- CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), font, NULL);
- //添加字体目标字符串从下标0开始到字符串结尾
- [string addAttribute:(id)kCTFontAttributeName
- value:CFBridgingRelease(helvetica)
- range:NSMakeRange(0, [string length])];
-
- //添加字体目标字符串从下标0开始,截止到4个单位的长度
- [string addAttribute:(id)kCTFontAttributeName
- value:CFBridgingRelease(helveticaBold)
- range:NSMakeRange(0, [string length])];
-
-
- [string addAttribute:(id)kCTForegroundColorAttributeName
- value:(id)[UIColor whiteColor].CGColor
- range:NSMakeRange(0, [string length])];
-
- CTTextAlignment alignment = kCTJustifiedTextAlignment;//这种对齐方式会自动调整,使左右始终对齐
- CTParagraphStyleSetting alignmentStyle;
- alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定为对齐属性
- alignmentStyle.valueSize=sizeof(alignment);
- alignmentStyle.value=&alignment;
-
-
- //设置字体间距
- long number = character;
- CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
- [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
- CFRelease(num);
-
-
-
- //创建文本行间距
- CGFloat lineSpace=line;//间距数据
- CTParagraphStyleSetting lineSpaceStyle;
- lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;//指定为行间距属性
- lineSpaceStyle.valueSize=sizeof(lineSpace);
- lineSpaceStyle.value=&lineSpace;
-
- //设置段落间距
- CGFloat paragraph = pragraph;
- CTParagraphStyleSetting paragraphStyle;
- paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
- paragraphStyle.valueSize = sizeof(CGFloat);
- paragraphStyle.value = ¶graph;
-
-
- //创建样式数组
- CTParagraphStyleSetting settings[]={
- alignmentStyle,lineSpaceStyle,paragraphStyle
- };
-
- //设置样式
- CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
-
- //给字符串添加样式attribute
- [string addAttribute:(id)kCTParagraphStyleAttributeName
- value:(id)paragraphStyle1
- range:NSMakeRange(0, [string length])];
-
- // layout master
- CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
- //计算文本绘制size
- CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(300, MAXFLOAT), NULL);
- //创建textBoxSize以设置view的frame
- CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
-
-
- return textBoxSize;
-
- }
- @end
|