TextDrawable.m 68 KB

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