OCBarrageTextDescriptor.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // OCBarrageTextDescriptor.m
  3. // TestApp
  4. //
  5. // Created by QMTV on 2017/8/23.
  6. // Copyright © 2017年 LFC. All rights reserved.
  7. //
  8. #import "OCBarrageTextDescriptor.h"
  9. @implementation OCBarrageTextDescriptor
  10. @synthesize textFont = _textFont, textColor = _textColor, shadowColor = _shadowColor, attributedText = _attributedText;
  11. - (instancetype)init {
  12. self = [super init];
  13. if (self) {
  14. _textAttribute = [NSMutableDictionary dictionary];
  15. _shadowColor = [UIColor blackColor];
  16. _shadowOffset = CGSizeZero;
  17. _shadowRadius = 2.0;
  18. _shadowOpacity = 0.5;
  19. }
  20. return self;
  21. }
  22. #pragma mark ----- setter
  23. - (void)setTextShadowOpened:(BOOL)textShadowOpened {
  24. _textShadowOpened = textShadowOpened;
  25. if (textShadowOpened) {
  26. [_textAttribute removeObjectForKey:NSStrokeColorAttributeName];
  27. [_textAttribute removeObjectForKey:NSStrokeWidthAttributeName];
  28. }
  29. }
  30. - (void)setTextFont:(UIFont *)textFont {
  31. _textFont = textFont;
  32. [_textAttribute setValue:textFont forKey:NSFontAttributeName];
  33. }
  34. - (void)setTextColor:(UIColor *)textColor {
  35. _textColor = textColor;
  36. [_textAttribute setValue:textColor forKey:NSForegroundColorAttributeName];
  37. }
  38. - (void)setStrokeColor:(UIColor *)strokeColor {
  39. _strokeColor = strokeColor;
  40. if (_textShadowOpened) {
  41. return;
  42. }
  43. [_textAttribute setValue:strokeColor forKey:NSStrokeColorAttributeName];
  44. }
  45. - (void)setStrokeWidth:(int)strokeWidth {
  46. _strokeWidth = strokeWidth;
  47. if (_textShadowOpened) {
  48. return;
  49. }
  50. [_textAttribute setValue:[NSNumber numberWithInt:strokeWidth] forKey:NSStrokeWidthAttributeName];
  51. }
  52. #pragma mark ----- getter
  53. - (NSString *)text {
  54. if (!_text) {
  55. _text = _attributedText.string;
  56. }
  57. return _text;
  58. }
  59. - (UIFont *)textFont {
  60. if (!_textFont) {
  61. _textFont = [UIFont systemFontOfSize:17.0];
  62. }
  63. return _textFont;
  64. }
  65. - (UIColor *)textColor {
  66. if (!_textColor) {
  67. _textColor = [UIColor whiteColor];
  68. }
  69. return _textColor;
  70. }
  71. - (UIColor *)shadowColor {
  72. if (!_shadowColor) {
  73. _shadowColor = [UIColor blackColor];
  74. }
  75. return _shadowColor;
  76. }
  77. - (NSAttributedString *)attributedText {
  78. if (!_attributedText) {
  79. if (!_text) {
  80. return nil;
  81. }
  82. _attributedText = [[NSAttributedString alloc] initWithString:_text attributes:_textAttribute];
  83. }
  84. //修复阿拉伯文字显示的bug.
  85. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  86. [paragraphStyle setBaseWritingDirection:NSWritingDirectionLeftToRight];
  87. NSMutableAttributedString *tempText = [[NSMutableAttributedString alloc] initWithAttributedString:_attributedText];
  88. [tempText addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, tempText.string.length)];
  89. return [tempText copy];
  90. }
  91. @end
  92. /**
  93. * API: Character Attributes , NSAttributedString 共有21个属性
  94. *
  95. * 1. NSFontAttributeName ->设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
  96. *
  97. *
  98. * 2. NSParagraphStyleAttributeName ->设置文本段落排版格式,取值为 NSParagraphStyle 对象(详情见下面的API说明)
  99. *
  100. *
  101. * 3. NSForegroundColorAttributeName ->设置字体颜色,取值为 UIColor对象,默认值为黑色
  102. *
  103. *
  104. * 4. NSBackgroundColorAttributeName ->设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
  105. *
  106. *
  107. * 5. NSLigatureAttributeName ->设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
  108. *
  109. *
  110. * 6. NSKernAttributeName ->设置字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
  111. *
  112. *
  113. * 7. NSStrikethroughStyleAttributeName ->设置删除线,取值为 NSNumber 对象(整数)
  114. *
  115. *
  116. * 8. NSStrikethroughColorAttributeName ->设置删除线颜色,取值为 UIColor 对象,默认值为黑色
  117. *
  118. *
  119. * 9. NSUnderlineStyleAttributeName ->设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
  120. *
  121. *
  122. * 10. NSUnderlineColorAttributeName ->设置下划线颜色,取值为 UIColor 对象,默认值为黑色
  123. *
  124. *
  125. * 11. NSStrokeWidthAttributeName ->设置笔画宽度(粗细),取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
  126. *
  127. *
  128. * 12. NSStrokeColorAttributeName ->填充部分颜色,不是字体颜色,取值为 UIColor 对象
  129. *
  130. *
  131. * 13. NSShadowAttributeName ->设置阴影属性,取值为 NSShadow 对象
  132. *
  133. *
  134. * 14. NSTextEffectAttributeName ->设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
  135. *
  136. *
  137. * 15. NSBaselineOffsetAttributeName ->设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
  138. *
  139. *
  140. * 16. NSObliquenessAttributeName ->设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
  141. *
  142. *
  143. * 17. NSExpansionAttributeName ->设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
  144. *
  145. *
  146. * 18. NSWritingDirectionAttributeName ->设置文字书写方向,从左向右书写或者从右向左书写
  147. *
  148. *
  149. * 19. NSVerticalGlyphFormAttributeName ->设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
  150. *
  151. *
  152. * 20. NSLinkAttributeName ->设置链接属性,点击后调用浏览器打开指定URL地址
  153. *
  154. *
  155. * 21. NSAttachmentAttributeName ->设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
  156. *
  157. */