iOS ReactiveCocoa RACObserve UITextView NSAttributedString

时间:2015-04-14 19:16:40

标签: ios objective-c nsattributedstring reactive-cocoa

我正在尝试使用ReactiveCocoa绑定将UITextView的attributedText绑定到模型。但是,referencedString值不会按预期保存。这种绑定有问题吗?

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:self.note.attributedText
                                                                      options:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                                                                NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                           documentAttributes:nil error:&error];
    self.textTextView.attributedText = attributedText;

    RAC(self.note, attributedText) = [RACObserve(self.textTextView, attributedText) map:^id(NSAttributedString *attributedText) {
        return [attributedText dataFromRange:NSMakeRange(0, attributedText.length)
                   documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                        NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                error:nil];
    }];

1 个答案:

答案 0 :(得分:0)

我相信您可以在此处使用rac_textSignal类别。你尝试过类似的东西吗?

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:self.note.attributedText
                                                                      options:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                                                                NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                           documentAttributes:nil error:&error];
    self.textTextView.attributedText = attributedText;

    @weakify(self);
    RAC(self.note, attributedText) = [self.textTextView.rac_textSignal map:^id(__unused NSString *text) {
        @strongify(self);
        return [self.textTextView.attributedText dataFromRange:NSMakeRange(0, attributedText.length)
                   documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
                                        NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                error:nil];
    }];
}