NSMutableAttributedString+RYText.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // NSMutableAttributedString+RYText.m
  3. // Asteria
  4. //
  5. // Created by 王猛 on 2024/1/6.
  6. //
  7. #import "NSMutableAttributedString+RYText.h"
  8. @implementation NSMutableAttributedString (RYText)
  9. - (void)setFont:(UIFont *)font {
  10. [self setFont:font range:NSMakeRange(0, self.length)];
  11. }
  12. -(void)setColor:(UIColor *)color {
  13. [self setColor:color range:NSMakeRange(0, self.length)];
  14. }
  15. - (void)setUnderlineStyle:(NSUnderlineStyle)underlineStyle {
  16. [self setUnderlineStyle:underlineStyle range:NSMakeRange(0, self.length)];
  17. }
  18. - (void)setFont:(UIFont *)font range:(NSRange)range {
  19. [self setAttribute:NSFontAttributeName value:font range:range];
  20. }
  21. - (void)setColor:(UIColor *)color range:(NSRange)range {
  22. [self setAttribute:NSForegroundColorAttributeName value:color range:range];
  23. }
  24. - (void)setUnderlineStyle:(NSUnderlineStyle)underlineStyle range:(NSRange)range {
  25. NSNumber *style = underlineStyle == 0 ? nil : @(underlineStyle);
  26. [self setAttribute:NSUnderlineStyleAttributeName value:style range:range];
  27. }
  28. - (void)setAttribute:(NSString *)name value:(id)value range:(NSRange)range {
  29. if (!name || [NSNull isEqual:name]) return;
  30. if (value && ![NSNull isEqual:value]) {
  31. [self addAttribute:name value:value range:range];
  32. }else{
  33. [self removeAttribute:name range:range];
  34. }
  35. }
  36. #pragma mark - **************** get ****************
  37. - (UIFont *)font {
  38. return [self fontAtIndex:0];
  39. }
  40. - (UIFont *)fontAtIndex:(NSUInteger)index {
  41. UIFont *font = [self attribute:NSFontAttributeName atIndex:index];
  42. return font;
  43. }
  44. - (UIColor *)color {
  45. return [self colorAtIndex:0];
  46. }
  47. - (UIColor *)colorAtIndex:(NSUInteger)index {
  48. UIColor *color = [self attribute:NSForegroundColorAttributeName atIndex:index];
  49. if (!color) {
  50. CGColorRef ref = (__bridge CGColorRef)([self attribute:(NSString *)kCTForegroundColorAttributeName atIndex:index]);
  51. color = [UIColor colorWithCGColor:ref];
  52. }
  53. if (color && ![color isKindOfClass:[UIColor class]]) {
  54. if (CFGetTypeID((__bridge CFTypeRef)(color)) == CGColorGetTypeID()) {
  55. color = [UIColor colorWithCGColor:(__bridge CGColorRef)(color)];
  56. } else {
  57. color = nil;
  58. }
  59. }
  60. return color;
  61. }
  62. - (NSUnderlineStyle)strikethroughStyle {
  63. return [self strikethroughStyleAtIndex:0];
  64. }
  65. - (NSUnderlineStyle)strikethroughStyleAtIndex:(NSUInteger)index {
  66. NSNumber *style = [self attribute:NSStrikethroughStyleAttributeName atIndex:index];
  67. return style.integerValue;
  68. }
  69. - (id)attribute:(NSString *)attributeName atIndex:(NSUInteger)index {
  70. if (!attributeName) return nil;
  71. if (index > self.length || self.length == 0) return nil;
  72. if (self.length > 0 && index == self.length) index--;
  73. return [self attribute:attributeName atIndex:index effectiveRange:NULL];
  74. }
  75. @end