Преглед на файлове

add NSCoding support to YYTextLayout

ibireme преди 9 години
родител
ревизия
4447d54cfd
променени са 2 файла, в които са добавени 31 реда и са изтрити 2 реда
  1. 2 1
      YYText/Component/YYTextLayout.h
  2. 29 1
      YYText/Component/YYTextLayout.m

+ 2 - 1
YYText/Component/YYTextLayout.h

@@ -142,7 +142,7 @@ extern const CGSize YYTextContainerMaxSize;
      │ [--------Line9--------]  │  <- Row6
      └──────────────────────────┘
  */
-@interface YYTextLayout : NSObject
+@interface YYTextLayout : NSObject <NSCoding>
 
 
 #pragma mark - Generate text layout
@@ -212,6 +212,7 @@ extern const CGSize YYTextContainerMaxSize;
 
 @property (nonatomic, readonly) YYTextContainer *container;    ///< The text container
 @property (nonatomic, readonly) NSAttributedString *text;      ///< The full text
+@property (nonatomic, readonly) NSRange range;                 ///< The text range in full text
 @property (nonatomic, readonly) CTFramesetterRef frameSetter;  ///< CTFrameSetter
 @property (nonatomic, readonly) CTFrameRef frame;              ///< CTFrame
 @property (nonatomic, readonly) NSArray *lines;                ///< Array of `YYTextLine`, no truncated

+ 29 - 1
YYText/Component/YYTextLayout.m

@@ -12,6 +12,7 @@
 #import "YYTextLayout.h"
 #import "YYTextUtilities.h"
 #import "YYTextAttribute.h"
+#import "YYTextArchiver.h"
 #import "NSAttributedString+YYText.h"
 #import <libkern/OSAtomic.h>
 
@@ -296,6 +297,7 @@ OSSpinLockUnlock(&_lock);
 
 @property (nonatomic, readwrite) YYTextContainer *container;
 @property (nonatomic, readwrite) NSAttributedString *text;
+@property (nonatomic, readwrite) NSRange range;
 
 @property (nonatomic, readwrite) CTFramesetterRef frameSetter;
 @property (nonatomic, readwrite) CTFrameRef frame;
@@ -395,6 +397,7 @@ OSSpinLockUnlock(&_lock);
     layout = [[YYTextLayout alloc] _init];
     layout.text = text;
     layout.container = container;
+    layout.range = range;
     isVerticalForm = container.verticalForm;
     
     // set cgPath and cgPathBox
@@ -525,7 +528,6 @@ OSSpinLockUnlock(&_lock);
             }
         }
     }
-    lineCount = lines.count;
     
     if (rowCount > 0) {
         if (maximumNumberOfRows > 0) {
@@ -874,6 +876,32 @@ fail:
     if (_lineRowsEdge) free(_lineRowsEdge);
 }
 
+#pragma mark - Coding
+
+
+- (void)encodeWithCoder:(NSCoder *)aCoder {
+    NSData *textData = [YYTextArchiver archivedDataWithRootObject:_text];
+    [aCoder encodeObject:textData forKey:@"text"];
+    [aCoder encodeObject:_container forKey:@"container"];
+    [aCoder encodeObject:[NSValue valueWithRange:_range] forKey:@"range"];
+}
+
+- (id)initWithCoder:(NSCoder *)aDecoder {
+    NSData *textData = [aDecoder decodeObjectForKey:@"text"];
+    NSAttributedString *text = [YYTextUnarchiver unarchiveObjectWithData:textData];
+    YYTextContainer *container = [aDecoder decodeObjectForKey:@"container"];
+    NSRange range = ((NSValue *)[aDecoder decodeObjectForKey:@"range"]).rangeValue;
+    self = [self.class layoutWithContainer:container text:text range:range];
+    return self;
+}
+
+#pragma mark - Copying
+
+- (id)copyWithZone:(NSZone *)zone {
+    return self; // readonly object
+}
+
+
 #pragma mark - Query
 
 /**