美文网首页
系统讲解NSAttributedString

系统讲解NSAttributedString

作者: 写啥呢 | 来源:发表于2016-10-26 19:53 被阅读0次
    
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            //富文本是字符串的一种,可以对同一个字符串中的字符设置不同的样式
            
            //设置文字
            //setText()
            //设置段落
            setParagraph()
            //插入图片---图文混排
            //setImage()
        }
        
        func setText() {
            let label1 = UILabel(frame: CGRectMake(10,10,200,50))
            label1.textColor = UIColor.blackColor()
            label1.font = UIFont.systemFontOfSize(20)
            self.view.addSubview(label1)
            
            let str1 = "¥300/位"
            
            //初始化富文本
            let attStr =  NSMutableAttributedString(string: str1)
            //分别设置单一的属性
            //设置字体大小
            //attStr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(30), range: NSMakeRange(0, 4))
            //设置字体的颜色
            //attStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 4))
            
            //通过字典的方式统一设置
            attStr.addAttributes([NSFontAttributeName:UIFont.boldSystemFontOfSize(30),NSForegroundColorAttributeName:UIColor.blueColor()], range: NSMakeRange(0, 4))
            
            
            //赋值
            label1.attributedText = attStr
        }
        
        func setParagraph() {
            
            let label2 = UILabel(frame: CGRectMake(0,10,self.view.frame.size.width,400))
            label2.numberOfLines = 0
            //设置折行方式
            label2.lineBreakMode = .ByWordWrapping
            self.view.addSubview(label2)
            
            let str = "苹果公司(Apple Inc. )是美国的一家高科技公司。由史蒂夫·乔布斯、斯蒂夫·沃兹尼亚克和罗·韦恩(Ron Wayne)等人于1976年4月1日创立,并命名为美国苹果电脑公司(Apple Computer Inc. ), 2007年1月9日更名为苹果公司,总部位于加利福尼亚州的库比蒂诺。苹果公司1980年12月12日公开招股上市,2012年创下6235亿美元的市值记录,截至2014年6月,苹果公司已经连续三年成为全球市值最大公司。苹果公司在2016年世界500强排行榜中排名第9名。"
            
          //初始化富文本
          let attStr = NSMutableAttributedString(string: str)
            
          //初始化段落样式
            let paraStyle = NSMutableParagraphStyle()
            //设置行间距,返回值类型为CGFloat
            paraStyle.lineSpacing = 20.0
            //段落间距
            paraStyle.paragraphSpacing = 10.0
            //设置首行缩进的距离
            paraStyle.firstLineHeadIndent = 40.0
            //设置除首行之外的行缩进的距离
            paraStyle.headIndent = 20.0
            
            //将段落样式设置给富文本
            attStr.addAttributes([NSParagraphStyleAttributeName:paraStyle], range: NSMakeRange(0, (str as NSString).length))
            
            
           label2.attributedText = attStr
        }
        
        func setImage() {
            
            let label3 = UILabel(frame: CGRectMake(0,10,300,400))
            label3.numberOfLines = 0
            label3.lineBreakMode = .ByWordWrapping
            label3.text = "苹果公司(Apple Inc. )是美国的一家高科技公司。由史蒂夫·乔布斯、斯蒂夫·沃兹尼亚克和罗·韦恩(Ron Wayne)等人于1976年4月1日创立,并命名为美国苹果电脑公司(Apple Computer Inc. ), 2007年1月9日更名为苹果公司,总部位于加利福尼亚州的库比蒂诺。苹果公司1980年12月12日公开招股上市,2012年创下6235亿美元的市值记录"
            self.view.addSubview(label3)
            
            //初始化富文本,mutableCopy复制一份对象
            let attStr = label3.attributedText?.mutableCopy()
            
            //插入图片
            let image = UIImage(named: "111")
            //初始化图文混排的类
            let attachment = NSTextAttachment()
            //设置大小---图片的大小
            attachment.bounds = CGRectMake(0, 0, 30, 30)
            //将图片赋给图文混排的类
            attachment.image = image
            
            //通过图文混排的类初始化一个新的富文本字符串
            let attStr1 = NSAttributedString(attachment: attachment)
            
    
            //将图片插入到原来的文字中
            attStr?.insertAttributedString(attStr1, atIndex: 50)
            
            label3.attributedText = attStr as! NSMutableAttributedString
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:系统讲解NSAttributedString

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