TextDrawable.m 71 KB

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