StampPreview.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. //
  2. // StampPreview.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "StampPreview.h"
  11. static float c01, c02, c03, c11, c12, c13;
  12. #define kStampPreview_OnlyText_Size 48.0
  13. #define kStampPreview_Text_Size 30.0
  14. #define kStampPreview_Date_Size 20.0
  15. @implementation StampPreview
  16. @synthesize textStampText = _textStampText;
  17. @synthesize textStampStyle = _textStampStyle;
  18. @synthesize textStampHaveDate = _textStampHaveDate;
  19. @synthesize textStampHaveTime = _textStampHaveTime;
  20. @synthesize leftMargin = _leftMargin;
  21. - (id)initWithFrame:(CGRect)frame
  22. {
  23. self = [super initWithFrame:frame];
  24. if (self)
  25. {
  26. self.backgroundColor = [UIColor clearColor];
  27. _scale = ([[UIScreen mainScreen] scale] == 2.0) ? 2.0f : 1.0f;
  28. }
  29. return self;
  30. }
  31. - (void)dealloc
  32. {
  33. [_textStampText release];
  34. [_dateTime release];
  35. [super dealloc];
  36. }
  37. - (void)setTextStampStyle:(NSInteger)style
  38. {
  39. _textStampStyle = style;
  40. if (TextStampColorType_Red == style ||
  41. TextStampColorType_RedLeft == style ||
  42. TextStampColorType_RedRight == style) {
  43. color[0] = 0.57;
  44. color[1] = 0.06;
  45. color[2] = 0.02;
  46. }
  47. else if (TextStampColorType_Green == style ||
  48. TextStampColorType_GreenLeft == style ||
  49. TextStampColorType_GreenRight == style)
  50. {
  51. color[0] = 0.25;
  52. color[1] = 0.42;
  53. color[2] = 0.13;
  54. }
  55. else if (TextStampColorType_Blue == style ||
  56. TextStampColorType_BlueLeft == style ||
  57. TextStampColorType_BlueRight == style)
  58. {
  59. color[0] = 0.09;
  60. color[1] = 0.15;
  61. color[2] = 0.39;
  62. }
  63. else if (TextStampColorType_Black == style)
  64. {
  65. color[0] = 0;
  66. color[1] = 0;
  67. color[2] = 0;
  68. }
  69. }
  70. - (NSString *)getDateTime
  71. {
  72. NSTimeZone* timename = [NSTimeZone systemTimeZone];
  73. NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init ];
  74. [outputFormatter setTimeZone:timename ];
  75. NSString *tDate = nil;
  76. if (_textStampHaveDate && !_textStampHaveTime)
  77. {
  78. tDate = [NSDateFormatter localizedStringFromDate:[NSDate date]
  79. dateStyle:NSDateFormatterShortStyle
  80. timeStyle:NSDateFormatterNoStyle];
  81. }
  82. else if (_textStampHaveTime && !_textStampHaveDate)
  83. {
  84. [outputFormatter setDateFormat:@"HH:mm:ss"];
  85. tDate = [[outputFormatter stringFromDate:[NSDate date]] retain];
  86. }
  87. else if (_textStampHaveDate && _textStampHaveTime)
  88. {
  89. [outputFormatter setDateFormat:@" HH:mm"];
  90. tDate = [NSDateFormatter localizedStringFromDate:[NSDate date]
  91. dateStyle:NSDateFormatterShortStyle
  92. timeStyle:NSDateFormatterNoStyle];
  93. tDate = [tDate stringByAppendingString:[outputFormatter stringFromDate:[NSDate date]]];
  94. }
  95. [outputFormatter release];
  96. return tDate;
  97. }
  98. //绘制的填充效果
  99. static void MyShaderProcedure(void *info, const CGFloat *in, CGFloat *out)
  100. {
  101. CGFloat v;
  102. size_t k, components;
  103. components = (size_t)info;
  104. v = *in;
  105. for (k = 0; k < components -1; k++)
  106. {
  107. if (0 == k) {
  108. *out++ = c01 + v * (c11-c01);
  109. }
  110. else if (1 == k)
  111. {
  112. *out++ = c02 + v * (c12-c02);
  113. }
  114. else if (2 == k) {
  115. *out++ = c03 + v * (c13-c03);
  116. }
  117. }
  118. *out++ = 0.85;
  119. }
  120. //根据文字计算Rect
  121. - (void)fitStampRect
  122. {
  123. UIFont *tTextFont = nil;
  124. UIFont *tTimeFont = nil;
  125. NSString *drawText = nil;
  126. NSString *dateText = nil;
  127. if (self.textStampText.length < 1 && !self.dateTime) {
  128. drawText = @"StampText";
  129. tTextFont = [UIFont fontWithName:@"Helvetica" size:kStampPreview_OnlyText_Size];
  130. } else if (self.textStampText.length > 0 && self.dateTime.length > 0) {
  131. tTextFont = [UIFont fontWithName:@"Helvetica" size:kStampPreview_Text_Size];
  132. tTimeFont = [UIFont fontWithName:@"Helvetica" size:kStampPreview_Date_Size];
  133. drawText = self.textStampText;
  134. dateText = self.dateTime;
  135. } else {
  136. if (self.dateTime) {
  137. drawText = self.dateTime;
  138. } else {
  139. drawText = self.textStampText;
  140. }
  141. tTextFont = [UIFont fontWithName:@"Helvetica" size:kStampPreview_OnlyText_Size];
  142. }
  143. CGSize tTextSize = [drawText sizeWithAttributes:@{ NSFontAttributeName : tTextFont }];
  144. CGSize tTimeSize = CGSizeZero;
  145. if (tTimeFont) {
  146. tTimeSize = [dateText sizeWithAttributes:@{ NSFontAttributeName : tTimeFont }];
  147. }
  148. int w = tTextSize.width > tTimeSize.width ? tTextSize.width : tTimeSize.width;
  149. int count = 0;
  150. for (int i = 0; i < drawText.length; ++i) {
  151. NSRange range = NSMakeRange(i,1);
  152. NSString *aStr = [drawText substringWithRange:range];
  153. if ([aStr isEqualToString:@" "]) {
  154. ++count;
  155. }
  156. }
  157. if (tTextSize.width < tTimeSize.width) {
  158. w += 15;
  159. } else {
  160. w += 13 + 5 * count;
  161. }
  162. int h = tTextSize.height + 5;
  163. if (dateText) {
  164. h = tTextSize.height + tTimeSize.height + 8.011;
  165. }
  166. if (_textStampStyle == TextStampColorType_BlueLeft ||
  167. _textStampStyle == TextStampColorType_GreenLeft ||
  168. _textStampStyle == TextStampColorType_RedLeft) {
  169. w = w + h * 0.618033;
  170. } else if (_textStampStyle == TextStampColorType_BlueRight ||
  171. _textStampStyle == TextStampColorType_GreenRight ||
  172. _textStampStyle == TextStampColorType_RedRight) {
  173. w = w + h * 0.618033;
  174. }
  175. float x = 0.0;
  176. float y = 0.0;
  177. _scale = 1.0;
  178. CGFloat maxW = 300 - _leftMargin;
  179. if (w > maxW ) {
  180. _scale = maxW/w;
  181. h = h*_scale;
  182. x = self.frame.size.width/2.0 - maxW/2.0;
  183. y = self.frame.size.height/2.0 - h/2.0;
  184. _stampBounds = CGRectMake(x + _leftMargin, y, maxW, h);
  185. } else {
  186. x = self.frame.size.width/2.0 - w/2.0;
  187. y = self.frame.size.height/2.0 - h/2.0;
  188. _stampBounds = CGRectMake(x + _leftMargin, y, w, h);
  189. }
  190. }
  191. // Only override drawRect: if you perform custom drawing.
  192. // An empty implementation adversely affects performance during animation.
  193. - (void)drawRect:(CGRect)rect
  194. {
  195. self.dateTime = [self getDateTime];
  196. [self fitStampRect];
  197. CGContextRef context = UIGraphicsGetCurrentContext();
  198. //绘制边框背景
  199. if (TextStampColorType_Black != _textStampStyle) {
  200. [self drawBounder:context];
  201. }
  202. //绘制自定义文字
  203. [self drawText:context];
  204. }
  205. - (void)drawBounder:(CGContextRef)context
  206. {
  207. if (!context) {
  208. return;
  209. }
  210. CGContextSaveGState(context);
  211. const CGFunctionCallbacks callbacks = {
  212. .version = 0, .evaluate = &MyShaderProcedure, .releaseInfo = NULL
  213. };
  214. c01 = c02 = c03 = c11 = c12 = c13 = 1.0;
  215. if (color[0] > color[1] && color[0] > color[2]) {
  216. c01 = 1.0;
  217. c02 = 0.78;
  218. c03 = 0.78;
  219. c11 = 0.98;
  220. c12 = 0.92;
  221. c13 = 0.91;
  222. }
  223. else if (color[1] > color[0] && color[1] > color[2]) {
  224. c01 = 0.81;
  225. c02 = 0.88;
  226. c03 = 0.78;
  227. c11 = 0.95;
  228. c12 = 0.95;
  229. c13 = 0.95;
  230. }
  231. else if (color[2] > color[0] && color[2] > color[1])
  232. {
  233. c01 = 0.79;
  234. c02 = 0.81;
  235. c03 = 0.89;
  236. c11 = 0.90;
  237. c12 = 0.95;
  238. c13 = 1.0;
  239. }
  240. // 创建函数对象
  241. size_t components = 1 + CGColorSpaceGetNumberOfComponents(CGColorSpaceCreateDeviceRGB());
  242. CGFunctionRef funcRef = CGFunctionCreate((void *) components, // 将info置空
  243. 1, // 1个输入元素(每个元素为2个分量来表示区间)
  244. (CGFloat[]){0.0f, 1.0f},
  245. 4, // 4个输出元素
  246. //(CGFloat[]){0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f},
  247. (CGFloat[]){c01, c11, c02, c12, c03, c13, 0.0f, 1.0f},
  248. &callbacks);
  249. // 创建色彩空间对象
  250. CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
  251. // 创建着色器对象
  252. CGShadingRef shadingRef = CGShadingCreateAxial(colorSpaceRef,
  253. CGPointMake(_stampBounds.origin.x+_stampBounds.size.width*0.75, _stampBounds.origin.y+_stampBounds.size.width*0.25), // 起点坐标
  254. CGPointMake(_stampBounds.origin.x+_stampBounds.size.width*0.25, _stampBounds.origin.y+_stampBounds.size.width*0.75), // 终点坐标
  255. funcRef, true, true);
  256. // 释放色彩空间对象
  257. CGColorSpaceRelease(colorSpaceRef);
  258. // 释放函数对象
  259. CGFunctionRelease(funcRef);
  260. if (_textStampStyle == TextStampColorType_Black ||
  261. _textStampStyle == TextStampColorType_Blue ||
  262. _textStampStyle == TextStampColorType_Green ||
  263. _textStampStyle == TextStampColorType_Red) {
  264. [self drawNormalRect:context];
  265. } else if (_textStampStyle == TextStampColorType_BlueLeft ||
  266. _textStampStyle == TextStampColorType_GreenLeft ||
  267. _textStampStyle == TextStampColorType_RedLeft) {
  268. [self drawLeftBounder:context];
  269. } else if (_textStampStyle == TextStampColorType_BlueRight ||
  270. _textStampStyle == TextStampColorType_GreenRight ||
  271. _textStampStyle == TextStampColorType_RedRight) {
  272. [self drawRightBounder:context];
  273. }
  274. CGContextClip (context);
  275. // 绘制着色渐变
  276. CGContextSaveGState(context);
  277. CGContextDrawShading(context, shadingRef);
  278. CGContextRestoreGState(context);
  279. // 释放着色器对象
  280. CGShadingRelease(shadingRef);
  281. if (_textStampStyle == TextStampColorType_Black ||
  282. _textStampStyle == TextStampColorType_Blue ||
  283. _textStampStyle == TextStampColorType_Green ||
  284. _textStampStyle == TextStampColorType_Red) {
  285. [self drawNormalRect:context];
  286. } else if (_textStampStyle == TextStampColorType_BlueLeft ||
  287. _textStampStyle == TextStampColorType_GreenLeft ||
  288. _textStampStyle == TextStampColorType_RedLeft) {
  289. [self drawLeftBounder:context];
  290. } else if (_textStampStyle == TextStampColorType_BlueRight ||
  291. _textStampStyle == TextStampColorType_GreenRight ||
  292. _textStampStyle == TextStampColorType_RedRight) {
  293. [self drawRightBounder:context];
  294. }
  295. CGContextStrokePath(context);
  296. }
  297. - (void)drawNormalRect:(CGContextRef)context
  298. {
  299. float tmpHeight = 11.0 * _stampBounds.size.height/50;
  300. float tmpWidth = 3.0 * _stampBounds.size.height/50;
  301. float hw1 = 5.54492 * _stampBounds.size.height/50;
  302. float hw2 = 4.40039 * _stampBounds.size.height/50;
  303. CGContextBeginPath (context);
  304. CGContextMoveToPoint(context, _stampBounds.origin.x+tmpWidth, _stampBounds.origin.y+_stampBounds.size.height-tmpHeight);
  305. CGContextAddCurveToPoint(context, _stampBounds.origin.x+tmpWidth, _stampBounds.origin.y+_stampBounds.size.height-tmpHeight+hw2, _stampBounds.origin.x+tmpHeight-hw1, _stampBounds.origin.y+_stampBounds.size.height-tmpWidth, _stampBounds.origin.x+tmpHeight, _stampBounds.origin.y+_stampBounds.size.height-tmpWidth);
  306. CGContextAddLineToPoint(context, _stampBounds.origin.x+_stampBounds.size.width-tmpHeight, _stampBounds.origin.y+_stampBounds.size.height-tmpWidth);
  307. CGContextAddCurveToPoint(context, _stampBounds.origin.x+_stampBounds.size.width-tmpHeight+hw1, _stampBounds.origin.y+_stampBounds.size.height-tmpWidth, _stampBounds.origin.x+_stampBounds.size.width-tmpWidth, _stampBounds.origin.y+_stampBounds.size.height-tmpHeight+hw2, _stampBounds.origin.x+_stampBounds.size.width-tmpWidth, _stampBounds.origin.y+_stampBounds.size.height-tmpHeight);
  308. CGContextAddLineToPoint(context, _stampBounds.origin.x+_stampBounds.size.width-tmpWidth, _stampBounds.origin.y+tmpHeight);
  309. CGContextAddCurveToPoint(context, _stampBounds.origin.x+_stampBounds.size.width-tmpWidth, _stampBounds.origin.y+tmpHeight-hw2, _stampBounds.origin.x+_stampBounds.size.width-tmpHeight+hw1, _stampBounds.origin.y+tmpWidth, _stampBounds.origin.x+_stampBounds.size.width-tmpHeight, _stampBounds.origin.y+tmpWidth);
  310. CGContextAddLineToPoint(context,_stampBounds.origin.x+tmpHeight, _stampBounds.origin.y+tmpWidth);
  311. CGContextAddCurveToPoint(context, _stampBounds.origin.x+tmpHeight-hw1, _stampBounds.origin.y+tmpWidth, _stampBounds.origin.x+tmpWidth, _stampBounds.origin.y+tmpHeight-hw2, _stampBounds.origin.x+tmpWidth, _stampBounds.origin.y+tmpHeight);
  312. CGContextAddLineToPoint(context,_stampBounds.origin.x+tmpWidth, _stampBounds.origin.y+_stampBounds.size.height-tmpHeight);
  313. CGContextClosePath (context);
  314. }
  315. - (void)drawLeftBounder:(CGContextRef)context
  316. {
  317. float tmpHeight = 11.0 * _stampBounds.size.height/50;
  318. float tmpWidth = 3.0 * _stampBounds.size.height/50;
  319. float hw1 = 5.54492 * _stampBounds.size.height/50;
  320. float hw2 = 4.40039 * _stampBounds.size.height/50;
  321. float x0 = _stampBounds.origin.x + _stampBounds.size.height * 0.618033;
  322. float y0 = _stampBounds.origin.y;
  323. float x1 = _stampBounds.origin.x + _stampBounds.size.width;
  324. float y1 = _stampBounds.origin.y + _stampBounds.size.height;
  325. float xp = _stampBounds.origin.x;
  326. float yp = _stampBounds.origin.y + _stampBounds.size.height / 2.0;
  327. CGContextBeginPath (context);
  328. CGContextMoveToPoint(context, x0 + tmpHeight, y1 - tmpWidth);
  329. CGContextAddLineToPoint(context, x1-tmpHeight, y1-tmpWidth);
  330. CGContextAddCurveToPoint(context, x1-tmpHeight+hw1, y1-tmpWidth, x1-tmpWidth, y1-tmpHeight+hw2, x1-tmpWidth, y1-tmpHeight);
  331. CGContextAddLineToPoint(context, x1-tmpWidth, y0+tmpHeight);
  332. CGContextAddCurveToPoint(context, x1-tmpWidth, y0+tmpHeight-hw2, x1-tmpHeight+hw1, y0+tmpWidth, x1-tmpHeight, y0+tmpWidth);
  333. CGContextAddLineToPoint(context, x0+tmpHeight, y0+tmpWidth);
  334. CGContextAddLineToPoint(context, xp+tmpHeight, yp);
  335. CGContextAddLineToPoint(context, x0+tmpHeight, y1-tmpWidth);
  336. CGContextClosePath (context);
  337. }
  338. - (void)drawRightBounder:(CGContextRef)context
  339. {
  340. float tmpHeight = 11.0 * _stampBounds.size.height/50;
  341. float tmpWidth = 3.0 * _stampBounds.size.height/50;
  342. float hw1 = 5.54492 * _stampBounds.size.height/50;
  343. float hw2 = 4.40039 * _stampBounds.size.height/50;
  344. float x0 = _stampBounds.origin.x;
  345. float y0 = _stampBounds.origin.y;
  346. float x1 = _stampBounds.origin.x + _stampBounds.size.width - _stampBounds.size.height * 0.618033;
  347. float y1 = _stampBounds.origin.y + _stampBounds.size.height;
  348. float xp = _stampBounds.origin.x + _stampBounds.size.width;
  349. float yp = _stampBounds.origin.y + _stampBounds.size.height / 2.0;
  350. CGContextBeginPath (context);
  351. CGContextMoveToPoint(context, x0 + tmpWidth, y1 - tmpHeight);
  352. CGContextAddCurveToPoint(context, x0+tmpWidth, y1-tmpHeight+hw2, x0+tmpHeight-hw1, y1-tmpWidth, x0+tmpHeight, y1-tmpWidth);
  353. CGContextAddLineToPoint(context, x1-tmpHeight, y1-tmpWidth);
  354. CGContextAddLineToPoint(context, xp-tmpHeight, yp);
  355. CGContextAddLineToPoint(context, x1-tmpHeight, y0+tmpWidth);
  356. CGContextAddLineToPoint(context, x0+tmpHeight, y0+tmpWidth);
  357. CGContextAddCurveToPoint(context, x0+tmpHeight-hw1, y0+tmpWidth, x0+tmpWidth, y0+tmpHeight-hw2, x0+tmpWidth, y0+tmpHeight);
  358. CGContextAddLineToPoint(context, x0+tmpWidth, y1-tmpHeight);
  359. CGContextClosePath (context);
  360. }
  361. - (void)drawText:(CGContextRef)context
  362. {
  363. if (!context) {
  364. return;
  365. }
  366. CGContextSetRGBFillColor(context, color[0], color[1], color[2], 1.0);
  367. CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
  368. NSString *drawText = nil;
  369. NSString *dateText = nil;
  370. if (self.textStampText.length < 1 && !self.dateTime) {
  371. drawText = @"StampText";
  372. } else if (self.textStampText.length > 0 && self.dateTime.length > 0) {
  373. drawText = self.textStampText;
  374. dateText = self.dateTime;
  375. } else {
  376. if (self.dateTime) {
  377. drawText = self.dateTime;
  378. } else {
  379. drawText = self.textStampText;
  380. }
  381. }
  382. if (!dateText) {
  383. float fontsize = kStampPreview_OnlyText_Size*_scale;
  384. UIFont* font = [UIFont fontWithName:@"Helvetica" size:fontsize];
  385. CGRect rt = CGRectInset(_stampBounds, 0, 0);
  386. rt.origin.x += 8.093 * _scale;
  387. if (_textStampStyle == TextStampColorType_BlueLeft ||
  388. _textStampStyle == TextStampColorType_GreenLeft ||
  389. _textStampStyle == TextStampColorType_RedLeft) {
  390. rt.origin.x += rt.size.height * 0.618033;
  391. }
  392. UIGraphicsPushContext(context);
  393. [[UIColor colorWithRed:color[0] green:color[1] blue:color[2] alpha:1] set];
  394. [drawText drawInRect:rt withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
  395. UIGraphicsPopContext();
  396. } else {
  397. float tFontSize = kStampPreview_Text_Size*_scale;
  398. UIFont *tFont = [UIFont fontWithName:@"Helvetica" size:tFontSize];
  399. CGRect tTextRT = CGRectInset(_stampBounds, 0, 0);
  400. tTextRT.origin.x += 8.093 * _scale;
  401. if (_textStampStyle == TextStampColorType_BlueLeft ||
  402. _textStampStyle == TextStampColorType_GreenLeft ||
  403. _textStampStyle == TextStampColorType_RedLeft) {
  404. tTextRT.origin.x += tTextRT.size.height * 0.618033;
  405. }
  406. UIGraphicsPushContext(context);
  407. [[UIColor colorWithRed:color[0] green:color[1] blue:color[2] alpha:1] set];
  408. if (drawText.length > 0) {
  409. [drawText drawInRect:tTextRT withFont:tFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
  410. CGRect tDateRT = CGRectInset(_stampBounds, 0, 0);
  411. tDateRT.origin.x += 8.093 * _scale;
  412. if (_textStampStyle == TextStampColorType_BlueLeft ||
  413. _textStampStyle == TextStampColorType_GreenLeft ||
  414. _textStampStyle == TextStampColorType_RedLeft) {
  415. tDateRT.origin.x += tDateRT.size.height * 0.618033;
  416. }
  417. tDateRT.origin.y = tDateRT.origin.y + tFontSize + 6.103*_scale;
  418. tFontSize = kStampPreview_Date_Size;
  419. tFont = [UIFont fontWithName:@"Helvetica" size:tFontSize];
  420. [[UIColor colorWithRed:color[0] green:color[1] blue:color[2] alpha:1] set];
  421. [dateText drawInRect:tDateRT withFont:tFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
  422. } else {
  423. float fontsize = kStampPreview_Date_Size*_scale;
  424. UIFont* font = [UIFont fontWithName:@"Helvetica" size:fontsize];
  425. CGRect rt = CGRectInset(_stampBounds, 0, 0);
  426. rt.origin.x += 8.093 * _scale;
  427. if (_textStampStyle == TextStampColorType_BlueLeft ||
  428. _textStampStyle == TextStampColorType_GreenLeft ||
  429. _textStampStyle == TextStampColorType_RedLeft) {
  430. rt.origin.x += rt.size.height * 0.618033;
  431. }
  432. [[UIColor colorWithRed:color[0] green:color[1] blue:color[2] alpha:1] set];
  433. [dateText drawInRect:rt withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
  434. }
  435. UIGraphicsPopContext();
  436. }
  437. }
  438. - (UIImage *)renderImage
  439. {
  440. UIImage *image = [self renderImageFromView:self];
  441. return image;
  442. }
  443. /* 将UIView转换成UIImage */
  444. - (UIImage *)renderImageFromView:(UIView *)view
  445. {
  446. UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);
  447. CGContextRef context=UIGraphicsGetCurrentContext();
  448. CGContextTranslateCTM(context, 1.0, 1.0);
  449. [view.layer renderInContext:context];
  450. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  451. UIGraphicsEndImageContext();
  452. return image;
  453. }
  454. @end