• NSMutableAttributedString 多样化显示文本以及NSTextAttachment实现图文混排


    #import "ViewController.h"
    
    @interface ViewController ()<UITextViewDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIView *bgview = [[UIView alloc] initWithFrame:CGRectMake(20, 200, self.view.frame.size.width-40,60)];
        bgview.backgroundColor  = [UIColor lightGrayColor];
        [self.view addSubview:bgview];
        
        UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(50, 10, bgview.frame.size.width-100, 40) textContainer:nil];
        textview.backgroundColor = [UIColor whiteColor];
        textview.font = [UIFont systemFontOfSize:20];
        textview.delegate = self;
        textview.text = @"我的网址:http://www.baidu.com";
        [bgview addSubview:textview];
        
        //UILabel  UITextField  UItextView
        //多样化显示文本:下划线 字体大小颜色粗细 URL链接
        //attributedText
        //1.将原来的普通文本转化为属性文本 NSAttributedString
        NSMutableAttributedString*attrstr= [[NSMutableAttributedString alloc] initWithString:textview.text] ;
        //2.设置自己的样式  我的网址:http://www.baidu.com
        /*(1)下划线  NSUnderlineStyleAttributeName
         下划线颜色
    NSUnderlineColorAttributeName
        (2)删除线
    NSStrikeyhroughStyleAttributeName
         删除线颜色NSStrikethroughColorAttributeName
        (3)字体
         NSFontAttributeName
         (4)
         NSStrokeWidthAttributeName ->设置笔画宽度(粗细),取值为 NSNumber 对象(整数),负值填充效果,正值中空效果 NSStrokeColorAttributeName ->填充部分颜色,不是字体颜色,取值为 UIColor 对象
         (5)url链接
         NSLinkAttributeName
        */
        [attrstr setAttributes:@{NSUnderlineStyleAttributeName:@1,NSUnderlineColorAttributeName:[UIColor redColor],NSStrikethroughStyleAttributeName:@1,NSStrikethroughColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:25],NSLinkAttributeName:@"http://www.baidu.com"} range:NSMakeRange(5, 20)];
        //将多样化文本赋给textview
        textview.attributedText = attrstr;
        
        //创建图片附件NSTextAttachment  文本附件 实现图文混排
        NSTextAttachment *attach=[[NSTextAttachment alloc] init];
        attach.image = [UIImage imageNamed:@"face"];
        attach.bounds = CGRectMake(0, 0, 20, 20);
        //将这个图片转化为属性字符串
        NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:attach];
        //属性字符串的拼接
        [attrstr appendAttributedString:imageAttr];
        
        //赋值属性字符串
        textview.attributedText=attrstr;
        
    
    
        
    }

  • 相关阅读:
    Ad-papers
    《wifi深入了解抓包分析密码破解》
    《Linux内核分析-内核源码,写操作系统,gdb,系统调用》
    《C/C++ 高级开发 与Linux内核源码探析 提高班(王保明老师)【2】》
    《C/C++ 高级开发 与Linux内核源码探析 提高班(王保明老师)》
    《【公开课】斯坦福李飞飞教授最新cs231n计算机视觉经典课程》
    Tensorflow 介绍和安装
    卷积的发展历程,原理和基于 TensorFlow 的实现
    一文彻底搞懂BP算法:原理推导+数据演示+项目实战(上篇)
    基于深度学习的计算机视觉应用之目标检测
  • 原文地址:https://www.cnblogs.com/kinghyt/p/10467363.html
Copyright © 2020-2023  润新知