TextDrawable.m 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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 "const.h"
  10. #import <CoreText/CoreText.h>
  11. #import "TextUtils.h"
  12. #define DEBUG_STRING_VALUE0 @"DEBUG OFF"
  13. #define DEBUG_STRING_VALUE1 @"DEBUG OFF"
  14. @implementation TextDrawable
  15. -(instancetype) init:(CGRect) rect source_type:(NSString*) source_type source:(NSString*)source textAlignment:(NSString*)textAlignment vAlign:(NSString*)vAlign size:(NSString*)size
  16. {
  17. if (self = [super init]) {
  18. // NSMutableDictionary* m1=[drawableTemplate mutableCopy];
  19. // NSMutableDictionary* m2=[drawableTemplate mutableCopy];
  20. // [self setDrawableTemplate:[[RAUtils string2dict:[RAUtils dict2string:drawableTemplate]] mutableCopy] ];
  21. NSMutableDictionary * drawableTemplate = [TextDrawable createTextTemplate:rect source_type:source_type source:source textAlignment:textAlignment vAlign:vAlign size:size];
  22. [self setDrawableTemplate:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:drawableTemplate]] ];
  23. }
  24. return self;
  25. }
  26. +(NSMutableDictionary *) createTextTemplate:(CGRect) rect source_type:(NSString*) source_type source:(NSString*)source textAlignment:(NSString*)textAlignment vAlign:(NSString*)vAlign size:(NSString*)size
  27. {
  28. DebugLog(@"createTextTemplate text align : %@",textAlignment);
  29. if(textAlignment.length==0)
  30. textAlignment= @"Center";
  31. if(vAlign.length==0)
  32. vAlign= @"Middle";
  33. if(size.length==0)
  34. vAlign= @"10";
  35. NSMutableDictionary* ret=[super createDrawableTemplate:rect];
  36. ret[@"type"]=@"text" ;
  37. ret[@"source_type"]=source_type ;
  38. ret[@"source"]=source ;
  39. ret[@"text"]=source ;
  40. ret[@"textAlignment"]=textAlignment ;
  41. ret[@"vAlign"]=vAlign ;
  42. ret[@"size"]=size ;
  43. return ret;
  44. }
  45. -(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
  46. {
  47. [super setDrawableTemplate:drawableTemplate];
  48. NSString* align= drawableTemplate[@"textAlignment"];
  49. DebugLog(@"setDrawableTemplate text align : %@",align);
  50. if(align.length==0 || [align.lowercaseString isEqualToString:@"left"])
  51. {
  52. self.textalignment= NSTextAlignmentLeft;
  53. }
  54. else if([align.lowercaseString isEqualToString:@"right"])
  55. {
  56. self.textalignment= NSTextAlignmentRight;
  57. }
  58. else if([align.lowercaseString isEqualToString:@"center"])
  59. {
  60. self.textalignment= NSTextAlignmentCenter;
  61. }
  62. else
  63. {
  64. self.textalignment= NSTextAlignmentLeft;
  65. }
  66. self.lineSpace = 0;
  67. self.paragraphSpace = 0;
  68. NSString* colorvalue=drawableTemplate[@"color"];
  69. if(colorvalue.length>0 )
  70. {
  71. unsigned long color = strtoul([colorvalue UTF8String],0,16);
  72. self.textColor= UIColorFromRGB(color);
  73. }
  74. else
  75. {
  76. self.textColor= [UIColor blackColor];
  77. }
  78. // kCTTextAlignmentJustified CT_ENUM_AVAILABLE(10_8, 6_0) = 3,
  79. // kCTTextAlignmentNatural CT_ENUM_AVAILABLE(10_8, 6_0) = 4,
  80. // _drawableTemplate = drawableTemplate;
  81. // self.margin_left=[self.drawableTemplate[@"margin_left"] doubleValue];
  82. // self.margin_right=[self.drawableTemplate[@"margin_right"] doubleValue];
  83. // self.margin_top=[self.drawableTemplate[@"margin_top"] doubleValue];
  84. // self.margin_bottom=[self.drawableTemplate[@"margin_bottom"] doubleValue];
  85. // self.rect = CGRectMake(0, 0, [self.drawableTemplate[@"width"] doubleValue], [self.drawableTemplate[@"height"] doubleValue]);
  86. }
  87. -(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
  88. {
  89. // return p_rect;
  90. bool expand=[self.drawableTemplate[@"expand"] boolValue];
  91. if(!expand)
  92. {
  93. CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  94. return parentrect;
  95. }
  96. else
  97. {
  98. NSString * source=self.drawableTemplate[@"source_type"];
  99. if(source==nil)
  100. source=@"const";
  101. if([source isEqualToString:@"const"])
  102. {
  103. self.text=self.drawableTemplate[@"text"];
  104. // self.text=[self.text stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br>"];
  105. }
  106. else if([source isEqualToString:@"data"])
  107. {
  108. if([self.drawableTemplate[@"source_isexpression"] boolValue])
  109. {
  110. NSString *content = data[self.drawableTemplate[@"source"]];
  111. NSString *pattern = @"\\$\\.\\{([\\s\\S]*?)\\}\\.\\$";
  112. NSArray* matches=[TextUtils expression_varable:content regex:pattern];
  113. // if (matches) {
  114. // for (NSTextCheckingResult *match in matches) {
  115. // for (int i = 0; i < match.numberOfRanges; ++i) {
  116. // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  117. // }
  118. // }
  119. // }
  120. if (matches)
  121. {
  122. self.text=content;
  123. for (NSTextCheckingResult *match in matches)
  124. {
  125. for (int i = 0; i < match.numberOfRanges/2; ++i) {
  126. DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  127. DebugLog(@"%d-> %@",i+1, [content substringWithRange:[match rangeAtIndex:i+1]]);
  128. self.text=[self.text stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[NSString stringWithFormat:@"%@",data[[content substringWithRange:[match rangeAtIndex:i+1]]]]];
  129. }
  130. }
  131. }
  132. //self.text=content;
  133. }
  134. else
  135. {
  136. self.text=data[self.drawableTemplate[@"source"]];
  137. }
  138. }
  139. if(self.text==nil)
  140. self.text=@"";
  141. // if([self.text isEqualToString:@"www.newpacificdirect.com"])
  142. // {
  143. // int debug=0;
  144. // }
  145. bool bold = [self.drawableTemplate[@"bold"] boolValue];
  146. // bool italic = [self.drawableTemplate[@"italic"] boolValue];
  147. NSString* font=self.drawableTemplate[@"font"];
  148. // [super flipContext:context ContextHeight:flip_height];
  149. // super flipContext:context ContextHeight:<#(double)#>
  150. CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
  151. //NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[title dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
  152. //创建AttributeStringfdsa
  153. NSMutableAttributedString *string = nil;
  154. // if([self.drawableTemplate[@"ishtml"] boolValue])
  155. // {
  156. // string=[[[NSAttributedString alloc] initWithData:[self.text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil] mutableCopy];
  157. //
  158. // }
  159. // else
  160. {
  161. string=[[NSMutableAttributedString alloc] initWithString:self.text];
  162. //创建字体以及字体大小
  163. // CTFontRef ctfont = [self CreateFont:font size:fontsize bold:bold];//CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  164. // // CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  165. // //字体,把helvetica 样式加到整个,string上
  166. // [string addAttribute:(id)kCTFontAttributeName
  167. // value:CFBridgingRelease(ctfont)
  168. // range:NSMakeRange(0, [string length])];
  169. //
  170. // //字体样式 ,把helveticaBold 样式加到整个,string上
  171. // // [string addAttribute:(id)kCTFontAttributeName
  172. // // value:CFBridgingRelease(helveticaBold)
  173. // // range:NSMakeRange(0, [string length])];
  174. //
  175. // //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  176. // [string addAttribute:(id)kCTForegroundColorAttributeName
  177. // value:(id)self.textColor.CGColor
  178. // range:NSMakeRange(0, [string length])];
  179. string = [self richtext2attributedstring:string font:font size:fontsize bold:bold];
  180. // //设置字体间距
  181. // long number = self.characterSpace;
  182. // CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
  183. // [string addAttribute:(id)kCTKernAttributeName value:(__bridge NSNumber*)num range:NSMakeRange(0, [string length])];
  184. // CFRelease(num);
  185. // //创建文本对齐方式
  186. // CTTextAlignment alignment = self.textalignment;//对齐方
  187. // CTParagraphStyleSetting alignmentStyle;
  188. // alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
  189. // alignmentStyle.valueSize=sizeof(alignment);
  190. // alignmentStyle.value=&alignment;
  191. // //创建文本, 行间距
  192. // CGFloat lineSpace=self.lineSpace;//间距数据
  193. // CTParagraphStyleSetting lineSpaceStyle;
  194. // lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
  195. // lineSpaceStyle.valueSize=sizeof(lineSpace);
  196. // lineSpaceStyle.value=&lineSpace;
  197. // //设置 line break;
  198. // CTLineBreakMode linebreakmode = kCTLineBreakByTruncatingTail;
  199. // CTParagraphStyleSetting linebreakStyle;
  200. // linebreakStyle.spec = kCTParagraphStyleSpecifierLineBreakMode;
  201. // linebreakStyle.valueSize = sizeof(CGFloat);
  202. // linebreakStyle.value = &linebreakmode;
  203. // //设置 段落间距
  204. // CGFloat paragraph = self.paragraphSpace;
  205. // CTParagraphStyleSetting paragraphStyle;
  206. // paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
  207. // paragraphStyle.valueSize = sizeof(CGFloat);
  208. // paragraphStyle.value = &paragraph;
  209. //创建样式数组
  210. // CTParagraphStyleSetting settings[]={
  211. // alignmentStyle/*,linebreakStyle,lineSpaceStyle,paragraphStyle*/
  212. // };
  213. //
  214. // //设置样式
  215. // CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
  216. //
  217. // //给字符串添加样式attribute
  218. // [string addAttribute:(id)kCTParagraphStyleAttributeName
  219. // value:(__bridge id)paragraphStyle1
  220. // range:NSMakeRange(0, [string length])];
  221. // CFRelease(paragraphStyle1);
  222. NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  223. [style setAlignment:self.textalignment];
  224. //[style setLineBreakMode:NSLineBreakByTruncatingTail];
  225. [style setLineBreakMode:NSLineBreakByWordWrapping];
  226. [string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [string length])];
  227. }
  228. // CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  229. // [string drawInRect:pdfrect];
  230. // [self.text drawInRect:pdfrect withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:40],NSForegroundColorAttributeName : [UIColor redColor]}];
  231. // layout master
  232. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
  233. //计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
  234. CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.rect.size.width-self.margin_left-self.margin_right, MAXFLOAT), NULL);
  235. //创建textBoxSize以设置view的frame
  236. // CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
  237. // DebugLog(@"textBoxSize0 == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);
  238. // self.frame = CGRectMake(0, 0, textBoxSize.width , textBoxSize.height);
  239. // [string release];
  240. //- (void)drawRect:(CGRect)rect;代码
  241. // CGMutablePathRef leftColumnPath = CGPathCreateMutable();
  242. CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  243. double height =0;
  244. if(tmpSize.height<parentrect.size.height)
  245. height=tmpSize.height;
  246. else
  247. if(expand)
  248. {
  249. height=tmpSize.height;
  250. self.rect = CGRectMake(self.rect.origin.x, self.rect.origin.y, self.rect.size.width, height+self.margin_top+self.margin_bottom);
  251. }
  252. else
  253. height=parentrect.size.height;
  254. CGRect rect=CGRectMake(parentrect.origin.x, parentrect.origin.y, parentrect.size.width, height);
  255. CGRect expand_rect=rect;
  256. // CGRect rect=[RAUtils rectAlign:parentrect rect:scalerect hAlign:self.hAlign vAlign:self.vAlign];
  257. // rect=[RAUtils rectVAlign:parentrect rect:rect vAlign:self.vAlign];
  258. //
  259. // // CGRect pdfrect = [self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];
  260. //
  261. // rect= [self parent_to_pdf_rect:rect contextHeight:flip_height];
  262. // // pdfrect=CGRectMake(pdfrect.origin.x, flip_height-pdfrect.origin.y, pdfrect.size.width, pdfrect.size.height);
  263. // CGPathAddRect(leftColumnPath, NULL,
  264. // rect);
  265. //
  266. // CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
  267. // CFRangeMake(0, 0),
  268. // leftColumnPath, NULL);
  269. //
  270. // // DebugLog(@"textBoxSize1 == %f,%f",self.frame.size.width,self.frame.size.height);
  271. // // flip the coordinate system
  272. // // CGContextRef context = UIGraphicsGetCurrentContext();
  273. //
  274. //
  275. // // CGContextClearRect(context, self.rect);
  276. // // CGContextSetFillColorWithColor(context, [[UIColor whiteColor]CGColor]);
  277. // // CGContextFillRect(context, CGRectMake(0, 0, self.rect.size.width, self.rect.size.height));
  278. //
  279. //
  280. // CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  281. // // CGContextTranslateCTM(context, 0, self.rect.size.height);
  282. // // CGContextScaleCTM(context, 1.0, -1.0);
  283. //
  284. // // draw
  285. // CTFrameDraw(leftFrame, context);
  286. //
  287. //
  288. // CFRelease(leftFrame);
  289. // // cleanup
  290. //
  291. // CGPathRelease(leftColumnPath);
  292. CFRelease(framesetter);
  293. // return self.rect;
  294. // [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  295. if(expand&&expand_rect.size.height>parentrect.size.height)
  296. return expand_rect;
  297. else
  298. return parentrect;
  299. // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  300. // [super restoreFlip:context ContextHeight:flip_height];
  301. }
  302. }
  303. -(UIFont*)CreateFont:(NSString*)name size:(double)size bold:(bool)bold
  304. {
  305. if(name.length==0)
  306. {
  307. name=@"Helvetica";
  308. // name=@"DejaVu Sans";
  309. }
  310. if(size==0)
  311. {
  312. size=10;
  313. }
  314. // NSString* style = @"Regular";
  315. // if(bold)
  316. // style=@"Bold";
  317. //name=@"Courier";
  318. NSDictionary *fontAttributes =
  319. [NSDictionary dictionaryWithObjectsAndKeys:
  320. name, UIFontDescriptorFamilyAttribute,
  321. // style, UIFontDescriptorTextStyleAttribute,
  322. [NSNumber numberWithFloat:size],
  323. UIFontDescriptorSizeAttribute,
  324. nil];
  325. UIFontDescriptor* fontdescriptor=[UIFontDescriptor fontDescriptorWithFontAttributes:fontAttributes];
  326. if(bold)
  327. fontdescriptor = [fontdescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
  328. UIFont * font = [UIFont fontWithDescriptor:fontdescriptor size:0.0];
  329. return font;
  330. //
  331. // // Create a descriptor.
  332. // CFDictionaryRef cffontAttributes =(__bridge CFDictionaryRef)fontAttributes;
  333. // CTFontDescriptorRef descriptor =
  334. // CTFontDescriptorCreateWithAttributes(cffontAttributes);
  335. //
  336. // // CFBridgingRelease(cffontAttributes);
  337. //
  338. // // Create a font using the descriptor.
  339. // CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0.0, NULL);
  340. // CFRelease(descriptor);
  341. //
  342. // return font;
  343. // if(bold)
  344. // {
  345. // CTFontRef font=CTFontCreateWithName((__bridge CFStringRef)name, size, NULL);
  346. // CTFontRef font_bold =CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
  347. // CFBridgingRelease(font);
  348. // return font_bold;
  349. // }
  350. // return CTFontCreateWithName((__bridge CFStringRef)name, size, NULL);
  351. }
  352. //-(NSString*) QueryFontName:(NSString*)font
  353. //{
  354. // if(font==nil)
  355. // {
  356. // return @"Helvetica";
  357. // }
  358. // else if([font.lowercaseString isEqualToString:@"kalinga"])
  359. // {
  360. // return @"kalinga";
  361. // }
  362. // return @"Helvetica";
  363. //}
  364. -(NSMutableAttributedString*) richtext2attributedstring:(NSMutableAttributedString*)string font:(NSString*)font size:(double)size bold:(bool)bold
  365. {
  366. // UILabel* uilabel=[[UILabel alloc]init];
  367. // uilabel.font=[UIFont fontWithName:font size:size];
  368. // NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[self.text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
  369. // uilabel.attributedText = attrStr;
  370. // DebugLog(@"%@", attrStr);
  371. // DebugLog(@"%@", uilabel.attributedText);
  372. // CTFontRef ctfont = [self CreateFont:font size:size bold:bold];//CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);b
  373. UIFont* uifont =[self CreateFont:font size:size bold:bold];
  374. //CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  375. // CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  376. //字体,把helvetica 样式加到整个,string上
  377. [string addAttribute:NSFontAttributeName
  378. value:uifont
  379. range:NSMakeRange(0, [string length])];
  380. // CFRelease(ctfont);
  381. //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  382. [string addAttribute:NSForegroundColorAttributeName
  383. value:self.textColor
  384. range:NSMakeRange(0, [string length])];
  385. if(!bold)
  386. {
  387. // NSString* newstring= nil;
  388. // NSString *content = string.string ;
  389. NSString *pattern = @"\\<b\\>([\\s\\S]*?)\\<\\/b\\>";
  390. // NSArray* matches=[TextUtils expression_varable:content regex:pattern];
  391. // if (matches) {
  392. // for (NSTextCheckingResult *match in matches) {
  393. // for (int i = 0; i < match.numberOfRanges; ++i) {
  394. // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  395. // }
  396. // }
  397. // }
  398. NSTextCheckingResult * match=[TextUtils expression_findfistMatch:string.string regex:pattern];
  399. UIFont* uifontbold = [self CreateFont:font size:size bold:true];
  400. while(match)
  401. {
  402. //for (int i = 0; i < match.numberOfRanges/2; ++i)
  403. {
  404. int i=0;
  405. DebugLog(@"%d->range with richtext label: %lu,%lu",i, (unsigned long)[match rangeAtIndex:i].location,(unsigned long)[match rangeAtIndex:i].length);
  406. DebugLog(@"%d-> range of text: %lu,%lu ",i+1, (unsigned long)[match rangeAtIndex:i+1].location,(unsigned long)[match rangeAtIndex:i+1].length);
  407. DebugLog(@"string length: %lu",(unsigned long)string.string.length);
  408. // CTFontRef ctfont_bold = [self CreateFont:font size:size bold:true];
  409. [string addAttribute:NSFontAttributeName
  410. value:uifontbold
  411. range:[match rangeAtIndex:i+1]];
  412. [string replaceCharactersInRange:NSMakeRange([match rangeAtIndex:i+1].location+[match rangeAtIndex:i+1].length, [match rangeAtIndex:i].location+[match rangeAtIndex:i].length-([match rangeAtIndex:i+1].location+[match rangeAtIndex:i+1].length)) withString:@""];
  413. [string replaceCharactersInRange:NSMakeRange([match rangeAtIndex:i].location, [match rangeAtIndex:i+1].location-[match rangeAtIndex:i].location) withString:@""];
  414. DebugLog(@"string after replace: %@",string.string);
  415. //newstring=[newstring stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[content substringWithRange:[match rangeAtIndex:i+1]]];
  416. match=[TextUtils expression_findfistMatch:string.string regex:pattern];
  417. }
  418. }
  419. // if (matches)
  420. // {
  421. // newstring=content;
  422. // for (NSTextCheckingResult *match in matches)
  423. // {
  424. //
  425. // for (int i = 0; i < match.numberOfRanges/2; ++i)
  426. // {
  427. // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  428. // DebugLog(@"%d-> %@",i+1, [content substringWithRange:[match rangeAtIndex:i+1]]);
  429. //
  430. // [string addAttribute:(id)kCTFontAttributeName
  431. // value:CFBridgingRelease(ctfont_bold)
  432. // range:[match rangeAtIndex:i+1]];
  433. //
  434. // newstring=[newstring stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[content substringWithRange:[match rangeAtIndex:i+1]]];
  435. //
  436. // }
  437. // }
  438. // }
  439. // CFRelease(ctfont_bold);
  440. }
  441. return string;
  442. }
  443. -(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
  444. {
  445. NSString * source=self.drawableTemplate[@"source_type"];
  446. if(source==nil)
  447. source=@"const";
  448. if([source isEqualToString:@"const"])
  449. {
  450. self.text=self.drawableTemplate[@"text"];
  451. // self.text=[self.text stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br>"];
  452. }
  453. else if([source isEqualToString:@"data"])
  454. {
  455. if([self.drawableTemplate[@"source_isexpression"] boolValue])
  456. {
  457. NSString *content = data[self.drawableTemplate[@"source"]];
  458. NSString *pattern = @"\\$\\.\\{([\\s\\S]*?)\\}\\.\\$";
  459. NSArray* matches=[TextUtils expression_varable:content regex:pattern];
  460. // if (matches) {
  461. // for (NSTextCheckingResult *match in matches) {
  462. // for (int i = 0; i < match.numberOfRanges; ++i) {
  463. // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  464. // }
  465. // }
  466. // }
  467. if (matches)
  468. {
  469. self.text=content;
  470. for (NSTextCheckingResult *match in matches)
  471. {
  472. for (int i = 0; i < match.numberOfRanges/2; ++i) {
  473. DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  474. DebugLog(@"%d-> %@",i+1, [content substringWithRange:[match rangeAtIndex:i+1]]);
  475. self.text=[self.text stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[NSString stringWithFormat:@"%@",data[[content substringWithRange:[match rangeAtIndex:i+1]]]]];
  476. }
  477. }
  478. }
  479. //self.text=content;
  480. }
  481. else
  482. {
  483. self.text=data[self.drawableTemplate[@"source"]];
  484. }
  485. }
  486. if(self.text==nil)
  487. self.text=@"";
  488. if([self.text isEqualToString:DEBUG_STRING_VALUE0])
  489. {
  490. self.text =DEBUG_STRING_VALUE1;
  491. }
  492. // if([self.text isEqualToString:@"www.newpacificdirect.com"])
  493. // {
  494. // int debug=0;
  495. // }
  496. bool bold = [self.drawableTemplate[@"bold"] boolValue];
  497. // bool italic = [self.drawableTemplate[@"italic"] boolValue];
  498. NSString* font=self.drawableTemplate[@"font"];
  499. // [super flipContext:context ContextHeight:flip_height];
  500. // super flipContext:context ContextHeight:<#(double)#>
  501. CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
  502. //NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[title dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
  503. //创建AttributeStringfdsa
  504. NSMutableAttributedString *string = nil;
  505. // if([self.drawableTemplate[@"ishtml"] boolValue])
  506. // {
  507. // string=[[[NSAttributedString alloc] initWithData:[self.text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil] mutableCopy];
  508. //
  509. // }
  510. // else
  511. string=[[NSMutableAttributedString alloc] initWithString:self.text];
  512. //创建字体以及字体大小
  513. // CTFontRef ctfont = [self CreateFont:font size:fontsize bold:bold];//CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  514. // // CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  515. // //字体,把helvetica 样式加到整个,string上
  516. // [string addAttribute:(id)kCTFontAttributeName
  517. // value:CFBridgingRelease(ctfont)
  518. // range:NSMakeRange(0, [string length])];
  519. //
  520. // //字体样式 ,把helveticaBold 样式加到整个,string上
  521. // // [string addAttribute:(id)kCTFontAttributeName
  522. // // value:CFBridgingRelease(helveticaBold)
  523. // // range:NSMakeRange(0, [string length])];
  524. //
  525. // //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  526. // [string addAttribute:(id)kCTForegroundColorAttributeName
  527. // value:(id)self.textColor.CGColor
  528. // range:NSMakeRange(0, [string length])];
  529. string = [self richtext2attributedstring:string font:font size:fontsize bold:bold];
  530. // //设置字体间距
  531. // long number = self.characterSpace;
  532. // CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
  533. // [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
  534. //CFRelease(num);
  535. //创建文本对齐方式
  536. // CTTextAlignment alignment = self.textalignment;//对齐方
  537. // CTParagraphStyleSetting alignmentStyle;
  538. // alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
  539. // alignmentStyle.valueSize=sizeof(CTTextAlignment);
  540. // alignmentStyle.value=&alignment;
  541. //
  542. // //创建文本, 行间距
  543. // CGFloat lineSpace=self.lineSpace;//间距数据
  544. // CTParagraphStyleSetting lineSpaceStyle;
  545. // lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
  546. // lineSpaceStyle.valueSize=sizeof(lineSpace);
  547. // lineSpaceStyle.value=&lineSpace;
  548. //
  549. // //设置 line break;
  550. // CTLineBreakMode linebreakmode = kCTLineBreakByTruncatingTail;
  551. // CTParagraphStyleSetting linebreakStyle;
  552. // linebreakStyle.spec = kCTParagraphStyleSpecifierLineBreakMode;
  553. // linebreakStyle.valueSize = sizeof(CGFloat);
  554. // linebreakStyle.value = &linebreakmode;
  555. //
  556. //
  557. // //设置 段落间距
  558. // CGFloat paragraph = self.paragraphSpace;
  559. // CTParagraphStyleSetting paragraphStylesetting;
  560. // paragraphStylesetting.spec = kCTParagraphStyleSpecifierParagraphSpacing;
  561. // paragraphStylesetting.valueSize = sizeof(CGFloat);
  562. // paragraphStylesetting.value = &paragraph;
  563. //
  564. //
  565. //
  566. //
  567. // //创建样式数组
  568. // CTParagraphStyleSetting settings[]={
  569. // alignmentStyle/*,linebreakStyle,lineSpaceStyle,paragraphStylesetting*/
  570. // };
  571. //
  572. // //设置样式
  573. // CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings));
  574. //
  575. // //给字符串添加样式attribute
  576. // [string addAttribute:NSParagraphStyleAttributeName
  577. // value:(__bridge id)(paragraphStyle)
  578. // range:NSMakeRange(0, [string length])];
  579. // CFRelease(paragraphStyle);
  580. NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  581. [style setAlignment:self.textalignment];
  582. // [style setLineBreakMode:NSLineBreakByTruncatingTail];
  583. [style setLineBreakMode:NSLineBreakByWordWrapping];
  584. // style SETLINE
  585. [string addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [string length])];
  586. if([self.text isEqualToString:DEBUG_STRING_VALUE1])
  587. {
  588. // bool debug=true;
  589. DebugLog(@"textdrawable text:%@",self.text);
  590. DebugLog(@"string %@",string);
  591. // DebugLog(@"compute size %@", NSStringFromCGSize(CGSizeMake(self.rect.size.width-self.margin_left-self.margin_right, MAXFLOAT)) );
  592. // DebugLog(@"tmpSize %@", NSStringFromCGSize(tmpSize) );
  593. // DebugLog(@"p_rect %@", NSStringFromCGRect(p_rect) );
  594. // DebugLog(@"self.rect %@", NSStringFromCGRect(self.rect) );
  595. }
  596. // CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  597. // [string drawInRect:pdfrect];
  598. // [self.text drawInRect:pdfrect withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:40],NSForegroundColorAttributeName : [UIColor redColor]}];
  599. // layout master
  600. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);
  601. //计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
  602. CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.rect.size.width-self.margin_left-self.margin_right, MAXFLOAT), NULL);
  603. //创建textBoxSize以设置view的frame
  604. // CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
  605. // DebugLog(@"textBoxSize0 == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);
  606. // self.frame = CGRectMake(0, 0, textBoxSize.width , textBoxSize.height);
  607. // [string release];
  608. //- (void)drawRect:(CGRect)rect;代码
  609. CGMutablePathRef leftColumnPath = CGPathCreateMutable();
  610. CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  611. bool expand=[self.drawableTemplate[@"expand"] boolValue];
  612. double height =0;
  613. if(tmpSize.height<parentrect.size.height)
  614. height=tmpSize.height;
  615. else
  616. if(expand)
  617. {
  618. height=tmpSize.height;
  619. self.rect = CGRectMake(self.rect.origin.x, self.rect.origin.y, self.rect.size.width, height+self.margin_top+self.margin_bottom);
  620. }
  621. else
  622. height=parentrect.size.height;
  623. CGRect rect=CGRectMake(parentrect.origin.x, parentrect.origin.y, parentrect.size.width, height);
  624. CGRect expand_rect=rect;
  625. if([self.text isEqualToString:DEBUG_STRING_VALUE1])
  626. {
  627. // bool debug=true;
  628. DebugLog(@"textdrawable text:%@",self.text);
  629. DebugLog(@"tmpSize %@", NSStringFromCGSize(tmpSize) );
  630. DebugLog(@"p_rect %@", NSStringFromCGRect(p_rect) );
  631. DebugLog(@"self.rect %@", NSStringFromCGRect(self.rect) );
  632. DebugLog(@"expand %@", expand?@"true":@"false" );
  633. DebugLog(@"height %f", height );
  634. DebugLog(@"expand_rect %@", NSStringFromCGRect(expand_rect) );
  635. }
  636. // CGRect rect=[RAUtils rectAlign:parentrect rect:scalerect hAlign:self.hAlign vAlign:self.vAlign];
  637. rect=[TextUtils rectVAlign:parentrect rect:rect vAlign:self.vAlign];
  638. if([self.text isEqualToString:DEBUG_STRING_VALUE1])
  639. {
  640. // bool debug=true;
  641. DebugLog(@"textdrawable text:%@",self.text);
  642. DebugLog(@"tmpSize %@", NSStringFromCGSize(tmpSize) );
  643. DebugLog(@"p_rect %@", NSStringFromCGRect(p_rect) );
  644. DebugLog(@"self.rect %@", NSStringFromCGRect(self.rect) );
  645. DebugLog(@"expand %@", expand?@"true":@"false" );
  646. DebugLog(@"height %f", height );
  647. DebugLog(@"expand_rect %@", NSStringFromCGRect(expand_rect) );
  648. DebugLog(@"rect %@", NSStringFromCGRect(rect) );
  649. DebugLog(@"vAlign %@", self.vAlign );
  650. }
  651. // CGRect pdfrect = [self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];
  652. rect= [self parent_to_pdf_rect:rect contextHeight:flip_height];
  653. // pdfrect=CGRectMake(pdfrect.origin.x, flip_height-pdfrect.origin.y, pdfrect.size.width, pdfrect.size.height);
  654. CGPathAddRect(leftColumnPath, NULL,
  655. rect);
  656. CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
  657. CFRangeMake(0, 0),
  658. leftColumnPath, NULL);
  659. // DebugLog(@"textBoxSize1 == %f,%f",self.frame.size.width,self.frame.size.height);
  660. // flip the coordinate system
  661. // CGContextRef context = UIGraphicsGetCurrentContext();
  662. // CGContextClearRect(context, self.rect);
  663. // CGContextSetFillColorWithColor(context, [[UIColor whiteColor]CGColor]);
  664. // CGContextFillRect(context, CGRectMake(0, 0, self.rect.size.width, self.rect.size.height));
  665. CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  666. // CGContextTranslateCTM(context, 0, self.rect.size.height);
  667. // CGContextScaleCTM(context, 1.0, -1.0);
  668. // draw
  669. CTFrameDraw(leftFrame, context);
  670. CFRelease(leftFrame);
  671. // cleanup
  672. CGPathRelease(leftColumnPath);
  673. CFRelease(framesetter);
  674. // return self.rect;
  675. [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  676. if(expand&&expand_rect.size.height>parentrect.size.height)
  677. return expand_rect;
  678. else
  679. return parentrect;
  680. // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  681. // [super restoreFlip:context ContextHeight:flip_height];
  682. }
  683. @end
  684. ////
  685. //// TextDrawable.m
  686. //// pdftest
  687. ////
  688. //// Created by Ray on 10/12/16.
  689. //// Copyright © 2016 United Software Applications, Inc. All rights reserved.
  690. ////
  691. //
  692. //#import "TextDrawable.h"
  693. //#import <CoreText/CoreText.h>
  694. //#import "TextUtils.h"
  695. //
  696. //@implementation TextDrawable
  697. //
  698. //
  699. //-(instancetype) init:(CGRect) rect source_type:(NSString*) source_type source:(NSString*)source textAlignment:(NSString*)textAlignment vAlign:(NSString*)vAlign size:(NSString*)size
  700. //{
  701. //
  702. // if (self = [super init]) {
  703. // // NSMutableDictionary* m1=[drawableTemplate mutableCopy];
  704. // // NSMutableDictionary* m2=[drawableTemplate mutableCopy];
  705. // // [self setDrawableTemplate:[[RAUtils string2dict:[RAUtils dict2string:drawableTemplate]] mutableCopy] ];
  706. //
  707. // NSMutableDictionary * drawableTemplate = [TextDrawable createTextTemplate:rect source_type:source_type source:source textAlignment:textAlignment vAlign:vAlign size:size];
  708. //
  709. //
  710. // [self setDrawableTemplate:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:drawableTemplate]] ];
  711. //
  712. // }
  713. // return self;
  714. //
  715. //}
  716. //+(NSMutableDictionary *) createTextTemplate:(CGRect) rect source_type:(NSString*) source_type source:(NSString*)source textAlignment:(NSString*)textAlignment vAlign:(NSString*)vAlign size:(NSString*)size
  717. //{
  718. //
  719. //
  720. //
  721. //
  722. //
  723. //
  724. // if(textAlignment.length==0)
  725. // textAlignment= @"Center";
  726. // if(vAlign.length==0)
  727. // vAlign= @"Middle";
  728. // if(size.length==0)
  729. // vAlign= @"10";
  730. //
  731. //
  732. //
  733. // NSMutableDictionary* ret=[super createDrawableTemplate:rect];
  734. //
  735. // ret[@"type"]=@"text" ;
  736. // ret[@"source_type"]=source_type ;
  737. // ret[@"source"]=source ;
  738. // ret[@"text"]=source ;
  739. // ret[@"textAlignment"]=textAlignment ;
  740. // ret[@"vAlign"]=vAlign ;
  741. // ret[@"size"]=size ;
  742. //
  743. // return ret;
  744. //}
  745. //
  746. //
  747. //-(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
  748. //{
  749. //
  750. // [super setDrawableTemplate:drawableTemplate];
  751. //
  752. //
  753. //
  754. // NSString* align= drawableTemplate[@"textAlignment"];
  755. // if(align.length==0 || [align.lowercaseString isEqualToString:@"left"])
  756. // {
  757. // self.textalignment= kCTTextAlignmentLeft;
  758. // }
  759. // else if([align.lowercaseString isEqualToString:@"right"])
  760. // {
  761. // self.textalignment= kCTTextAlignmentRight;
  762. // }
  763. // else if([align.lowercaseString isEqualToString:@"center"])
  764. // {
  765. // self.textalignment= kCTTextAlignmentCenter;
  766. // }
  767. // else
  768. // {
  769. // self.textalignment= kCTTextAlignmentLeft;
  770. // }
  771. //
  772. //// self.lineSpace = 0;
  773. //// self.paragraphSpace = 0;
  774. //
  775. //
  776. // NSString* colorvalue=drawableTemplate[@"color"];
  777. // if(colorvalue.length>0 )
  778. // {
  779. // unsigned long color = strtoul([colorvalue UTF8String],0,16);
  780. // self.textColor= UIColorFromRGB(color);
  781. // }
  782. // else
  783. // {
  784. // self.textColor= [UIColor blackColor];
  785. //
  786. // }
  787. //
  788. //
  789. // // kCTTextAlignmentJustified CT_ENUM_AVAILABLE(10_8, 6_0) = 3,
  790. // // kCTTextAlignmentNatural CT_ENUM_AVAILABLE(10_8, 6_0) = 4,
  791. //
  792. //
  793. // // _drawableTemplate = drawableTemplate;
  794. // // self.margin_left=[self.drawableTemplate[@"margin_left"] doubleValue];
  795. // // self.margin_right=[self.drawableTemplate[@"margin_right"] doubleValue];
  796. // // self.margin_top=[self.drawableTemplate[@"margin_top"] doubleValue];
  797. // // self.margin_bottom=[self.drawableTemplate[@"margin_bottom"] doubleValue];
  798. // // self.rect = CGRectMake(0, 0, [self.drawableTemplate[@"width"] doubleValue], [self.drawableTemplate[@"height"] doubleValue]);
  799. //}
  800. //
  801. //
  802. //-(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
  803. //{
  804. //
  805. // bool expand=[self.drawableTemplate[@"expand"] boolValue];
  806. // if(!expand)
  807. // {
  808. // CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  809. //
  810. // return parentrect;
  811. // }
  812. // else
  813. // {
  814. //
  815. //
  816. //
  817. //
  818. // NSString * source=self.drawableTemplate[@"source_type"];
  819. // if(source==nil)
  820. // source=@"const";
  821. // if([source isEqualToString:@"const"])
  822. // {
  823. // self.text=self.drawableTemplate[@"text"];
  824. // // self.text=[self.text stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br>"];
  825. //
  826. // }
  827. // else if([source isEqualToString:@"data"])
  828. // {
  829. // if([self.drawableTemplate[@"source_isexpression"] boolValue])
  830. // {
  831. // NSString *content = data[self.drawableTemplate[@"source"]];
  832. // NSString *pattern = @"\\$\\.\\{([\\s\\S]*?)\\}\\.\\$";
  833. // NSArray* matches=[TextUtils expression_varable:content regex:pattern];
  834. // // if (matches) {
  835. // // for (NSTextCheckingResult *match in matches) {
  836. // // for (int i = 0; i < match.numberOfRanges; ++i) {
  837. // // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  838. // // }
  839. // // }
  840. // // }
  841. // if (matches)
  842. // {
  843. // self.text=content;
  844. // for (NSTextCheckingResult *match in matches)
  845. // {
  846. //
  847. // for (int i = 0; i < match.numberOfRanges/2; ++i) {
  848. // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  849. // DebugLog(@"%d-> %@",i+1, [content substringWithRange:[match rangeAtIndex:i+1]]);
  850. //
  851. // self.text=[self.text stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[NSString stringWithFormat:@"%@",data[[content substringWithRange:[match rangeAtIndex:i+1]]]]];
  852. // }
  853. // }
  854. // }
  855. // //self.text=content;
  856. //
  857. // }
  858. // else
  859. // {
  860. // self.text=data[self.drawableTemplate[@"source"]];
  861. // }
  862. // }
  863. //
  864. // if(self.text==nil)
  865. // self.text=@"";
  866. //
  867. //
  868. //
  869. //
  870. //
  871. //// if([self.text isEqualToString:@"www.newpacificdirect.com"])
  872. //// {
  873. //// int debug=0;
  874. //// }
  875. //
  876. //
  877. // bool bold = [self.drawableTemplate[@"bold"] boolValue];
  878. // bool italic = [self.drawableTemplate[@"italic"] boolValue];
  879. // NSString* font=self.drawableTemplate[@"font"];
  880. //
  881. // // [super flipContext:context ContextHeight:flip_height];
  882. // // super flipContext:context ContextHeight:<#(double)#>
  883. // CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
  884. //
  885. // //NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[title dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
  886. // //创建AttributeStringfdsa
  887. //
  888. // NSMutableAttributedString *string = nil;
  889. // // if([self.drawableTemplate[@"ishtml"] boolValue])
  890. // // {
  891. // // string=[[[NSAttributedString alloc] initWithData:[self.text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil] mutableCopy];
  892. // //
  893. // // }
  894. // // else
  895. // {
  896. // string=[[NSMutableAttributedString alloc] initWithString:self.text];
  897. // //创建字体以及字体大小
  898. // // CTFontRef ctfont = [self CreateFont:font size:fontsize bold:bold];//CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  899. // // // CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  900. // // //字体,把helvetica 样式加到整个,string上
  901. // // [string addAttribute:(id)kCTFontAttributeName
  902. // // value:CFBridgingRelease(ctfont)
  903. // // range:NSMakeRange(0, [string length])];
  904. // //
  905. // // //字体样式 ,把helveticaBold 样式加到整个,string上
  906. // // // [string addAttribute:(id)kCTFontAttributeName
  907. // // // value:CFBridgingRelease(helveticaBold)
  908. // // // range:NSMakeRange(0, [string length])];
  909. // //
  910. // // //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  911. // // [string addAttribute:(id)kCTForegroundColorAttributeName
  912. // // value:(id)self.textColor.CGColor
  913. // // range:NSMakeRange(0, [string length])];
  914. //
  915. // string = [self richtext2attributedstring:string font:font size:fontsize bold:bold];
  916. //
  917. //
  918. // //设置字体间距
  919. // long number = self.characterSpace;
  920. // CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
  921. // [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
  922. // //CFRelease(num);
  923. //
  924. //
  925. // //创建文本对齐方式
  926. // CTTextAlignment alignment = self.textalignment;//对齐方
  927. // CTParagraphStyleSetting alignmentStyle;
  928. // alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
  929. // alignmentStyle.valueSize=sizeof(alignment);
  930. // alignmentStyle.value=&alignment;
  931. //
  932. // //创建文本, 行间距
  933. // CGFloat lineSpace=self.lineSpace;//间距数据
  934. // CTParagraphStyleSetting lineSpaceStyle;
  935. // lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
  936. // lineSpaceStyle.valueSize=sizeof(lineSpace);
  937. // lineSpaceStyle.value=&lineSpace;
  938. //
  939. // //设置 line break;
  940. // CTLineBreakMode linebreakmode = kCTLineBreakByTruncatingTail;
  941. // CTParagraphStyleSetting linebreakStyle;
  942. // linebreakStyle.spec = kCTParagraphStyleSpecifierLineBreakMode;
  943. // linebreakStyle.valueSize = sizeof(CGFloat);
  944. // linebreakStyle.value = &linebreakmode;
  945. //
  946. //
  947. // //设置 段落间距
  948. // CGFloat paragraph = self.paragraphSpace;
  949. // CTParagraphStyleSetting paragraphStyle;
  950. // paragraphStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
  951. // paragraphStyle.valueSize = sizeof(CGFloat);
  952. // paragraphStyle.value = &paragraph;
  953. //
  954. //
  955. //
  956. //
  957. // //创建样式数组
  958. // CTParagraphStyleSetting settings[]={
  959. // alignmentStyle,lineSpaceStyle,paragraphStyle,linebreakStyle
  960. // };
  961. //
  962. // //设置样式
  963. // CTParagraphStyleRef paragraphStyle1 = CTParagraphStyleCreate(settings, sizeof(settings));
  964. //
  965. // //给字符串添加样式attribute
  966. // [string addAttribute:(id)kCTParagraphStyleAttributeName
  967. // value:CFBridgingRelease(paragraphStyle1)
  968. // range:NSMakeRange(0, [string length])];
  969. //
  970. // }
  971. //
  972. //
  973. //
  974. // // CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  975. // // [string drawInRect:pdfrect];
  976. //
  977. //
  978. // // [self.text drawInRect:pdfrect withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:40],NSForegroundColorAttributeName : [UIColor redColor]}];
  979. //
  980. // // layout master
  981. // CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
  982. // //计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
  983. // CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.rect.size.width-self.margin_left-self.margin_right, MAXFLOAT), NULL);
  984. // //创建textBoxSize以设置view的frame
  985. // CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
  986. // // DebugLog(@"textBoxSize0 == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);
  987. // // self.frame = CGRectMake(0, 0, textBoxSize.width , textBoxSize.height);
  988. // // [string release];
  989. //
  990. // //- (void)drawRect:(CGRect)rect;代码
  991. //
  992. //
  993. //
  994. //
  995. // // CGMutablePathRef leftColumnPath = CGPathCreateMutable();
  996. //
  997. // CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  998. //
  999. //
  1000. // double height =0;
  1001. // if(tmpSize.height<parentrect.size.height)
  1002. // height=tmpSize.height;
  1003. // else
  1004. // if(expand)
  1005. // {
  1006. // height=tmpSize.height;
  1007. // self.rect = CGRectMake(self.rect.origin.x, self.rect.origin.y, self.rect.size.width, height+self.margin_top+self.margin_bottom);
  1008. // }
  1009. // else
  1010. // height=parentrect.size.height;
  1011. //
  1012. // CGRect rect=CGRectMake(parentrect.origin.x, parentrect.origin.y, parentrect.size.width, height);
  1013. //
  1014. // CGRect expand_rect=rect;
  1015. //
  1016. // // CGRect rect=[RAUtils rectAlign:parentrect rect:scalerect hAlign:self.hAlign vAlign:self.vAlign];
  1017. // // rect=[RAUtils rectVAlign:parentrect rect:rect vAlign:self.vAlign];
  1018. // //
  1019. // // // CGRect pdfrect = [self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];
  1020. // //
  1021. // // rect= [self parent_to_pdf_rect:rect contextHeight:flip_height];
  1022. // // // pdfrect=CGRectMake(pdfrect.origin.x, flip_height-pdfrect.origin.y, pdfrect.size.width, pdfrect.size.height);
  1023. // // CGPathAddRect(leftColumnPath, NULL,
  1024. // // rect);
  1025. // //
  1026. // // CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
  1027. // // CFRangeMake(0, 0),
  1028. // // leftColumnPath, NULL);
  1029. // //
  1030. // // // DebugLog(@"textBoxSize1 == %f,%f",self.frame.size.width,self.frame.size.height);
  1031. // // // flip the coordinate system
  1032. // // // CGContextRef context = UIGraphicsGetCurrentContext();
  1033. // //
  1034. // //
  1035. // // // CGContextClearRect(context, self.rect);
  1036. // // // CGContextSetFillColorWithColor(context, [[UIColor whiteColor]CGColor]);
  1037. // // // CGContextFillRect(context, CGRectMake(0, 0, self.rect.size.width, self.rect.size.height));
  1038. // //
  1039. // //
  1040. // // CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  1041. // // // CGContextTranslateCTM(context, 0, self.rect.size.height);
  1042. // // // CGContextScaleCTM(context, 1.0, -1.0);
  1043. // //
  1044. // // // draw
  1045. // // CTFrameDraw(leftFrame, context);
  1046. // //
  1047. // //
  1048. // // CFRelease(leftFrame);
  1049. // // // cleanup
  1050. // //
  1051. // // CGPathRelease(leftColumnPath);
  1052. // CFRelease(framesetter);
  1053. //
  1054. //
  1055. // // return self.rect;
  1056. // // [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  1057. // if(expand&&expand_rect.size.height>parentrect.size.height)
  1058. // return expand_rect;
  1059. // else
  1060. // return parentrect;
  1061. // // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  1062. //
  1063. // // [super restoreFlip:context ContextHeight:flip_height];
  1064. // }
  1065. //
  1066. //
  1067. //}
  1068. //-(CTFontRef)CreateFont:(NSString*)name size:(double)size bold:(bool)bold
  1069. //{
  1070. //
  1071. // if(name.length==0)
  1072. // {
  1073. // name=@"Helvetica";
  1074. // // name=@"DejaVu Sans";
  1075. // }
  1076. // if(size==0)
  1077. // {
  1078. // size=10;
  1079. // }
  1080. // NSString* style = @"Regular";
  1081. // if(bold)
  1082. // style=@"Bold";
  1083. //
  1084. // //name=@"Courier";
  1085. //
  1086. // NSDictionary *fontAttributes =
  1087. // [NSDictionary dictionaryWithObjectsAndKeys:
  1088. // name, (NSString *)kCTFontFamilyNameAttribute,
  1089. // style, (NSString *)kCTFontStyleNameAttribute,
  1090. // [NSNumber numberWithFloat:size],
  1091. // (NSString *)kCTFontSizeAttribute,
  1092. // nil];
  1093. // // Create a descriptor.
  1094. // CFDictionaryRef cffontAttributes =(__bridge CFDictionaryRef)fontAttributes;
  1095. // CTFontDescriptorRef descriptor =
  1096. // CTFontDescriptorCreateWithAttributes(cffontAttributes);
  1097. //
  1098. // // CFBridgingRelease(cffontAttributes);
  1099. //
  1100. // // Create a font using the descriptor.
  1101. // CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0.0, NULL);
  1102. // CFBridgingRelease(descriptor);
  1103. //
  1104. // return font;
  1105. //
  1106. // // if(bold)
  1107. // // {
  1108. // // CTFontRef font=CTFontCreateWithName((__bridge CFStringRef)name, size, NULL);
  1109. // // CTFontRef font_bold =CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
  1110. // // CFBridgingRelease(font);
  1111. // // return font_bold;
  1112. // // }
  1113. // // return CTFontCreateWithName((__bridge CFStringRef)name, size, NULL);
  1114. //}
  1115. ////-(NSString*) QueryFontName:(NSString*)font
  1116. ////{
  1117. //// if(font==nil)
  1118. //// {
  1119. //// return @"Helvetica";
  1120. //// }
  1121. //// else if([font.lowercaseString isEqualToString:@"kalinga"])
  1122. //// {
  1123. //// return @"kalinga";
  1124. //// }
  1125. //// return @"Helvetica";
  1126. ////}
  1127. //-(NSMutableAttributedString*) richtext2attributedstring:(NSMutableAttributedString*)string font:(NSString*)font size:(double)size bold:(bool)bold
  1128. //{
  1129. // // UILabel* uilabel=[[UILabel alloc]init];
  1130. // // uilabel.font=[UIFont fontWithName:font size:size];
  1131. // // NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[self.text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
  1132. // // uilabel.attributedText = attrStr;
  1133. // // DebugLog(@"%@", attrStr);
  1134. // // DebugLog(@"%@", uilabel.attributedText);
  1135. //
  1136. // CTFontRef ctfont = [self CreateFont:font size:size bold:bold];//CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);b
  1137. //
  1138. // //CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  1139. // // CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  1140. // //字体,把helvetica 样式加到整个,string上
  1141. // [string addAttribute:(id)kCTFontAttributeName
  1142. // value:CFBridgingRelease(ctfont)
  1143. // range:NSMakeRange(0, [string length])];
  1144. //
  1145. // //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  1146. // [string addAttribute:(id)kCTForegroundColorAttributeName
  1147. // value:(id)self.textColor.CGColor
  1148. // range:NSMakeRange(0, [string length])];
  1149. //
  1150. //
  1151. // if(!bold)
  1152. // {
  1153. // CTFontRef ctfont_bold = [self CreateFont:font size:size bold:true];
  1154. // NSString* newstring= nil;
  1155. // // NSString *content = string.string ;
  1156. // NSString *pattern = @"\\<b\\>([\\s\\S]*?)\\<\\/b\\>";
  1157. // // NSArray* matches=[TextUtils expression_varable:content regex:pattern];
  1158. // // if (matches) {
  1159. // // for (NSTextCheckingResult *match in matches) {
  1160. // // for (int i = 0; i < match.numberOfRanges; ++i) {
  1161. // // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  1162. // // }
  1163. // // }
  1164. // // }
  1165. // NSTextCheckingResult * match=[TextUtils expression_findfistMatch:string.string regex:pattern];
  1166. //
  1167. // while(match)
  1168. // {
  1169. // //for (int i = 0; i < match.numberOfRanges/2; ++i)
  1170. // {
  1171. // int i=0;
  1172. // DebugLog(@"%d->range with richtext label: %d,%d",i, [match rangeAtIndex:i].location,[match rangeAtIndex:i].length);
  1173. // DebugLog(@"%d-> range of text: %d,%d ",i+1, [match rangeAtIndex:i+1].location,[match rangeAtIndex:i+1].length);
  1174. //
  1175. //
  1176. // DebugLog(@"string length: %d",string.string.length);
  1177. // [string addAttribute:(id)kCTFontAttributeName
  1178. // value:(__bridge id _Nonnull)(ctfont_bold)
  1179. // range:[match rangeAtIndex:i+1]];
  1180. //
  1181. // [string replaceCharactersInRange:NSMakeRange([match rangeAtIndex:i+1].location+[match rangeAtIndex:i+1].length, [match rangeAtIndex:i].location+[match rangeAtIndex:i].length-([match rangeAtIndex:i+1].location+[match rangeAtIndex:i+1].length)) withString:@""];
  1182. // [string replaceCharactersInRange:NSMakeRange([match rangeAtIndex:i].location, [match rangeAtIndex:i+1].location-[match rangeAtIndex:i].location) withString:@""];
  1183. //
  1184. // DebugLog(@"string after replace: %@",string.string);
  1185. // //newstring=[newstring stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[content substringWithRange:[match rangeAtIndex:i+1]]];
  1186. // match=[TextUtils expression_findfistMatch:string.string regex:pattern];
  1187. // }
  1188. // }
  1189. // // if (matches)
  1190. // // {
  1191. // // newstring=content;
  1192. // // for (NSTextCheckingResult *match in matches)
  1193. // // {
  1194. // //
  1195. // // for (int i = 0; i < match.numberOfRanges/2; ++i)
  1196. // // {
  1197. // // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  1198. // // DebugLog(@"%d-> %@",i+1, [content substringWithRange:[match rangeAtIndex:i+1]]);
  1199. // //
  1200. // // [string addAttribute:(id)kCTFontAttributeName
  1201. // // value:CFBridgingRelease(ctfont_bold)
  1202. // // range:[match rangeAtIndex:i+1]];
  1203. // //
  1204. // // newstring=[newstring stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[content substringWithRange:[match rangeAtIndex:i+1]]];
  1205. // //
  1206. // // }
  1207. // // }
  1208. // // }
  1209. //
  1210. //
  1211. //
  1212. // CFBridgingRelease(ctfont_bold);
  1213. // }
  1214. //
  1215. //
  1216. // return string;
  1217. //}
  1218. //-(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
  1219. //{
  1220. //
  1221. //
  1222. //
  1223. //
  1224. // NSString * source=self.drawableTemplate[@"source_type"];
  1225. // if(source==nil)
  1226. // source=@"const";
  1227. // if([source isEqualToString:@"const"])
  1228. // {
  1229. // self.text=self.drawableTemplate[@"text"];
  1230. // // self.text=[self.text stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br>"];
  1231. //
  1232. // }
  1233. // else if([source isEqualToString:@"data"])
  1234. // {
  1235. // if([self.drawableTemplate[@"source_isexpression"] boolValue])
  1236. // {
  1237. // NSString *content = data[self.drawableTemplate[@"source"]];
  1238. // NSString *pattern = @"\\$\\.\\{([\\s\\S]*?)\\}\\.\\$";
  1239. // NSArray* matches=[TextUtils expression_varable:content regex:pattern];
  1240. // // if (matches) {
  1241. // // for (NSTextCheckingResult *match in matches) {
  1242. // // for (int i = 0; i < match.numberOfRanges; ++i) {
  1243. // // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  1244. // // }
  1245. // // }
  1246. // // }
  1247. // if (matches)
  1248. // {
  1249. // self.text=content;
  1250. // for (NSTextCheckingResult *match in matches)
  1251. // {
  1252. //
  1253. // for (int i = 0; i < match.numberOfRanges/2; ++i) {
  1254. // DebugLog(@"%d-> %@",i, [content substringWithRange:[match rangeAtIndex:i]]);
  1255. // DebugLog(@"%d-> %@",i+1, [content substringWithRange:[match rangeAtIndex:i+1]]);
  1256. //
  1257. // self.text=[self.text stringByReplacingOccurrencesOfString:[content substringWithRange:[match rangeAtIndex:i]] withString:[NSString stringWithFormat:@"%@",data[[content substringWithRange:[match rangeAtIndex:i+1]]]]];
  1258. // }
  1259. // }
  1260. // }
  1261. // //self.text=content;
  1262. //
  1263. // }
  1264. // else
  1265. // {
  1266. // self.text=data[self.drawableTemplate[@"source"]];
  1267. // }
  1268. // }
  1269. //
  1270. // if(self.text==nil)
  1271. // self.text=@"";
  1272. //
  1273. //
  1274. //
  1275. //
  1276. //
  1277. //// if([self.text isEqualToString:@"www.newpacificdirect.com"])
  1278. //// {
  1279. //// int debug=0;
  1280. //// }
  1281. //
  1282. //
  1283. // bool bold = [self.drawableTemplate[@"bold"] boolValue];
  1284. // bool italic = [self.drawableTemplate[@"italic"] boolValue];
  1285. // NSString* font=self.drawableTemplate[@"font"];
  1286. //
  1287. // // [super flipContext:context ContextHeight:flip_height];
  1288. // // super flipContext:context ContextHeight:<#(double)#>
  1289. // CGFloat fontsize = [self.drawableTemplate[@"size"] doubleValue];
  1290. //
  1291. // //NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[title dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
  1292. // //创建AttributeStringfdsa
  1293. //
  1294. // NSMutableAttributedString *string = nil;
  1295. // // if([self.drawableTemplate[@"ishtml"] boolValue])
  1296. // // {
  1297. // // string=[[[NSAttributedString alloc] initWithData:[self.text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil] mutableCopy];
  1298. // //
  1299. // // }
  1300. // // else
  1301. //
  1302. // string=[[NSMutableAttributedString alloc] initWithString:self.text];
  1303. // //创建字体以及字体大小
  1304. // // CTFontRef ctfont = [self CreateFont:font size:fontsize bold:bold];//CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  1305. // // // CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica"), fontsize, NULL);
  1306. // // //字体,把helvetica 样式加到整个,string上
  1307. // // [string addAttribute:(id)kCTFontAttributeName
  1308. // // value:CFBridgingRelease(ctfont)
  1309. // // range:NSMakeRange(0, [string length])];
  1310. // //
  1311. // // //字体样式 ,把helveticaBold 样式加到整个,string上
  1312. // // // [string addAttribute:(id)kCTFontAttributeName
  1313. // // // value:CFBridgingRelease(helveticaBold)
  1314. // // // range:NSMakeRange(0, [string length])];
  1315. // //
  1316. // // //颜色,此处为黑色,你可以自己改颜色,[UIColor redColor]
  1317. // // [string addAttribute:(id)kCTForegroundColorAttributeName
  1318. // // value:(id)self.textColor.CGColor
  1319. // // range:NSMakeRange(0, [string length])];
  1320. //
  1321. // string = [self richtext2attributedstring:string font:font size:fontsize bold:bold];
  1322. //
  1323. //
  1324. // //设置字体间距
  1325. // long number = self.characterSpace;
  1326. // CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &number);
  1327. // [string addAttribute:(id)kCTKernAttributeName value:CFBridgingRelease(num) range:NSMakeRange(0, [string length])];
  1328. // //CFRelease(num);
  1329. //
  1330. //
  1331. // //创建文本对齐方式
  1332. // CTTextAlignment alignment = self.textalignment;//对齐方
  1333. // CTParagraphStyleSetting alignmentStyle;
  1334. // alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;
  1335. // alignmentStyle.valueSize=sizeof(alignment);
  1336. // alignmentStyle.value=&alignment;
  1337. //
  1338. // //创建文本, 行间距
  1339. // CGFloat lineSpace=self.lineSpace;//间距数据
  1340. // CTParagraphStyleSetting lineSpaceStyle;
  1341. // lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;
  1342. // lineSpaceStyle.valueSize=sizeof(lineSpace);
  1343. // lineSpaceStyle.value=&lineSpace;
  1344. //
  1345. // //设置 line break;
  1346. // CTLineBreakMode linebreakmode = kCTLineBreakByTruncatingTail;
  1347. // CTParagraphStyleSetting linebreakStyle;
  1348. // linebreakStyle.spec = kCTParagraphStyleSpecifierLineBreakMode;
  1349. // linebreakStyle.valueSize = sizeof(CGFloat);
  1350. // linebreakStyle.value = &linebreakmode;
  1351. //
  1352. //
  1353. // //设置 段落间距
  1354. // CGFloat paragraph = self.paragraphSpace;
  1355. // CTParagraphStyleSetting paragraphStylesetting;
  1356. // paragraphStylesetting.spec = kCTParagraphStyleSpecifierParagraphSpacing;
  1357. // paragraphStylesetting.valueSize = sizeof(CGFloat);
  1358. // paragraphStylesetting.value = &paragraph;
  1359. //
  1360. //
  1361. //
  1362. //
  1363. // //创建样式数组
  1364. // CTParagraphStyleSetting settings[]={
  1365. // alignmentStyle,lineSpaceStyle,paragraphStylesetting,linebreakStyle
  1366. // };
  1367. //
  1368. // //设置样式
  1369. // CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings));
  1370. //
  1371. // //给字符串添加样式attribute
  1372. // [string addAttribute:(id)kCTParagraphStyleAttributeName
  1373. // value:CFBridgingRelease(paragraphStyle)
  1374. // range:NSMakeRange(0, [string length])];
  1375. //
  1376. //
  1377. //
  1378. //
  1379. //
  1380. // // CGRect pdfrect=[self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  1381. // // [string drawInRect:pdfrect];
  1382. //
  1383. //
  1384. // // [self.text drawInRect:pdfrect withAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:40],NSForegroundColorAttributeName : [UIColor redColor]}];
  1385. //
  1386. // // layout master
  1387. // CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
  1388. // //计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
  1389. // CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.rect.size.width-self.margin_left-self.margin_right, MAXFLOAT), NULL);
  1390. // //创建textBoxSize以设置view的frame
  1391. // CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);
  1392. // // DebugLog(@"textBoxSize0 == %f,%f,%f",textBoxSize.width,textBoxSize.height,textBoxSize.width / textBoxSize.height);
  1393. // // self.frame = CGRectMake(0, 0, textBoxSize.width , textBoxSize.height);
  1394. // // [string release];
  1395. //
  1396. // //- (void)drawRect:(CGRect)rect;代码
  1397. //
  1398. //
  1399. //
  1400. //
  1401. // CGMutablePathRef leftColumnPath = CGPathCreateMutable();
  1402. //
  1403. // CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  1404. //
  1405. // bool expand=[self.drawableTemplate[@"expand"] boolValue];
  1406. // double height =0;
  1407. // if(tmpSize.height<parentrect.size.height)
  1408. // height=tmpSize.height;
  1409. // else
  1410. // if(expand)
  1411. // {
  1412. // height=tmpSize.height;
  1413. // self.rect = CGRectMake(self.rect.origin.x, self.rect.origin.y, self.rect.size.width, height+self.margin_top+self.margin_bottom);
  1414. // }
  1415. // else
  1416. // height=parentrect.size.height;
  1417. //
  1418. // CGRect rect=CGRectMake(parentrect.origin.x, parentrect.origin.y, parentrect.size.width, height);
  1419. //
  1420. // CGRect expand_rect=rect;
  1421. //
  1422. // // CGRect rect=[RAUtils rectAlign:parentrect rect:scalerect hAlign:self.hAlign vAlign:self.vAlign];
  1423. // rect=[TextUtils rectVAlign:parentrect rect:rect vAlign:self.vAlign];
  1424. //
  1425. // // CGRect pdfrect = [self to_pdf_rect:p_rect pos:self.rect contextHeight:flip_height];
  1426. //
  1427. // rect= [self parent_to_pdf_rect:rect contextHeight:flip_height];
  1428. // // pdfrect=CGRectMake(pdfrect.origin.x, flip_height-pdfrect.origin.y, pdfrect.size.width, pdfrect.size.height);
  1429. // CGPathAddRect(leftColumnPath, NULL,
  1430. // rect);
  1431. //
  1432. // CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
  1433. // CFRangeMake(0, 0),
  1434. // leftColumnPath, NULL);
  1435. //
  1436. // // DebugLog(@"textBoxSize1 == %f,%f",self.frame.size.width,self.frame.size.height);
  1437. // // flip the coordinate system
  1438. // // CGContextRef context = UIGraphicsGetCurrentContext();
  1439. //
  1440. //
  1441. // // CGContextClearRect(context, self.rect);
  1442. // // CGContextSetFillColorWithColor(context, [[UIColor whiteColor]CGColor]);
  1443. // // CGContextFillRect(context, CGRectMake(0, 0, self.rect.size.width, self.rect.size.height));
  1444. //
  1445. //
  1446. // CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  1447. // // CGContextTranslateCTM(context, 0, self.rect.size.height);
  1448. // // CGContextScaleCTM(context, 1.0, -1.0);
  1449. //
  1450. // // draw
  1451. // CTFrameDraw(leftFrame, context);
  1452. //
  1453. //
  1454. // CFRelease(leftFrame);
  1455. // // cleanup
  1456. //
  1457. // CGPathRelease(leftColumnPath);
  1458. // CFRelease(framesetter);
  1459. //
  1460. //
  1461. // // return self.rect;
  1462. // [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  1463. // if(expand&&expand_rect.size.height>parentrect.size.height)
  1464. // return expand_rect;
  1465. // else
  1466. // return parentrect;
  1467. // // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  1468. //
  1469. // // [super restoreFlip:context ContextHeight:flip_height];
  1470. //}
  1471. //
  1472. //
  1473. //@end