首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTextAttachment在使用textkit时覆盖文本

NSTextAttachment在使用textkit时覆盖文本
EN

Stack Overflow用户
提问于 2015-06-07 23:10:28
回答 1查看 334关注 0票数 0

我想在UITextView中插入图片,所以我使用了textkit和NSTextAttachment。但当我插入图片时,图片显示在UITextView内容的上方,效果如下:

代码是这样的:

代码语言:javascript
复制
//
//  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
EN

回答 1

Stack Overflow用户

发布于 2015-06-08 12:09:05

最后,我自己找到了解决方案。因为我使用的NSAttributedString有一个段落样式属性,并且我之前设置了MaximumLineHeight,所以它会影响NSTextAttachment。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30695108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档