YYTextRubyAnnotation.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // YYTextRubyAnnotation.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/4/24.
  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 "YYTextRubyAnnotation.h"
  12. @implementation YYTextRubyAnnotation
  13. - (instancetype)init {
  14. self = super.init;
  15. self.alignment = kCTRubyAlignmentAuto;
  16. self.overhang = kCTRubyOverhangAuto;
  17. self.sizeFactor = 0.5;
  18. return self;
  19. }
  20. + (instancetype)rubyWithCTRubyRef:(CTRubyAnnotationRef)ctRuby {
  21. if (!ctRuby) return nil;
  22. YYTextRubyAnnotation *one = [self new];
  23. one.alignment = CTRubyAnnotationGetAlignment(ctRuby);
  24. one.overhang = CTRubyAnnotationGetOverhang(ctRuby);
  25. one.sizeFactor = CTRubyAnnotationGetSizeFactor(ctRuby);
  26. one.textBefore = (__bridge NSString *)(CTRubyAnnotationGetTextForPosition(ctRuby, kCTRubyPositionBefore));
  27. one.textAfter = (__bridge NSString *)(CTRubyAnnotationGetTextForPosition(ctRuby, kCTRubyPositionAfter));
  28. one.textInterCharacter = (__bridge NSString *)(CTRubyAnnotationGetTextForPosition(ctRuby, kCTRubyPositionInterCharacter));
  29. one.textInline = (__bridge NSString *)(CTRubyAnnotationGetTextForPosition(ctRuby, kCTRubyPositionInline));
  30. return one;
  31. }
  32. - (CTRubyAnnotationRef)CTRubyAnnotation CF_RETURNS_RETAINED {
  33. if (((long)CTRubyAnnotationCreate + 1) == 1) return NULL; // system not support
  34. CFStringRef text[kCTRubyPositionCount];
  35. text[kCTRubyPositionBefore] = (__bridge CFStringRef)(_textBefore);
  36. text[kCTRubyPositionAfter] = (__bridge CFStringRef)(_textAfter);
  37. text[kCTRubyPositionInterCharacter] = (__bridge CFStringRef)(_textInterCharacter);
  38. text[kCTRubyPositionInline] = (__bridge CFStringRef)(_textInline);
  39. CTRubyAnnotationRef ruby = CTRubyAnnotationCreate(_alignment, _overhang, _sizeFactor, text);
  40. return ruby;
  41. }
  42. - (id)copyWithZone:(NSZone *)zone {
  43. YYTextRubyAnnotation *one = [self.class new];
  44. one.alignment = _alignment;
  45. one.overhang = _overhang;
  46. one.sizeFactor = _sizeFactor;
  47. one.textBefore = _textBefore;
  48. one.textAfter = _textAfter;
  49. one.textInterCharacter = _textInterCharacter;
  50. one.textInline = _textInline;
  51. return one;
  52. }
  53. - (void)encodeWithCoder:(NSCoder *)aCoder {
  54. [aCoder encodeObject:@(_alignment) forKey:@"alignment"];
  55. [aCoder encodeObject:@(_overhang) forKey:@"overhang"];
  56. [aCoder encodeObject:@(_sizeFactor) forKey:@"sizeFactor"];
  57. [aCoder encodeObject:_textBefore forKey:@"textBefore"];
  58. [aCoder encodeObject:_textAfter forKey:@"textAfter"];
  59. [aCoder encodeObject:_textInterCharacter forKey:@"textInterCharacter"];
  60. [aCoder encodeObject:_textInline forKey:@"textInline"];
  61. }
  62. - (id)initWithCoder:(NSCoder *)aDecoder {
  63. self = [self init];
  64. _alignment = ((NSNumber *)[aDecoder decodeObjectForKey:@"alignment"]).intValue;
  65. _overhang = ((NSNumber *)[aDecoder decodeObjectForKey:@"overhang"]).intValue;
  66. _sizeFactor = ((NSNumber *)[aDecoder decodeObjectForKey:@"sizeFactor"]).intValue;
  67. _textBefore = [aDecoder decodeObjectForKey:@"textBefore"];
  68. _textAfter = [aDecoder decodeObjectForKey:@"textAfter"];
  69. _textInterCharacter = [aDecoder decodeObjectForKey:@"textInterCharacter"];
  70. _textInline = [aDecoder decodeObjectForKey:@"textInline"];
  71. return self;
  72. }
  73. @end