0
点赞
收藏
分享

微信扫一扫

ios使用UITextView布局实现填空题

菜头粿子园 2021-09-29 阅读 92
iOS OC开发
    最近在做项目的时候在实现一个填空题的时候遇到一个问题,如下图所示自定义的空格视图布局乱了,在不懈的努力下终于解决了这个问题,现在记录一下。

    一、如果感兴趣的朋友可以去研究研究以下两个三方。

【TYText 与TYAttributedLabel是同一个】https://github.com/12207480/TYText
【YYText】https://github.com/ibireme/YYText

    二、以上两个三方都可以实现,但是时间紧促,TYText提供的案例比较容易快速上手,所以就选择了它,具体使用方法大家可以去看看它的案例。在使用中遇到的问题,解决方法如下:
    经过查看源码分析如下:
- (void)layoutManager:(TYLayoutManager *)layoutManager drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin {
    [self addAttachmentViews];
}

- (void)addAttachmentViews {

    NSArray *attachments = _textRender.attachmentViews;
    if (!_attachments && !attachments) {
        return;
    }
    NSSet *attachmentSet = [NSSet setWithArray:attachments];
    for (TYTextAttachment *attachment in _attachments) {
        if (!attachmentSet || ![attachmentSet containsObject:attachment]) {
            [attachment removeFromSuperView:self];
        }
    }
  
    NSRange visibleRange = [_textRender visibleCharacterRange];
    for (TYTextAttachment *attachment in attachments) {
        if (NSLocationInRange(attachment.range.location, visibleRange)) {
            
                 CGRect rect = {attachment.position,attachment.size};
                [attachment addToSuperView:self];
                attachment.frame = rect;
                       
        }else {
            [attachment removeFromSuperView:self];
        }
    }
    
    _attachments = attachments;
}

    CGRect rect = {attachment.position,attachment.size};
                [attachment addToSuperView:self];
                attachment.frame = rect;
                       
 if (attachment.position.x > 0 && attachment.position.y > 0) {                               CGRect rect = {attachment.position,attachment.size};                
    [attachment addToSuperView:self];
         attachment.frame = rect;
             }
举报

相关推荐

0 条评论