image.png
定義公用的屬性
let str = "我不要種田 我要當(dāng)老板"
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])