美文网首页
Swift-富文本使用

Swift-富文本使用

作者: SK丿希望 | 来源:发表于2018-08-18 14:06 被阅读0次
image.png

定义公用的属性

        let str = "我不要种田 我要当老板"
        let attrStr = NSMutableAttributedString.init(string: str)

修改颜色(可多段)

// MARK: - 修改颜色
        let textLabel = UILabel.init(frame: CGRect.init(x: 20, y: 100, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel.textColor = UIColor.red
        view.addSubview(textLabel)
        attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.orange, range:NSRange.init(location:0, length: 5))
        attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.red, range:NSRange.init(location:6, length: 5))
        textLabel.attributedText = attrStr

删除线

 // MARK: - 删除线
        let textLabel2 = UILabel.init(frame: CGRect.init(x: 20, y: 130, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel2.textColor = UIColor.red
        view.addSubview(textLabel2)
        attrStr.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber.init(value: 1), range: NSRange.init(location:0, length: str.count))
        textLabel2.attributedText = attrStr

文字大小

// MARK: - 文字大小
        let textLabel3 = UILabel.init(frame: CGRect.init(x: 20, y: 160, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel3.textColor = UIColor.red
        view.addSubview(textLabel3)
        textLabel3.attributedText = NSAttributedString.init(string: str, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize:28)])

字体颜色

// MARK: - 字体颜色
        let textLabel4 = UILabel.init(frame: CGRect.init(x: 20, y: 190, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel4.textColor = UIColor.red
        view.addSubview(textLabel4)
        textLabel4.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.foregroundColor:UIColor.blue])

背景色

// MARK: - 背景色
        let textLabel5 = UILabel.init(frame: CGRect.init(x: 20, y: 220, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel5.textColor = UIColor.red
        view.addSubview(textLabel5)
        textLabel5.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.backgroundColor:UIColor.green])

阴影

// MARK: - 阴影
        let textLabel6 = UILabel.init(frame: CGRect.init(x: 20, y: 250, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel6.textColor = UIColor.red
        view.addSubview(textLabel6)
        let shadow = NSShadow.init()
        shadow.shadowColor = UIColor.red
        shadow.shadowOffset = CGSize.init(width: 2, height: 2)
        textLabel6.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.foregroundColor:UIColor.red,  NSAttributedStringKey.font:UIFont.systemFont(ofSize:18), NSAttributedStringKey.shadow: shadow])

下划线

// MARK: - 下划线
        let textLabel7 = UILabel.init(frame: CGRect.init(x: 20, y: 280, width: UIScreen.main.bounds.size.width - 40, height: 30))
        textLabel7.textColor = UIColor.red
        view.addSubview(textLabel7)
        textLabel7.attributedText =  NSAttributedString.init(string:str, attributes: [NSAttributedStringKey.foregroundColor:UIColor.purple,  NSAttributedStringKey.font:UIFont.systemFont(ofSize:18), NSAttributedStringKey.underlineStyle:NSUnderlineStyle.styleSingle.rawValue])

Dome

相关文章

  • Swift-富文本使用

    定义公用的属性 修改颜色(可多段) 删除线 文字大小 字体颜色 背景色 阴影 下划线 Dome

  • Swift-富文本

    查看demo 使用介绍 具体实现

  • Swift-富文本封装

    定义字符串 修改颜色 删除线 文字大小 字体颜色 背景色 阴影 下划线 富文本拼接 实现 Dome

  • iOS 富文本属性

    富文本属性的一些简单使用 一、NSAttributedString属性列表: 二、富文本属性设置: 因富文本属性这...

  • iOS:链式编程-富文本

    由于项目经常使用富文本,所以对富文本NSMutableAttributedString进行封装,思路仿照mason...

  • 富文本使用

    H1标题 H2标题 H3标题 H4标题 斜体 加粗 删除线 引用

  • 富文本使用

    有一个需求,如图: 刚开始的时候想到用三个UILabel实现,第一个显示“酒店评分”,设置一个字体,第二个显示“4...

  • js中富文本清除复制内容样式问题

    场景:当我们在使用富文本需要复制富文本内容的时候,却不希望富文本中的样式和标签被一起复制。

  • react-draft-wysiwyg富文本组件

    目录 react-draft-wysiwyg富文本组件 react-draft-wysiwyg富文本组件 组件使用...

  • 7.富文本总结:

    1.前言: 2.富文本使用的案列: 3.自己项目中的使用富文本: 4.IOS之NSMutableAttribute...

网友评论

      本文标题:Swift-富文本使用

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