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

作者: shannoon | 来源:发表于2016-06-28 12:02 被阅读967次

    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富文本实现简单的图文混排

        本文链接:https://www.haomeiwen.com/subject/rhdtjttx.html