2015-06-07 4 views
0

Я хочу вставить изображение в UITextView, поэтому я использую textkit и NSTextAttachment. Но когда я вставить изображение, отображаемое изображение выше содержание в UITextView, эффект, как показано ниже: enter image description hereNSTextAttachment обложка текста при использовании textkit

и код выглядит так:

// 
// TextKitView.m 
// TextKit 
// 
// Copyright (c) 2015 icell.com. All rights reserved. 
// 

#import "SNArticleView.h" 

@interface SNArticleImageAttachment : NSTextAttachment 

@end 

@implementation SNArticleImageAttachment 

- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex { 

    CGFloat attachmentWidth = CGRectGetWidth(lineFrag) - textContainer.lineFragmentPadding * 2; 
    return CGRectMake(0, 0, attachmentWidth, attachmentWidth/self.image.size.width * self.image.size.height); 
} 



@end 



@interface SNArticleView() 

@property (strong, nonatomic) NSTextStorage *textStorage; 
@property (strong, nonatomic) UITextView *textView; 

@end 

@implementation SNArticleView 

- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self commonInit]; 
    } 
    return self; 
} 

- (void)commonInit { 
    _textStorage = [[NSTextStorage alloc] init]; 

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; 
    [_textStorage addLayoutManager:layoutManager]; 

    NSTextContainer *textContainer = [[NSTextContainer alloc] init]; 
    [layoutManager addTextContainer:textContainer]; 

    _textView = [[UITextView alloc] initWithFrame:self.bounds textContainer:textContainer]; 
    [_textView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth]; 
    [_textView.textContainer setLineFragmentPadding:20]; 
    [self addSubview:_textView]; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    [self.textView setFrame:self.bounds]; 
} 

- (void)setAttributedText:(NSAttributedString *)attributedText { 
    _attributedText = [attributedText copy]; 
    [_textStorage insertAttributedString:_attributedText atIndex:0]; 
} 

- (void)insertImage:(UIImage *)image atLocation:(NSUInteger)index; { 
    NSMutableAttributedString *attributedTemp = [self.attributedText mutableCopy]; 

    SNArticleImageAttachment *attachment = [[SNArticleImageAttachment alloc] init]; 
    [attachment setImage:image]; 
    NSAttributedString *imageAttrStr = [NSAttributedString attributedStringWithAttachment:attachment]; 
    [attributedTemp insertAttributedString:imageAttrStr atIndex:_attributedText.length]; 

    [_textStorage replaceCharactersInRange:NSMakeRange(0, _attributedText.length) withAttributedString:attributedTemp]; 

} 

@end 

ответ

1

Наконец, я нашел решение самостоятельно. Поскольку у NSAttributedString, который я использовал, был атрибут стиля абзаца, и я устанавливал MaximumLineHeight раньше, поэтому он выполнял NSTextAttachment.

 Смежные вопросы

  • Нет связанных вопросов^_^