zoukankan      html  css  js  c++  java
  • 富文本-图文混排

    图文混排

        // 图文混排
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
        // 1 - 你好
        NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"你好"];
        [attributedText appendAttributedString:first];
    
        // 2 - 图片
        // 带有图片的附件对象
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = [UIImage imageNamed:@"header_cry_icon"];
        CGFloat lineH = label.font.lineHeight;
        attachment.bounds = CGRectMake(0, - ((label.xmg_height - lineH) * 0.5 - 1), lineH, lineH);
        // 将附件对象包装成一个属性文字
        NSAttributedString *second = [NSAttributedString attributedStringWithAttachment:attachment];
        [attributedText appendAttributedString:second];
    
        // 3 - 哈哈哈
        NSAttributedString *third = [[NSAttributedString alloc] initWithString:@"哈哈哈"];
        [attributedText appendAttributedString:third];
    
        label.attributedText = attributedText;
    

    结果展示

      UILabel *label = [[UILabel alloc] init];
      //    label.text = @"你好哈哈哈";
        NSMutableAttributedString *text =    [[NSMutableAttributedString alloc] initWithString:@"你好哈哈哈"];
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 3)];
        [text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
        label.attributedText = text;
        label.frame = CGRectMake(100, 100, 100, 25);
        [self.view addSubview:label];
    

    结果展示

        UILabel *label = [[UILabel alloc] init];
        // 设置属性文字
        NSString *text = @"你好
    哈哈哈";
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
        [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, text.length)];
        [attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 3)];
        label.attributedText = attributedText;
        // 其他设置
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        label.frame = CGRectMake(0, 0, 100, 40);
        [self.view addSubview:label];
        self.navigationItem.titleView = label;
    

    结果展示

    Me WMHaa
  • 相关阅读:
    hdu 4504 dp问题 转化能力不够 对状态的转移也是不够
    BZOJ_2594_[Wc2006]水管局长数据加强版_LCT
    BZOJ_4530_[Bjoi2014]大融合_LCT
    BZOJ_3669_[Noi2014]魔法森林_LCT
    BZOJ_1180_[CROATIAN2009]OTOCI_LCT
    BZOJ_2631_tree_LCT
    BZOJ_3282_Tree_LCT
    BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT
    BZOJ_2622_[2012国家集训队测试]深入虎穴_最短路
    BZOJ_3653_谈笑风生_树状数组
  • 原文地址:https://www.cnblogs.com/WMHaa/p/5280412.html
Copyright © 2011-2022 走看看