YYTextAttributeExample.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // YYTextAttributeExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/8/19.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextAttributeExample.h"
  9. #import "YYText.h"
  10. #import "YYTextExampleHelper.h"
  11. #import "UIImage+YYWebImage.h"
  12. #import "UIView+YYAdd.h"
  13. #import "NSString+YYAdd.h"
  14. @implementation YYTextAttributeExample
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. self.view.backgroundColor = [UIColor whiteColor];
  18. __weak typeof(self) _self = self;
  19. [YYTextExampleHelper addDebugOptionToViewController:self];
  20. NSMutableAttributedString *text = [NSMutableAttributedString new];
  21. {
  22. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Shadow"];
  23. one.yy_font = [UIFont boldSystemFontOfSize:30];
  24. one.yy_color = [UIColor whiteColor];
  25. YYTextShadow *shadow = [YYTextShadow new];
  26. shadow.color = [UIColor colorWithWhite:0.000 alpha:0.490];
  27. shadow.offset = CGSizeMake(0, 1);
  28. shadow.radius = 5;
  29. one.yy_textShadow = shadow;
  30. [text appendAttributedString:one];
  31. [text appendAttributedString:[self padding]];
  32. }
  33. {
  34. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Inner Shadow"];
  35. one.yy_font = [UIFont boldSystemFontOfSize:30];
  36. one.yy_color = [UIColor whiteColor];
  37. YYTextShadow *shadow = [YYTextShadow new];
  38. shadow.color = [UIColor colorWithWhite:0.000 alpha:0.40];
  39. shadow.offset = CGSizeMake(0, 1);
  40. shadow.radius = 1;
  41. one.yy_textInnerShadow = shadow;
  42. [text appendAttributedString:one];
  43. [text appendAttributedString:[self padding]];
  44. }
  45. {
  46. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Multiple Shadows"];
  47. one.yy_font = [UIFont boldSystemFontOfSize:30];
  48. one.yy_color = [UIColor colorWithRed:1.000 green:0.795 blue:0.014 alpha:1.000];
  49. YYTextShadow *shadow = [YYTextShadow new];
  50. shadow.color = [UIColor colorWithWhite:0.000 alpha:0.20];
  51. shadow.offset = CGSizeMake(0, -1);
  52. shadow.radius = 1.5;
  53. YYTextShadow *subShadow = [YYTextShadow new];
  54. subShadow.color = [UIColor colorWithWhite:1 alpha:0.99];
  55. subShadow.offset = CGSizeMake(0, 1);
  56. subShadow.radius = 1.5;
  57. shadow.subShadow = subShadow;
  58. one.yy_textShadow = shadow;
  59. YYTextShadow *innerShadow = [YYTextShadow new];
  60. innerShadow.color = [UIColor colorWithRed:0.851 green:0.311 blue:0.000 alpha:0.780];
  61. innerShadow.offset = CGSizeMake(0, 1);
  62. innerShadow.radius = 1;
  63. one.yy_textInnerShadow = innerShadow;
  64. [text appendAttributedString:one];
  65. [text appendAttributedString:[self padding]];
  66. }
  67. {
  68. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Background Image"];
  69. one.yy_font = [UIFont boldSystemFontOfSize:30];
  70. one.yy_color = [UIColor colorWithRed:1.000 green:0.795 blue:0.014 alpha:1.000];
  71. CGSize size = CGSizeMake(20, 20);
  72. UIImage *background = [UIImage yy_imageWithSize:size drawBlock:^(CGContextRef context) {
  73. UIColor *c0 = [UIColor colorWithRed:0.054 green:0.879 blue:0.000 alpha:1.000];
  74. UIColor *c1 = [UIColor colorWithRed:0.869 green:1.000 blue:0.030 alpha:1.000];
  75. [c0 setFill];
  76. CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
  77. [c1 setStroke];
  78. CGContextSetLineWidth(context, 2);
  79. for (int i = 0; i < size.width * 2; i+= 4) {
  80. CGContextMoveToPoint(context, i, -2);
  81. CGContextAddLineToPoint(context, i - size.height, size.height + 2);
  82. }
  83. CGContextStrokePath(context);
  84. }];
  85. one.yy_color = [UIColor colorWithPatternImage:background];
  86. [text appendAttributedString:one];
  87. [text appendAttributedString:[self padding]];
  88. }
  89. {
  90. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Border"];
  91. one.yy_font = [UIFont boldSystemFontOfSize:30];
  92. one.yy_color = [UIColor colorWithRed:1.000 green:0.029 blue:0.651 alpha:1.000];
  93. YYTextBorder *border = [YYTextBorder new];
  94. border.strokeColor = [UIColor colorWithRed:1.000 green:0.029 blue:0.651 alpha:1.000];
  95. border.strokeWidth = 3;
  96. border.lineStyle = YYTextLineStylePatternCircleDot;
  97. border.cornerRadius = 3;
  98. border.insets = UIEdgeInsetsMake(0, -4, 0, -4);
  99. one.yy_textBackgroundBorder = border;
  100. [text appendAttributedString:[self padding]];
  101. [text appendAttributedString:one];
  102. [text appendAttributedString:[self padding]];
  103. [text appendAttributedString:[self padding]];
  104. [text appendAttributedString:[self padding]];
  105. [text appendAttributedString:[self padding]];
  106. }
  107. {
  108. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Link"];
  109. one.yy_font = [UIFont boldSystemFontOfSize:30];
  110. one.yy_underlineColor = one.yy_color;
  111. one.yy_underlineStyle = NSUnderlineStyleSingle;
  112. /// 1. you can set a highlight with these code
  113. /*
  114. one.yy_color = [UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000];
  115. YYTextBorder *border = [YYTextBorder new];
  116. border.cornerRadius = 3;
  117. border.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
  118. border.fillColor = [UIColor colorWithWhite:0.000 alpha:0.220];
  119. YYTextHighlight *highlight = [YYTextHighlight new];
  120. [highlight setBorder:border];
  121. highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  122. [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
  123. };
  124. [one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
  125. */
  126. /// 2. or you can use the convenience method
  127. [one yy_setTextHighlightRange:one.yy_rangeOfAll
  128. color:[UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000]
  129. backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]
  130. tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
  131. [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
  132. }];
  133. [text appendAttributedString:one];
  134. [text appendAttributedString:[self padding]];
  135. }
  136. {
  137. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Another Link"];
  138. one.yy_font = [UIFont boldSystemFontOfSize:30];
  139. one.yy_color = [UIColor redColor];
  140. YYTextBorder *border = [YYTextBorder new];
  141. border.cornerRadius = 50;
  142. border.insets = UIEdgeInsetsMake(0, -10, 0, -10);
  143. border.strokeWidth = 0.5;
  144. border.strokeColor = one.yy_color;
  145. border.lineStyle = YYTextLineStyleSingle;
  146. one.yy_textBackgroundBorder = border;
  147. YYTextBorder *highlightBorder = border.copy;
  148. highlightBorder.strokeWidth = 0;
  149. highlightBorder.strokeColor = one.yy_color;
  150. highlightBorder.fillColor = one.yy_color;
  151. YYTextHighlight *highlight = [YYTextHighlight new];
  152. [highlight setColor:[UIColor whiteColor]];
  153. [highlight setBackgroundBorder:highlightBorder];
  154. highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  155. [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
  156. };
  157. [one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
  158. [text appendAttributedString:one];
  159. [text appendAttributedString:[self padding]];
  160. }
  161. {
  162. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Yet Another Link"];
  163. one.yy_font = [UIFont boldSystemFontOfSize:30];
  164. one.yy_color = [UIColor whiteColor];
  165. YYTextShadow *shadow = [YYTextShadow new];
  166. shadow.color = [UIColor colorWithWhite:0.000 alpha:0.490];
  167. shadow.offset = CGSizeMake(0, 1);
  168. shadow.radius = 5;
  169. one.yy_textShadow = shadow;
  170. YYTextShadow *shadow0 = [YYTextShadow new];
  171. shadow0.color = [UIColor colorWithWhite:0.000 alpha:0.20];
  172. shadow0.offset = CGSizeMake(0, -1);
  173. shadow0.radius = 1.5;
  174. YYTextShadow *shadow1 = [YYTextShadow new];
  175. shadow1.color = [UIColor colorWithWhite:1 alpha:0.99];
  176. shadow1.offset = CGSizeMake(0, 1);
  177. shadow1.radius = 1.5;
  178. shadow0.subShadow = shadow1;
  179. YYTextShadow *innerShadow0 = [YYTextShadow new];
  180. innerShadow0.color = [UIColor colorWithRed:0.851 green:0.311 blue:0.000 alpha:0.780];
  181. innerShadow0.offset = CGSizeMake(0, 1);
  182. innerShadow0.radius = 1;
  183. YYTextHighlight *highlight = [YYTextHighlight new];
  184. [highlight setColor:[UIColor colorWithRed:1.000 green:0.795 blue:0.014 alpha:1.000]];
  185. [highlight setShadow:shadow0];
  186. [highlight setInnerShadow:innerShadow0];
  187. [one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
  188. [text appendAttributedString:one];
  189. }
  190. YYLabel *label = [YYLabel new];
  191. label.attributedText = text;
  192. label.width = self.view.width;
  193. label.height = self.view.height - (kiOS7Later ? 64 : 44);
  194. label.top = (kiOS7Later ? 64 : 0);
  195. label.textAlignment = NSTextAlignmentCenter;
  196. label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
  197. label.numberOfLines = 0;
  198. label.backgroundColor = [UIColor colorWithWhite:0.933 alpha:1.000];
  199. [self.view addSubview:label];
  200. /*
  201. If the 'highlight.tapAction' is not nil, the label will invoke 'highlight.tapAction'
  202. and ignore 'label.highlightTapAction'.
  203. If the 'highlight.tapAction' is nil, you can use 'highlightTapAction' to handle
  204. all tap action in this label.
  205. */
  206. label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  207. [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
  208. };
  209. }
  210. - (NSAttributedString *)padding {
  211. NSMutableAttributedString *pad = [[NSMutableAttributedString alloc] initWithString:@"\n\n"];
  212. pad.yy_font = [UIFont systemFontOfSize:4];
  213. return pad;
  214. }
  215. - (void)showMessage:(NSString *)msg {
  216. CGFloat padding = 10;
  217. YYLabel *label = [YYLabel new];
  218. label.text = msg;
  219. label.font = [UIFont systemFontOfSize:16];
  220. label.textAlignment = NSTextAlignmentCenter;
  221. label.textColor = [UIColor whiteColor];
  222. label.backgroundColor = [UIColor colorWithRed:0.033 green:0.685 blue:0.978 alpha:0.730];
  223. label.width = self.view.width;
  224. label.textContainerInset = UIEdgeInsetsMake(padding, padding, padding, padding);
  225. label.height = [msg heightForFont:label.font width:label.width] + 2 * padding;
  226. label.bottom = (kiOS7Later ? 64 : 0);
  227. [self.view addSubview:label];
  228. [UIView animateWithDuration:0.3 animations:^{
  229. label.top = (kiOS7Later ? 64 : 0);
  230. } completion:^(BOOL finished) {
  231. [UIView animateWithDuration:0.2 delay:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  232. label.bottom = (kiOS7Later ? 64 : 0);
  233. } completion:^(BOOL finished) {
  234. [label removeFromSuperview];
  235. }];
  236. }];
  237. }
  238. @end