zoukankan      html  css  js  c++  java
  • 把图片添加到文字中实现图文混排

    NSString * introString = @"圣芭芭拉,圣莫妮卡海滩,圣地亚哥老城,科罗拉多岛";
    NSArray * introArray = [introString componentsSeparatedByString:@","];

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(13, 0, self.view.frame.size.width-26, [self askTextHeight:introString])];
    label.font = [UIFont systemFontOfSize:15];
    label.text = introString;
    label.numberOfLines = 0;
    label.backgroundColor = UIColorFromRGB(0xf7f7f7);
    [routecell addSubview:label];

    NSMutableAttributedString * mutStr = [label.attributedText mutableCopy];
    for (int i = 0; i<introArray.count-1; i++) {
    NSString * ssssting = introArray[i];
    NSRange sssRang = [introString rangeOfString:ssssting];
    UIImage * image1 = [UIImage imageNamed:@"detail_more_arrowback"];
    NSTextAttachment * attachment1 = [[NSTextAttachment alloc] init];
    attachment1.bounds = CGRectMake(0, 0, 37, 13);
    attachment1.image = image1;
    NSAttributedString * attachStr1 = [NSAttributedString attributedStringWithAttachment:attachment1];
    [mutStr replaceCharactersInRange:NSMakeRange(sssRang.length+sssRang.location, 1) withAttributedString:attachStr1];
    }
    label.attributedText = [mutStr copy];

    NSMutableAttributedString *attributedString = [label.attributedText mutableCopy];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:16];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.attributedText length])];
    label.attributedText = attributedString;
    [label sizeToFit];

    效果:

  • 相关阅读:
    Freemarker中JS取Data-model的值的问题
    Jquery动态添加元素并给元素增加onchange相应
    [算法] 动态规划
    Linux安装mysql.8.0.12
    Linux命令
    [算法] 并查集
    Flume整合Kafka完成实时数据采集
    Kafka 单节点部署
    Spark Streaming——Flume实例
    Spark实战——日志分析
  • 原文地址:https://www.cnblogs.com/xilanglang/p/5504221.html
Copyright © 2011-2022 走看看