
/// 效果一:更改不同颜色
func differentColors() {
let attrStr = NSMutableAttributedString.init(string: "效果一:花开堪折直须折 莫待无花空折枝")
// 字符串下标[0]开始,长度为3
attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value:UIColor.orange, range:NSRange.init(location:0, length: 3))
// 字符串下标[0]开始,长度4
attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value:UIColor.red, range:NSRange.init(location:9, length: 4))
label1?.attributedText = attrStr
}
/// 效果二:设置背景颜色 & 字体大小
func fontSizeBackground() {
label2?.attributedText = NSAttributedString.init(string:"效果二:谁道人生无再少 门前流水尚能西", attributes: [NSAttributedString.Key.backgroundColor:UIColor.cyan, NSAttributedString.Key.font:UIFont.systemFont(ofSize:16)])
}
/// 效果三:设置阴影
func shadow() {
let shadow = NSShadow.init()
shadow.shadowColor = UIColor.red
// 阴影偏移距离
shadow.shadowOffset = CGSize.init(width: 2, height: 2)
label3?.attributedText = NSAttributedString.init(string:"效果三:人生一世 草木一春", attributes: [NSAttributedString.Key.foregroundColor:UIColor.red, NSAttributedString.Key.font:UIFont.systemFont(ofSize:18), NSAttributedString.Key.shadow: shadow])
}
/// 效果四:下划线 & 字体大小 & 字体颜色
func underlineFontSizeColor() {
label4?.attributedText = NSAttributedString.init(string:"效果四:乘风破浪会有时 直挂云帆济沧海", attributes: [NSAttributedString.Key.foregroundColor:UIColor.purple, NSAttributedString.Key.font:UIFont.systemFont(ofSize:18), NSAttributedString.Key.underlineStyle:NSUnderlineStyle.single.rawValue])
}
下载Demo地址:Github
【原创:iOS第九层】
网友评论