TextDrawable.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. //
  2. // TextDrawable.m
  3. // pdftest
  4. //
  5. // Created by Ray on 10/12/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "TextDrawable.h"
  9. #import <CoreText/CoreText.h>
  10. @implementation TextDrawable
  11. -(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
  12. {
  13. [super setDrawableTemplate:drawableTemplate];
  14. NSString* align= drawableTemplate[@"align"];
  15. if(align.length==0 || [align isEqualToString:@"left"])
  16. {
  17. self.alignment= kCTTextAlignmentLeft;
  18. }
  19. else if([align isEqualToString:@"right"])
  20. {
  21. self.alignment= kCTTextAlignmentRight;
  22. }
  23. else if([align isEqualToString:@"center"])
  24. {
  25. self.alignment= kCTTextAlignmentCenter;
  26. }
  27. else
  28. {
  29. self.alignment= kCTTextAlignmentLeft;
  30. }
  31. NSString* colorvalue=drawableTemplate[@"color"];
  32. if(colorvalue.length>0 )
  33. {
  34. unsigned long color = strtoul([colorvalue UTF8String],0,16);
  35. self.textColor= UIColorFromRGB(color);
  36. }
  37. else
  38. {
  39. self.textColor= [UIColor blackColor];
  40. }
  41. // kCTTextAlignmentJustified CT_ENUM_AVAILABLE(10_8, 6_0) = 3,
  42. // kCTTextAlignmentNatural CT_ENUM_AVAILABLE(10_8, 6_0) = 4,
  43. // _drawableTemplate = drawableTemplate;
  44. // self.margin_left=[self.drawableTemplate[@"margin_left"] doubleValue];
  45. // self.margin_right=[self.drawableTemplate[@"margin_right"] doubleValue];
  46. // self.margin_top=[self.drawableTemplate[@"margin_top"] doubleValue];
  47. // self.margin_bottom=[self.drawableTemplate[@"margin_bottom"] doubleValue];
  48. // self.rect = CGRectMake(0, 0, [self.drawableTemplate[@"width"] doubleValue], [self.drawableTemplate[@"height"] doubleValue]);
  49. }
  50. -(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
  51. {
  52. CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  53. CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
  54. NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.text] ;
  55. //创建字体以及字体大小
  56. CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  57. CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  58. //字体,把helvetica 样式加到整个,string上
  59. [string addAttribute:(id)kCTFontAttributeName
  60. value:CFBridgingRelease(helvetica)
  61. range:NSMakeRange(0, [string length])];
  62. //字体样式 ,把helveticaBold 样式加到整个,string上
  63. [string addAttribute:(id)kCTFontAttributeName
  64. value:CFBridgingRelease(helveticaBold)
  65. range:NSMakeRange(0, [string length])];
  66. //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  67. [string addAttribute:(id)kCTForegroundColorAttributeName
  68. value:(id)self.textColor.CGColor
  69. range:NSMakeRange(0, [string length])];
  70. //设置字体间距
  71. long number = self.characterSpace;
  72. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
  73. [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
  74. CFRelease(num);
  75. //创建文本对齐方式
  76. CTTextAlignment alignment = self.alignment;//对齐方
  77. CTParagraphStyleSetting alignmentStyle;
  78. alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
  79. alignmentStyle.valueSize=sizeof(alignment);
  80. alignmentStyle.value=&alignment;
  81. //创建文本, 行间距
  82. CGFloat lineSpace=self.lineSpace;//间距数据
  83. CTParagraphStyleSetting lineSpaceStyle;
  84. lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
  85. lineSpaceStyle.valueSize=sizeof(lineSpace);
  86. lineSpaceStyle.value=&lineSpace;
  87. //设置 段落间距
  88. CGFloat paragraph = self.paragraphSpace;
  89. CTParagraphStyleSetting paragraphStyle;
  90. paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
  91. paragraphStyle.valueSize = sizeof(CGFloat);
  92. paragraphStyle.value = &paragraph;
  93. //创建样式数组
  94. CTParagraphStyleSetting settings[]={
  95. alignmentStyle,lineSpaceStyle,paragraphStyle
  96. };
  97. //设置样式
  98. CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
  99. //给字符串添加样式attribute
  100. // [string addAttribute:(id)kCTParagraphStyleAttributeName
  101. // value:(id)paragraphStyle1
  102. // range:NSMakeRange(0, [string length])];
  103. // [super flipContext:context ContextHeight:flip_height];
  104. // 切换上下文 Core Graphics 和 UIKit 混用
  105. UIGraphicsPushContext(context);
  106. // NSMutableDictionary* attribute = [[NSMutableDictionary alloc]init];
  107. //
  108. //
  109. // attribute[NSFontAttributeName]=[UIFont systemFontOfSize:fontsize];
  110. //
  111. //
  112. //
  113. // attribute[NSForegroundColorAttributeName]=self.textColor;
  114. //
  115. //
  116. // UIKIT_EXTERN NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSParagraphStyle, default defaultParagraphStyle
  117. // UIKIT_EXTERN NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: no background
  118. // UIKIT_EXTERN NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
  119. // 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.
  120. // UIKIT_EXTERN NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no strikethrough
  121. // UIKIT_EXTERN NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no underline
  122. // UIKIT_EXTERN NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: same as foreground color
  123. // 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)
  124. // UIKIT_EXTERN NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0); // NSShadow, default nil: no shadow
  125. // UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE(10_10, 7_0); // NSString, default nil: no text effect
  126. //
  127. // UIKIT_EXTERN NSString * const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0); // NSTextAttachment, default nil
  128. // UIKIT_EXTERN NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0); // NSURL (preferred) or NSString
  129. // UIKIT_EXTERN NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value, in points; offset from baseline, default 0
  130. // UIKIT_EXTERN NSString * const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
  131. // UIKIT_EXTERN NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
  132. // 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
  133. // 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
  134. //
  135. // 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,
  136. //
  137. // 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.
  138. //
  139. //
  140. // 使用UIKit绘制内容
  141. // [self.text drawInRect:pdfrect withAttributes:attribute];
  142. [string drawInRect:pdfrect];
  143. // 绘制完位图后,再恢复上下文
  144. UIGraphicsPopContext();
  145. // [super restoreFlip:context ContextHeight:flip_height];
  146. }
  147. -(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
  148. {
  149. CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  150. return parentrect;
  151. }
  152. -(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
  153. {
  154. NSString * source=self.drawableTemplate[@"source_type"];
  155. if(source==nil)
  156. source=@"const";
  157. if([source isEqualToString:@"const"])
  158. {
  159. self.text=self.drawableTemplate[@"text"];
  160. }
  161. else
  162. {
  163. // not impl;
  164. self.text=data[self.drawableTemplate[@"source"]];
  165. }
  166. // [super flipContext:context ContextHeight:flip_height];
  167. // super flipContext:context ContextHeight:<#(double)#>
  168. CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
  169. //创建AttributeStringfdsa
  170. NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
  171. initWithString:self.text];
  172. //创建字体以及字体大小
  173. CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  174. CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  175. //字体,把helvetica 样式加到整个,string上
  176. [string addAttribute:(id)kCTFontAttributeName
  177. value:CFBridgingRelease(helvetica)
  178. range:NSMakeRange(0, [string length])];
  179. //字体样式 ,把helveticaBold 样式加到整个,string上
  180. [string addAttribute:(id)kCTFontAttributeName
  181. value:CFBridgingRelease(helveticaBold)
  182. range:NSMakeRange(0, [string length])];
  183. //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  184. [string addAttribute:(id)kCTForegroundColorAttributeName
  185. value:(id)self.textColor.CGColor
  186. range:NSMakeRange(0, [string length])];
  187. //设置字体间距
  188. long number = self.characterSpace;
  189. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
  190. [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
  191. CFRelease(num);
  192. //创建文本对齐方式
  193. CTTextAlignment alignment = self.alignment;//对齐方
  194. CTParagraphStyleSetting alignmentStyle;
  195. alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
  196. alignmentStyle.valueSize=sizeof(alignment);
  197. alignmentStyle.value=&alignment;
  198. //创建文本, 行间距
  199. CGFloat lineSpace=self.lineSpace;//间距数据
  200. CTParagraphStyleSetting lineSpaceStyle;
  201. lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
  202. lineSpaceStyle.valueSize=sizeof(lineSpace);
  203. lineSpaceStyle.value=&lineSpace;
  204. //设置 段落间距
  205. CGFloat paragraph = self.paragraphSpace;
  206. CTParagraphStyleSetting paragraphStyle;
  207. paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
  208. paragraphStyle.valueSize = sizeof(CGFloat);
  209. paragraphStyle.value = &paragraph;
  210. //创建样式数组
  211. CTParagraphStyleSetting settings[]={
  212. alignmentStyle,lineSpaceStyle,paragraphStyle
  213. };
  214. //设置样式
  215. CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
  216. //给字符串添加样式attribute
  217. [string addAttribute:(id)kCTParagraphStyleAttributeName
  218. value:(id)paragraphStyle1
  219. range:NSMakeRange(0, [string length])];
  220. // CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  221. // [string drawInRect:pdfrect];
  222. // [self.text drawInRect:pdfrect withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:40],NSForegroundColorAttributeName : [UIColor redColor]}];
  223. // layout master
  224. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
  225. //计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
  226. CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.rect.size.width, MAXFLOAT), NULL);
  227. //创建textBoxSize以设置view的frame
  228. CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
  229. // NSLog(@"textBoxSize0 == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);
  230. // self.frame = CGRectMake(0, 0, textBoxSize.width , textBoxSize.height);
  231. // [string release];
  232. //- (void)drawRect:(CGRect)rect;代码
  233. CGMutablePathRef leftColumnPath = CGPathCreateMutable();
  234. CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  235. CGRect pdfrect = [self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];
  236. // pdfrect=CGRectMake(pdfrect.origin.x, flip_height-pdfrect.origin.y, pdfrect.size.width, pdfrect.size.height);
  237. CGPathAddRect(leftColumnPath, NULL,
  238. pdfrect);
  239. CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
  240. CFRangeMake(0, 0),
  241. leftColumnPath, NULL);
  242. // NSLog(@"textBoxSize1 == %f,%f",self.frame.size.width,self.frame.size.height);
  243. // flip the coordinate system
  244. // CGContextRef context = UIGraphicsGetCurrentContext();
  245. // CGContextClearRect(context, self.rect);
  246. // CGContextSetFillColorWithColor(context, [[UIColor whiteColor]CGColor]);
  247. // CGContextFillRect(context, CGRectMake(0, 0, self.rect.size.width, self.rect.size.height));
  248. CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  249. // CGContextTranslateCTM(context, 0, self.rect.size.height);
  250. // CGContextScaleCTM(context, 1.0, -1.0);
  251. // draw
  252. CTFrameDraw(leftFrame, context);
  253. // cleanup
  254. CGPathRelease(leftColumnPath);
  255. CFRelease(framesetter);
  256. [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  257. return parentrect;
  258. // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  259. // [super restoreFlip:context ContextHeight:flip_height];
  260. }
  261. -(void) Draw1:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y
  262. {
  263. // self.attributedText.length
  264. //
  265. // if (context == NULL ||
  266. // attributedText == nil ||
  267. // attributedText.length == 0 ||
  268. // CGRectIsEmpty(rect) ||
  269. // textRange.length == 0) return;
  270. // NSAttributedString* attributedText = [[NSAttributedString alloc]initWithString:self.text];
  271. CGContextSaveGState(context);
  272. {
  273. // Prepare font
  274. CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
  275. NSString* font = self.drawableTemplate[@"font"] ;
  276. if(font==nil)
  277. {
  278. font=@"STHeitiSC-Medium";
  279. }
  280. CTFontRef ctfont = CTFontCreateWithName( (__bridge CFStringRef)font, fontsize, NULL);
  281. // text color
  282. CGColorRef ctColor = [[UIColor greenColor] CGColor];
  283. //text align
  284. CTTextAlignment alignment = kCTRightTextAlignment; //文本对齐方式
  285. CTParagraphStyleSetting alignmentStyle;
  286. alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定为对齐属性
  287. alignmentStyle.valueSize=sizeof(alignment);
  288. alignmentStyle.value=&alignment;
  289. CTParagraphStyleSetting settings[]={
  290. alignmentStyle
  291. };//设置样式
  292. CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings));
  293. CGContextSetShouldSmoothFonts(context, YES);
  294. CGContextSetShouldAntialias(context, YES);
  295. // Create an attributed string
  296. CFStringRef keys[] = { kCTFontAttributeName,kCTForegroundColorAttributeName,kCTParagraphStyleAttributeName };
  297. CFTypeRef values[] = { ctfont,ctColor,paragraphStyle};
  298. CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
  299. sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  300. CFStringRef ctStr = CFStringCreateWithCString(nil, [self.text UTF8String], kCFStringEncodingUTF8);
  301. CFAttributedStringRef attrString = CFAttributedStringCreate(NULL,ctStr, attr);
  302. // //给字符串添加样式attribute
  303. //
  304. // [resultAttributedStringaddAttribute:(id)kCTParagraphStyleAttributeName
  305. // value:(id)paragraphStyle
  306. //
  307. // range:NSMakeRange(0, [resultAttributedString length])];
  308. CTLineRef line = CTLineCreateWithAttributedString(attrString);
  309. CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  310. //CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(3.14));
  311. //CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); //Use this one if the view's coordinates are flipped
  312. CGContextSetTextPosition(context, fontsize, fontsize);
  313. CTLineDraw(line, context);
  314. // CFRelease(line);
  315. // CFRelease(attrString);
  316. // CFRelease(ctStr);
  317. //
  318. // CFStringRef ctStr2 = CFStringCreateWithCString(nil, [sY UTF8String], kCFStringEncodingUTF8);
  319. // CFAttributedStringRef attrString2 = CFAttributedStringCreate(NULL,ctStr2, attr);
  320. // CTLineRef line2 = CTLineCreateWithAttributedString(attrString2);
  321. // CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  322. // CGContextSetTextPosition(context, s, 2*s);
  323. // CTLineDraw(line2, context);
  324. //
  325. // CFRelease(line2);
  326. // CFRelease(attrString2);
  327. // CFRelease(ctStr2);
  328. //
  329. // CFStringRef ctStr3 = CFStringCreateWithCString(nil, [sZ UTF8String], kCFStringEncodingUTF8);
  330. // CFAttributedStringRef attrString3 = CFAttributedStringCreate(NULL,ctStr3, attr);
  331. // CTLineRef line3 = CTLineCreateWithAttributedString(attrString3);
  332. // CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  333. // CGContextSetTextPosition(context, s, 3*s);
  334. // CTLineDraw(line3, context);
  335. //
  336. // CFRelease(line3);
  337. // CFRelease(attrString3);
  338. // CFRelease(ctStr3);
  339. //
  340. // // Clean up
  341. // CFRelease(attr);
  342. // CFRelease(ctfont);
  343. }
  344. /* Restore the current graphics state from the one on the top of the
  345. graphics state stack, popping the graphics state stack in the process. */
  346. CGContextRestoreGState(context);
  347. }
  348. -(CGSize)height1:(NSString *)text Font:(CGFloat)font Character:(CGFloat)character Line:(CGFloat)line Pragraph:(CGFloat)pragraph
  349. {
  350. //创建AttributeStringfdsa
  351. NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
  352. initWithString:text];
  353. //创建字体以及字体大小
  354. CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), font, NULL);
  355. CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), font, NULL);
  356. //添加字体目标字符串从下标0开始到字符串结尾
  357. [string addAttribute:(id)kCTFontAttributeName
  358. value:CFBridgingRelease(helvetica)
  359. range:NSMakeRange(0, [string length])];
  360. //添加字体目标字符串从下标0开始,截止到4个单位的长度
  361. [string addAttribute:(id)kCTFontAttributeName
  362. value:CFBridgingRelease(helveticaBold)
  363. range:NSMakeRange(0, [string length])];
  364. [string addAttribute:(id)kCTForegroundColorAttributeName
  365. value:(id)[UIColor whiteColor].CGColor
  366. range:NSMakeRange(0, [string length])];
  367. CTTextAlignment alignment = kCTJustifiedTextAlignment;//这种对齐方式会自动调整,使左右始终对齐
  368. CTParagraphStyleSetting alignmentStyle;
  369. alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定为对齐属性
  370. alignmentStyle.valueSize=sizeof(alignment);
  371. alignmentStyle.value=&alignment;
  372. //设置字体间距
  373. long number = character;
  374. CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
  375. [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
  376. CFRelease(num);
  377. //创建文本行间距
  378. CGFloat lineSpace=line;//间距数据
  379. CTParagraphStyleSetting lineSpaceStyle;
  380. lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;//指定为行间距属性
  381. lineSpaceStyle.valueSize=sizeof(lineSpace);
  382. lineSpaceStyle.value=&lineSpace;
  383. //设置段落间距
  384. CGFloat paragraph = pragraph;
  385. CTParagraphStyleSetting paragraphStyle;
  386. paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
  387. paragraphStyle.valueSize = sizeof(CGFloat);
  388. paragraphStyle.value = &paragraph;
  389. //创建样式数组
  390. CTParagraphStyleSetting settings[]={
  391. alignmentStyle,lineSpaceStyle,paragraphStyle
  392. };
  393. //设置样式
  394. CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
  395. //给字符串添加样式attribute
  396. [string addAttribute:(id)kCTParagraphStyleAttributeName
  397. value:(id)paragraphStyle1
  398. range:NSMakeRange(0, [string length])];
  399. // layout master
  400. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
  401. //计算文本绘制size
  402. CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(300, MAXFLOAT), NULL);
  403. //创建textBoxSize以设置view的frame
  404. CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
  405. return textBoxSize;
  406. }
  407. @end