TextDrawable.m 71 KB

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