TextView的图文混排

234 阅读2分钟

搞个图文混排,发现网上的都比较复杂,我就想简单的使用一下textview,没有必要用coreText等复杂的东西,上代码啦

    NSString *textStr = @"首单红包规则\n1.每个淘宝账号每天可领取一次\n2.首单礼金金额随机,以实际领取的金额为准\n3.礼金当日有效,请及时使用\n4.领取后建议当日使用,否则以后领取的金额会变小\n5.使用礼金的订单发生退款,礼金作废\n";
    self.view.backgroundColor = SepratorLineColor;
    UITextView * textview = [[UITextView alloc] init];    textview.frame = CGRectMake(0, 0, ScreenWidth, 600);    [self.view addSubview:textview];    [textview mas_makeConstraints:^(MASConstraintMaker *make) {       make.left.right.bottom.mas_equalTo(0);       make.top.mas_equalTo(WidthRate(15));    }];   textview.font = SYSTEM_FONT(13);   textview.editable = false;   textview.textColor = textColor_333333;

    //调整文字和间距    NSMutableParagraphStyle *MParaStyle = [[NSMutableParagraphStyle alloc] init];    MParaStyle.paragraphSpacing = 11; // 段落间距离    MParaStyle.firstLineHeadIndent = 15;   NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:textStr];   [att addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 6)];   [att addAttribute:NSFontAttributeName value:BoldSYSTEM_FONT(16) range:NSMakeRange(0, 6)];  [att  addAttribute:NSParagraphStyleAttributeName value:MParaStyle range:NSMakeRange(0, [textStr length])];     //添加分割线图片
    NSTextAttachment *attachmentLine = [[NSTextAttachment alloc] init];
      attachmentLine.image = [YFDTools createImageWithColor:SepratorLineColor frame:CGRectMake(0, 0, ScreenWidth, WidthRate(15))];
      NSMutableAttributedString *attachmentLineString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachmentLine]];    attachmentLine.bounds = CGRectMake(0, -0, ScreenWidth, WidthRate(15));    [att appendAttributedString:attachmentLineString];
   NSString *SecondtextStr = @"\n\n如何使用\n第一步:领取首单礼金(禁拍)\n";    //调整文字和间距     NSMutableParagraphStyle *SecondMParaStyle = [[NSMutableParagraphStyle alloc] init];    SecondMParaStyle.paragraphSpacing = 11; // 段落间距离    SecondMParaStyle.firstLineHeadIndent = 15;    NSMutableAttributedString *secondAtt = [[NSMutableAttributedString alloc] initWithString:SecondtextStr];    [secondAtt addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 6)];     [secondAtt addAttribute:NSFontAttributeName value:BoldSYSTEM_FONT(16) range:NSMakeRange(0, 4)];     [secondAtt  addAttribute:NSParagraphStyleAttributeName value:SecondMParaStyle range:NSMakeRange(0, [SecondtextStr length])];    [att appendAttributedString:secondAtt];

    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];      attachment.image = [UIImage imageNamed:@"bg_explain1"];
      NSMutableAttributedString *attachmentString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];    attachment.bounds = CGRectMake(0, -200, ScreenWidth, WidthRate(235));    [att appendAttributedString:attachmentString];
    NSString *ThirdtextStr = @"\n第二步:返回活动页面选择满意的商品下单\n";
      //调整文字和间距       NSMutableParagraphStyle *thirdMParaStyle = [[NSMutableParagraphStyle alloc] init];       thirdMParaStyle.paragraphSpacing = 11; // 段落间距离       thirdMParaStyle.firstLineHeadIndent = 15;       NSMutableAttributedString *thirdAtt = [[NSMutableAttributedString alloc] initWithString:ThirdtextStr];       [thirdAtt  addAttribute:NSParagraphStyleAttributeName value:thirdMParaStyle range:NSMakeRange(0, [ThirdtextStr length])];      [att appendAttributedString:thirdAtt];
    NSTextAttachment *attachment1 = [[NSTextAttachment alloc] init];
      attachment1.image = [UIImage imageNamed:@"bg_explain2"];
      NSMutableAttributedString *attachmentString1 = (NSMutableAttributedString *)[NSAttributedString attributedStringWithAttachment:attachment1];
//只是简单的计算的高度    CGFloat newH = 368*279/346;    attachment1.bounds = CGRectMake(0, 0, WidthRate(368), WidthRate(newH));
    [att appendAttributedString:attachmentString1];    textview.attributedText = att;

实现效果