博客> 图文混排。
图文混排。
2018-08-15 02:37 评论:0 阅读:707 wangwei1
ios UIView 图文混排。
        postToFloorTableViewCell * mycell = [tableView dequeueReusableCellWithIdentifier:@"postToFloorTableViewCell"];

        NSString *newName = [ReplyUserName stringByAppendingString:@"  "];

        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithAttributedString:[[NSAttributedString alloc] initWithString:newName]];

// 添加图片 [attr appendAttributedString:[self createImageAttatchment]];

        NSString * newContent = [NSString stringWithFormat:@"  :%@",replyContent];

        [attr appendAttributedString:[[NSAttributedString alloc] initWithString:newContent]];

         [attr addAttribute:NSForegroundColorAttributeName value: UIColorFromRGB(0x48acea) range:NSMakeRange(0, ReplyUserName.length)];

        [attr addAttribute:NSFontAttributeName value: [UIFont systemFontOfSize:15] range:NSMakeRange(0, attr.length)];
        mycell.postToFloorTextView.attributedText = attr;

// 最后选中的textView字段 添加删除等按钮

        mycell.postToFloorTextView.selectedRange = 

NSMakeRange(mycell.postToFloorTextView.attributedText.length - result.length, result.length);

        NSArray *rects = [ mycell.postToFloorTextView selectionRectsForRange: mycell.postToFloorTextView.selectedTextRange];

        for (UITextSelectionRect *textRect in rects) {

            CGRect frame = textRect.rect;

            UIButton *button = [[UIButton alloc] initWithFrame:frame];

            [button setTitle:result forState:UIControlStateNormal];

            button.titleLabel.font = [UIFont systemFontOfSize:12];

            button.backgroundColor = [UIColor redColor];

            [ mycell.postToFloorTextView addSubview:button];

        }

        return mycell;

pragma mark - 图文混排

/**

  • 创建“角色”标记附件,用于将图片嵌在属性字符串中 */

    • (NSMutableAttributedString *)createImageAttatchment{

    UIImage *image = [UIImage imageNamed:@"postparent"];

    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];

    attachment.image = image;

    // 调整附件对象y值,对齐 attachment.bounds = CGRectMake(0, -2, image.size.width, image.size.height);

    NSAttributedString *attatchAttri =[NSAttributedString attributedStringWithAttachment:attachment];

    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] init];

    [attri appendAttributedString:attatchAttri];

    return attri; }

收藏
1
sina weixin mail 回到顶部