YYTextRunDelegate.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // YYTextRunDelegate.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/14.
  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 "YYTextRunDelegate.h"
  12. static void DeallocCallback(void *ref) {
  13. YYTextRunDelegate *self = (__bridge_transfer YYTextRunDelegate *)(ref);
  14. self = nil; // release
  15. }
  16. static CGFloat GetAscentCallback(void *ref) {
  17. YYTextRunDelegate *self = (__bridge YYTextRunDelegate *)(ref);
  18. return self.ascent;
  19. }
  20. static CGFloat GetDecentCallback(void *ref) {
  21. YYTextRunDelegate *self = (__bridge YYTextRunDelegate *)(ref);
  22. return self.descent;
  23. }
  24. static CGFloat GetWidthCallback(void *ref) {
  25. YYTextRunDelegate *self = (__bridge YYTextRunDelegate *)(ref);
  26. return self.width;
  27. }
  28. @implementation YYTextRunDelegate
  29. - (CTRunDelegateRef)CTRunDelegate CF_RETURNS_RETAINED {
  30. CTRunDelegateCallbacks callbacks;
  31. callbacks.version = kCTRunDelegateCurrentVersion;
  32. callbacks.dealloc = DeallocCallback;
  33. callbacks.getAscent = GetAscentCallback;
  34. callbacks.getDescent = GetDecentCallback;
  35. callbacks.getWidth = GetWidthCallback;
  36. return CTRunDelegateCreate(&callbacks, (__bridge_retained void *)(self.copy));
  37. }
  38. - (void)encodeWithCoder:(NSCoder *)aCoder {
  39. [aCoder encodeObject:@(_ascent) forKey:@"ascent"];
  40. [aCoder encodeObject:@(_descent) forKey:@"descent"];
  41. [aCoder encodeObject:@(_width) forKey:@"width"];
  42. [aCoder encodeObject:_userInfo forKey:@"userInfo"];
  43. }
  44. - (id)initWithCoder:(NSCoder *)aDecoder {
  45. self = [super init];
  46. _ascent = ((NSNumber *)[aDecoder decodeObjectForKey:@"ascent"]).floatValue;
  47. _descent = ((NSNumber *)[aDecoder decodeObjectForKey:@"descent"]).floatValue;
  48. _width = ((NSNumber *)[aDecoder decodeObjectForKey:@"width"]).floatValue;
  49. _userInfo = [aDecoder decodeObjectForKey:@"userInfo"];
  50. return self;
  51. }
  52. - (id)copyWithZone:(NSZone *)zone {
  53. typeof(self) one = [self.class new];
  54. one.ascent = self.ascent;
  55. one.descent = self.descent;
  56. one.width = self.width;
  57. one.userInfo = self.userInfo;
  58. return one;
  59. }
  60. @end