kishikawakatsumi / SECoreTextView

SECoreTextView is multi style text view.
MIT License
943 stars 143 forks source link

use SETextView crash when insert Image #59

Open bing5 opened 8 years ago

bing5 commented 8 years ago
- (void)bubbleAttributtedStringWithText:(NSString *)text isSendingMessage:(BOOL)isSending withView:(SETextView *)textView {
    if (!text) {
        text = @"";
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    textView.attributedText = attributedString;
    UIFont *font = [UIFont systemFontOfSize:16.];

    NSString *regex_text = @"\\[[\u4e00-\u9fa5]+\\]";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regex_text options:NSRegularExpressionCaseInsensitive error:NULL];
    NSArray *matches = [regex matchesInString:text options:NSMatchingReportCompletion range:NSMakeRange(0, attributedString.length)];
    if (matches.count < 1) {
        return;
    }

    CGSize imageSize = CGSizeMake(font.lineHeight, font.lineHeight);
    CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    NSDictionary *cnKeys  = [SFEmoticonManager sharedObject].objectCnKey;

    [textView removeAllAttributedText];
    for (NSTextCheckingResult *result in matches) {
        NSRange matchRange = result.range;

        NSString *placeholder = [text substringWithRange:matchRange];
        SFEmoticon *emoticon  = [cnKeys objectForKey:placeholder];
        if (!emoticon) {
            continue;
        }

        UIImage *image = [UIImage imageNamed:emoticon.imageName];
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
        [image drawInRect:imageRect];
        UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [textView addObject:newimage size:imageSize replaceRange:matchRange];
    }
}

when I use SETextView init and and run top code and ‘SETextView’ attribute ‘attachments’ init has spilth data.at last the APP crash this line: NSAttributedString *originalAttributedString = [editingAttributedText attributedSubstringFromRange:attachment.range];

so I add method ‘[self.attachments removeAllObjects]’ the is ok . I don’t know how?please help me

limost commented 8 years ago

where is the method '[self.attachments removeAllObjects]' ? Before the line 'NSAttributedString *originalAttributedString = [editingAttributedText attributedSubstringFromRange:attachment.range];' or after?

bing5 commented 8 years ago

'[self.attachments removeAllObjects]' 单独的方法,不过是在赋值之前调的