iOS富文本实现简单的图文混排

1. 图文混排示例

iOS富文本实现简单的图文混排_第1张图片
untitled.png

// 1. 文字部分
        NSString *tempString1 = @"绑定手机号 " ;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:tempString1];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = THHomeShowContentLineSpacing; //调整行间距
        paragraphStyle.alignment = NSTextAlignmentLeft;
        [attributedString addAttributes:@{NSForegroundColorAttributeName : XLHexColor(THMainLightBlueColor) , NSFontAttributeName : XLSystemFont(12) ,NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, tempString1.length)];
        
        // 2. 添加表情
        NSTextAttachment *attchment = [[NSTextAttachment alloc] init];
        // 表情图片
        attchment.image = [UIImage imageNamed:@"btn_go"];
        // 设置图片大小 以及xy的偏移量 , 注意一点y是负值的时候,图片位置往下偏移
        attchment.bounds = CGRectMake(0, -20, 10, 10);
        [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attchment]];
        NSAttributedString *attributedString2 = [[NSAttributedString alloc] initWithString:@" 绑定支付宝" attributes:
                                                 @{NSForegroundColorAttributeName : XLHexColor(THMainLightBlueColor) , NSFontAttributeName : XLSystemFont(12) ,NSParagraphStyleAttributeName : paragraphStyle}
                                                 ];
        [attributedString appendAttributedString:attributedString2];
        indicatorLabel.attributedText = attributedString;
  • iOS技术开发交流QQ群: 579572313

你可能感兴趣的:(iOS富文本实现简单的图文混排)