Swift-富文本使用

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-富文本使用)