YYTextAttribute.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. //
  2. // YYTextAttribute.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/26.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "YYTextAttribute.h"
  12. #import <UIKit/UIKit.h>
  13. #import <CoreText/CoreText.h>
  14. #import "NSObject+YYAdd.h"
  15. #import "NSAttributedString+YYText.h"
  16. #import "YYAnimatedImageView.h"
  17. #import "YYTextArchiver.h"
  18. #import "UIFont+YYAdd.h"
  19. #import "UIDevice+YYAdd.h"
  20. NSString *const YYTextBackedStringAttributeName = @"YYTextBackedString";
  21. NSString *const YYTextBindingAttributeName = @"YYTextBinding";
  22. NSString *const YYTextShadowAttributeName = @"YYTextShadow";
  23. NSString *const YYTextInnerShadowAttributeName = @"YYTextInnerShadow";
  24. NSString *const YYTextUnderlineAttributeName = @"YYTextUnderline";
  25. NSString *const YYTextStrikethroughAttributeName = @"YYTextStrikethrough";
  26. NSString *const YYTextBorderAttributeName = @"YYTextBorder";
  27. NSString *const YYTextBackgroundBorderAttributeName = @"YYTextBackgroundBorder";
  28. NSString *const YYTextBlockBorderAttributeName = @"YYTextBlockBorder";
  29. NSString *const YYTextAttachmentAttributeName = @"YYTextAttachment";
  30. NSString *const YYTextHighlightAttributeName = @"YYTextHighlight";
  31. NSString *const YYTextGlyphTransformAttributeName = @"YYTextGlyphTransform";
  32. NSString *const YYTextAttachmentToken = @"\uFFFC";
  33. NSString *const YYTextTruncationToken = @"\u2026";
  34. YYTextAttributeType YYTextAttributeGetType(NSString *name){
  35. if (name.length == 0) return YYTextAttributeTypeNone;
  36. static NSMutableDictionary *dic;
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. dic = [NSMutableDictionary new];
  40. NSNumber *All = @(YYTextAttributeTypeUIKit | YYTextAttributeTypeCoreText | YYTextAttributeTypeYYText);
  41. NSNumber *CoreText_YYText = @(YYTextAttributeTypeCoreText | YYTextAttributeTypeYYText);
  42. NSNumber *UIKit_YYText = @(YYTextAttributeTypeUIKit | YYTextAttributeTypeYYText);
  43. NSNumber *UIKit_CoreText = @(YYTextAttributeTypeUIKit | YYTextAttributeTypeCoreText);
  44. NSNumber *UIKit = @(YYTextAttributeTypeUIKit);
  45. NSNumber *CoreText = @(YYTextAttributeTypeCoreText);
  46. NSNumber *YYText = @(YYTextAttributeTypeYYText);
  47. dic[NSFontAttributeName] = All;
  48. dic[NSKernAttributeName] = All;
  49. dic[NSForegroundColorAttributeName] = UIKit;
  50. dic[(id)kCTForegroundColorAttributeName] = CoreText;
  51. dic[(id)kCTForegroundColorFromContextAttributeName] = CoreText;
  52. dic[NSBackgroundColorAttributeName] = UIKit;
  53. dic[NSStrokeWidthAttributeName] = All;
  54. dic[NSStrokeColorAttributeName] = UIKit;
  55. dic[(id)kCTStrokeColorAttributeName] = CoreText_YYText;
  56. dic[NSShadowAttributeName] = UIKit_YYText;
  57. dic[NSStrikethroughStyleAttributeName] = UIKit;
  58. dic[NSUnderlineStyleAttributeName] = UIKit_CoreText;
  59. dic[(id)kCTUnderlineColorAttributeName] = CoreText;
  60. dic[NSLigatureAttributeName] = All;
  61. dic[(id)kCTSuperscriptAttributeName] = UIKit; //it's a CoreText attrubite, but only supported by UIKit...
  62. dic[NSVerticalGlyphFormAttributeName] = All;
  63. dic[(id)kCTGlyphInfoAttributeName] = CoreText_YYText;
  64. dic[(id)kCTCharacterShapeAttributeName] = CoreText_YYText;
  65. dic[(id)kCTRunDelegateAttributeName] = CoreText_YYText;
  66. dic[(id)kCTBaselineClassAttributeName] = CoreText_YYText;
  67. dic[(id)kCTBaselineInfoAttributeName] = CoreText_YYText;
  68. dic[(id)kCTBaselineReferenceInfoAttributeName] = CoreText_YYText;
  69. dic[(id)kCTWritingDirectionAttributeName] = CoreText_YYText;
  70. dic[NSParagraphStyleAttributeName] = All;
  71. if (kiOS7Later) {
  72. dic[NSStrikethroughColorAttributeName] = UIKit;
  73. dic[NSUnderlineColorAttributeName] = UIKit;
  74. dic[NSTextEffectAttributeName] = UIKit;
  75. dic[NSObliquenessAttributeName] = UIKit;
  76. dic[NSExpansionAttributeName] = UIKit;
  77. dic[(id)kCTLanguageAttributeName] = CoreText_YYText;
  78. dic[NSBaselineOffsetAttributeName] = UIKit;
  79. dic[NSWritingDirectionAttributeName] = All;
  80. dic[NSAttachmentAttributeName] = UIKit;
  81. dic[NSLinkAttributeName] = UIKit;
  82. }
  83. if (kiOS8Later) {
  84. dic[(id)kCTRubyAnnotationAttributeName] = CoreText;
  85. }
  86. dic[YYTextBackedStringAttributeName] = YYText;
  87. dic[YYTextBindingAttributeName] = YYText;
  88. dic[YYTextShadowAttributeName] = YYText;
  89. dic[YYTextInnerShadowAttributeName] = YYText;
  90. dic[YYTextUnderlineAttributeName] = YYText;
  91. dic[YYTextStrikethroughAttributeName] = YYText;
  92. dic[YYTextBorderAttributeName] = YYText;
  93. dic[YYTextBackgroundBorderAttributeName] = YYText;
  94. dic[YYTextBlockBorderAttributeName] = YYText;
  95. dic[YYTextAttachmentAttributeName] = YYText;
  96. dic[YYTextHighlightAttributeName] = YYText;
  97. dic[YYTextGlyphTransformAttributeName] = YYText;
  98. });
  99. NSNumber *num = dic[name];
  100. if (num != nil) return num.integerValue;
  101. return YYTextAttributeTypeNone;
  102. }
  103. @implementation YYTextBackedString
  104. + (instancetype)stringWithString:(NSString *)string {
  105. YYTextBackedString *one = [self new];
  106. one.string = string;
  107. return one;
  108. }
  109. - (void)encodeWithCoder:(NSCoder *)aCoder {
  110. [aCoder encodeObject:self.string forKey:@"string"];
  111. }
  112. - (id)initWithCoder:(NSCoder *)aDecoder {
  113. self = [super init];
  114. _string = [aDecoder decodeObjectForKey:@"string"];
  115. return self;
  116. }
  117. - (id)copyWithZone:(NSZone *)zone {
  118. typeof(self) one = [self.class new];
  119. one.string = self.string;
  120. return one;
  121. }
  122. @end
  123. @implementation YYTextBinding
  124. + (instancetype)bindingWithDeleteConfirm:(BOOL)deleteConfirm {
  125. YYTextBinding *one = [self new];
  126. one.deleteConfirm = deleteConfirm;
  127. return one;
  128. }
  129. - (void)encodeWithCoder:(NSCoder *)aCoder {
  130. [aCoder encodeObject:@(self.deleteConfirm) forKey:@"deleteConfirm"];
  131. }
  132. - (id)initWithCoder:(NSCoder *)aDecoder {
  133. self = [super init];
  134. _deleteConfirm = ((NSNumber *)[aDecoder decodeObjectForKey:@"deleteConfirm"]).boolValue;
  135. return self;
  136. }
  137. - (id)copyWithZone:(NSZone *)zone {
  138. typeof(self) one = [self.class new];
  139. one.deleteConfirm = self.deleteConfirm;
  140. return one;
  141. }
  142. @end
  143. @implementation YYTextShadow
  144. + (instancetype)shadowWithColor:(UIColor *)color offset:(CGSize)offset radius:(CGFloat)radius {
  145. YYTextShadow *one = [self new];
  146. one.color = color;
  147. one.offset = offset;
  148. one.radius = radius;
  149. return one;
  150. }
  151. + (instancetype)shadowWithNSShadow:(NSShadow *)nsShadow {
  152. if (!nsShadow) return nil;
  153. YYTextShadow *shadow = [self new];
  154. shadow.offset = nsShadow.shadowOffset;
  155. shadow.radius = nsShadow.shadowBlurRadius;
  156. id color = nsShadow.shadowColor;
  157. if (color) {
  158. if (CGColorGetTypeID() == CFGetTypeID((__bridge CFTypeRef)(color))) {
  159. color = [UIColor colorWithCGColor:(__bridge CGColorRef)(color)];
  160. }
  161. if ([color isKindOfClass:[UIColor class]]) {
  162. shadow.color = color;
  163. }
  164. }
  165. return shadow;
  166. }
  167. - (NSShadow *)nsShadow {
  168. NSShadow *shadow = [NSShadow new];
  169. shadow.shadowOffset = self.offset;
  170. shadow.shadowBlurRadius = self.radius;
  171. shadow.shadowColor = self.color;
  172. return shadow;
  173. }
  174. - (void)encodeWithCoder:(NSCoder *)aCoder {
  175. [aCoder encodeObject:self.color forKey:@"color"];
  176. [aCoder encodeObject:@(self.radius) forKey:@"radius"];
  177. [aCoder encodeObject:[NSValue valueWithCGSize:self.offset] forKey:@"offset"];
  178. [aCoder encodeObject:self.subShadow forKey:@"subShadow"];
  179. }
  180. - (id)initWithCoder:(NSCoder *)aDecoder {
  181. self = [super init];
  182. _color = [aDecoder decodeObjectForKey:@"color"];
  183. _radius = ((NSNumber *)[aDecoder decodeObjectForKey:@"radius"]).floatValue;
  184. _offset = ((NSValue *)[aDecoder decodeObjectForKey:@"offset"]).CGSizeValue;
  185. _subShadow = [aDecoder decodeObjectForKey:@"subShadow"];
  186. return self;
  187. }
  188. - (id)copyWithZone:(NSZone *)zone {
  189. typeof(self) one = [self.class new];
  190. one.color = self.color;
  191. one.radius = self.radius;
  192. one.offset = self.offset;
  193. one.subShadow = self.subShadow.copy;
  194. return one;
  195. }
  196. @end
  197. @implementation YYTextDecoration
  198. - (instancetype)init {
  199. self = [super init];
  200. _style = YYTextLineStyleSingle;
  201. return self;
  202. }
  203. + (instancetype)decorationWithStyle:(YYTextLineStyle)style {
  204. YYTextDecoration *one = [self new];
  205. one.style = style;
  206. return one;
  207. }
  208. + (instancetype)decorationWithStyle:(YYTextLineStyle)style width:(NSNumber *)width color:(UIColor *)color {
  209. YYTextDecoration *one = [self new];
  210. one.style = style;
  211. one.width = width;
  212. one.color = color;
  213. return one;
  214. }
  215. - (void)encodeWithCoder:(NSCoder *)aCoder {
  216. [aCoder encodeObject:@(self.style) forKey:@"style"];
  217. [aCoder encodeObject:self.width forKey:@"width"];
  218. [aCoder encodeObject:self.color forKey:@"color"];
  219. }
  220. - (id)initWithCoder:(NSCoder *)aDecoder {
  221. self = [super init];
  222. self.style = ((NSNumber *)[aDecoder decodeObjectForKey:@"style"]).unsignedIntegerValue;
  223. self.width = [aDecoder decodeObjectForKey:@"width"];
  224. self.color = [aDecoder decodeObjectForKey:@"color"];
  225. return self;
  226. }
  227. - (id)copyWithZone:(NSZone *)zone {
  228. typeof(self) one = [self.class new];
  229. one.style = self.style;
  230. one.width = self.width;
  231. one.color = self.color;
  232. return one;
  233. }
  234. @end
  235. @implementation YYTextBorder
  236. + (instancetype)borderWithLineStyle:(YYTextLineStyle)lineStyle lineWidth:(CGFloat)width strokeColor:(UIColor *)color {
  237. YYTextBorder *one = [self new];
  238. one.lineStyle = lineStyle;
  239. one.strokeWidth = width;
  240. one.strokeColor = color;
  241. return one;
  242. }
  243. + (instancetype)borderWithFillColor:(UIColor *)color cornerRadius:(CGFloat)cornerRadius {
  244. YYTextBorder *one = [self new];
  245. one.fillColor = color;
  246. one.cornerRadius = cornerRadius;
  247. one.insets = UIEdgeInsetsMake(-2, 0, 0, -2);
  248. return one;
  249. }
  250. - (instancetype)init {
  251. self = [super init];
  252. self.lineStyle = YYTextLineStyleSingle;
  253. return self;
  254. }
  255. - (void)encodeWithCoder:(NSCoder *)aCoder {
  256. [aCoder encodeObject:@(self.lineStyle) forKey:@"lineStyle"];
  257. [aCoder encodeObject:@(self.strokeWidth) forKey:@"strokeWidth"];
  258. [aCoder encodeObject:self.strokeColor forKey:@"strokeColor"];
  259. [aCoder encodeObject:@(self.lineJoin) forKey:@"lineJoin"];
  260. [aCoder encodeObject:[NSValue valueWithUIEdgeInsets:self.insets] forKey:@"insets"];
  261. [aCoder encodeObject:@(self.cornerRadius) forKey:@"cornerRadius"];
  262. [aCoder encodeObject:self.shadow forKey:@"shadow"];
  263. [aCoder encodeObject:self.fillColor forKey:@"fillColor"];
  264. }
  265. - (id)initWithCoder:(NSCoder *)aDecoder {
  266. self = [super init];
  267. _lineStyle = ((NSNumber *)[aDecoder decodeObjectForKey:@"lineStyle"]).unsignedIntegerValue;
  268. _strokeWidth = ((NSNumber *)[aDecoder decodeObjectForKey:@"strokeWidth"]).doubleValue;
  269. _strokeColor = [aDecoder decodeObjectForKey:@"strokeColor"];
  270. _lineJoin = (CGLineJoin)((NSNumber *)[aDecoder decodeObjectForKey:@"join"]).unsignedIntegerValue;
  271. _insets = ((NSValue *)[aDecoder decodeObjectForKey:@"insets"]).UIEdgeInsetsValue;
  272. _cornerRadius = ((NSNumber *)[aDecoder decodeObjectForKey:@"cornerRadius"]).doubleValue;
  273. _shadow = [aDecoder decodeObjectForKey:@"shadow"];
  274. _fillColor = [aDecoder decodeObjectForKey:@"fillColor"];
  275. return self;
  276. }
  277. - (id)copyWithZone:(NSZone *)zone {
  278. typeof(self) one = [self.class new];
  279. one.lineStyle = self.lineStyle;
  280. one.strokeWidth = self.strokeWidth;
  281. one.strokeColor = self.strokeColor;
  282. one.lineJoin = self.lineJoin;
  283. one.insets = self.insets;
  284. one.cornerRadius = self.cornerRadius;
  285. one.shadow = self.shadow.copy;
  286. one.fillColor = self.fillColor;
  287. return one;
  288. }
  289. @end
  290. @implementation YYTextAttachment
  291. + (instancetype)attachmentWithContent:(id)content {
  292. YYTextAttachment *one = [self new];
  293. one.content = content;
  294. return one;
  295. }
  296. - (void)encodeWithCoder:(NSCoder *)aCoder {
  297. [aCoder encodeObject:self.content forKey:@"content"];
  298. [aCoder encodeObject:[NSValue valueWithUIEdgeInsets:self.contentInsets] forKey:@"contentInsets"];
  299. [aCoder encodeObject:self.userInfo forKey:@"userInfo"];
  300. }
  301. - (id)initWithCoder:(NSCoder *)aDecoder {
  302. self = [super init];
  303. _content = [aDecoder decodeObjectForKey:@"content"];
  304. _contentInsets = ((NSValue *)[aDecoder decodeObjectForKey:@"contentInsets"]).UIEdgeInsetsValue;
  305. _userInfo = [aDecoder decodeObjectForKey:@"userInfo"];
  306. return self;
  307. }
  308. - (id)copyWithZone:(NSZone *)zone {
  309. typeof(self) one = [self.class new];
  310. if ([self.content respondsToSelector:@selector(copy)]) {
  311. one.content = [self.content copy];
  312. } else {
  313. one.content = self.content;
  314. }
  315. one.contentInsets = self.contentInsets;
  316. one.userInfo = self.userInfo.copy;
  317. return one;
  318. }
  319. @end
  320. @implementation YYTextHighlight
  321. + (instancetype)highlightWithAttributes:(NSDictionary *)attributes {
  322. YYTextHighlight *one = [self new];
  323. one.attributes = attributes;
  324. return one;
  325. }
  326. + (instancetype)highlightWithBackgroundColor:(UIColor *)color {
  327. YYTextBorder *highlightBorder = [YYTextBorder new];
  328. highlightBorder.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
  329. highlightBorder.cornerRadius = 3;
  330. highlightBorder.fillColor = color;
  331. YYTextHighlight *one = [self new];
  332. [one setBackgroundBorder:highlightBorder];
  333. return one;
  334. }
  335. - (void)setAttributes:(NSDictionary *)attributes {
  336. _attributes = attributes.mutableCopy;
  337. }
  338. - (void)encodeWithCoder:(NSCoder *)aCoder {
  339. NSData *data = nil;
  340. @try {
  341. data = [YYTextArchiver archivedDataWithRootObject:self.attributes];
  342. }
  343. @catch (NSException *exception) {
  344. NSLog(@"%@",exception);
  345. }
  346. [aCoder encodeObject:data forKey:@"attributes"];
  347. }
  348. - (id)initWithCoder:(NSCoder *)aDecoder {
  349. self = [super init];
  350. NSData *data = [aDecoder decodeObjectForKey:@"attributes"];
  351. @try {
  352. _attributes = [YYTextUnarchiver unarchiveObjectWithData:data];
  353. }
  354. @catch (NSException *exception) {
  355. NSLog(@"%@",exception);
  356. }
  357. return self;
  358. }
  359. - (id)copyWithZone:(NSZone *)zone {
  360. typeof(self) one = [self.class new];
  361. one.attributes = self.attributes.mutableCopy;
  362. return one;
  363. }
  364. - (void)_makeMutableAttributes {
  365. if (!_attributes) {
  366. _attributes = [NSMutableDictionary new];
  367. } else if (![_attributes isKindOfClass:[NSMutableDictionary class]]) {
  368. _attributes = _attributes.mutableCopy;
  369. }
  370. }
  371. - (void)setFont:(UIFont *)font {
  372. [self _makeMutableAttributes];
  373. if (font == (id)[NSNull null] || font == nil) {
  374. ((NSMutableDictionary *)_attributes)[(id)kCTFontAttributeName] = [NSNull null];
  375. } else {
  376. CTFontRef ctFont = [font CTFontRef];
  377. if (ctFont) {
  378. ((NSMutableDictionary *)_attributes)[(id)kCTFontAttributeName] = (__bridge id)(ctFont);
  379. CFRelease(ctFont);
  380. }
  381. }
  382. }
  383. - (void)setColor:(UIColor *)color {
  384. [self _makeMutableAttributes];
  385. if (color == (id)[NSNull null] || color == nil) {
  386. ((NSMutableDictionary *)_attributes)[(id)kCTForegroundColorAttributeName] = [NSNull null];
  387. ((NSMutableDictionary *)_attributes)[NSForegroundColorAttributeName] = [NSNull null];
  388. } else {
  389. ((NSMutableDictionary *)_attributes)[(id)kCTForegroundColorAttributeName] = (__bridge id)(color.CGColor);
  390. ((NSMutableDictionary *)_attributes)[NSForegroundColorAttributeName] = color;
  391. }
  392. }
  393. - (void)setStrokeWidth:(NSNumber *)width {
  394. [self _makeMutableAttributes];
  395. if (width == (id)[NSNull null] || width == nil) {
  396. ((NSMutableDictionary *)_attributes)[(id)kCTStrokeWidthAttributeName] = [NSNull null];
  397. } else {
  398. ((NSMutableDictionary *)_attributes)[(id)kCTStrokeWidthAttributeName] = width;
  399. }
  400. }
  401. - (void)setStrokeColor:(UIColor *)color {
  402. [self _makeMutableAttributes];
  403. if (color == (id)[NSNull null] || color == nil) {
  404. ((NSMutableDictionary *)_attributes)[(id)kCTStrokeColorAttributeName] = [NSNull null];
  405. ((NSMutableDictionary *)_attributes)[NSStrokeColorAttributeName] = [NSNull null];
  406. } else {
  407. ((NSMutableDictionary *)_attributes)[(id)kCTStrokeColorAttributeName] = (__bridge id)(color.CGColor);
  408. ((NSMutableDictionary *)_attributes)[NSStrokeColorAttributeName] = color;
  409. }
  410. }
  411. - (void)setTextAttribute:(NSString *)attribute value:(id)value {
  412. [self _makeMutableAttributes];
  413. if (value == nil) value = [NSNull null];
  414. ((NSMutableDictionary *)_attributes)[attribute] = value;
  415. }
  416. - (void)setShadow:(YYTextShadow *)shadow {
  417. [self setTextAttribute:YYTextShadowAttributeName value:shadow];
  418. }
  419. - (void)setInnerShadow:(YYTextShadow *)shadow {
  420. [self setTextAttribute:YYTextInnerShadowAttributeName value:shadow];
  421. }
  422. - (void)setUnderline:(YYTextDecoration *)underline {
  423. [self setTextAttribute:YYTextUnderlineAttributeName value:underline];
  424. }
  425. - (void)setStrikethrough:(YYTextDecoration *)strikethrough {
  426. [self setTextAttribute:YYTextStrikethroughAttributeName value:strikethrough];
  427. }
  428. - (void)setBackgroundBorder:(YYTextBorder *)border {
  429. [self setTextAttribute:YYTextBackgroundBorderAttributeName value:border];
  430. }
  431. - (void)setBorder:(YYTextBorder *)border {
  432. [self setTextAttribute:YYTextBorderAttributeName value:border];
  433. }
  434. - (void)setAttachment:(YYTextAttachment *)attachment {
  435. [self setTextAttribute:YYTextAttachmentAttributeName value:attachment];
  436. }
  437. @end