image.png
定义字符串
let str = "我不要种田 我要当老板"
修改颜色
// MARK: - 修改颜色
let textLabel = UILabel.init(frame: CGRect.init(x: 20, y: 100, width: UIScreen.main.bounds.size.width - 40, height: 30))
view.addSubview(textLabel)
textLabel.attributedText = str.hw_toAttribute()
.hw_color(UIColor.gray)
删除线
// MARK: - 删除线
let textLabel2 = UILabel.init(frame: CGRect.init(x: 20, y: 130, width: UIScreen.main.bounds.size.width - 40, height: 30))
view.addSubview(textLabel2)
textLabel2.attributedText = str.hw_toAttribute()
.hw_addMidline(1)
.hw_midlineColor(UIColor.red)
文字大小
// 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 = str.hw_toAttribute()
.hw_fontSize(25)
字体颜色
// MARK: - 字体颜色
let textLabel4 = UILabel.init(frame: CGRect.init(x: 20, y: 190, width: UIScreen.main.bounds.size.width - 40, height: 30))
view.addSubview(textLabel4)
textLabel4.attributedText = str.hw_toAttribute()
.hw_color(UIColor.red)
背景色
// MARK: - 背景色
let textLabel5 = UILabel.init(frame: CGRect.init(x: 20, y: 220, width: UIScreen.main.bounds.size.width - 40, height: 30))
view.addSubview(textLabel5)
textLabel5.attributedText = str.hw_toAttribute()
.hw_backgroundColor(UIColor.blue)
阴影
// MARK: - 阴影
let textLabel6 = UILabel.init(frame: CGRect.init(x: 20, y: 250, width: UIScreen.main.bounds.size.width - 40, height: 30))
view.addSubview(textLabel6)
textLabel6.attributedText = str.hw_toAttribute()
.hw_addShadow()
下划线
// MARK: - 下划线
let textLabel7 = UILabel.init(frame: CGRect.init(x: 20, y: 280, width: UIScreen.main.bounds.size.width - 40, height: 30))
view.addSubview(textLabel7)
textLabel7.attributedText = str.hw_toAttribute()
.hw_addUnderLine(.styleSingle)
.hw_underLineColor(UIColor.yellow)
富文本拼接
// MARK: - 拼接
let textLabel8 = UILabel.init(frame: CGRect.init(x: 20, y: 310, width: UIScreen.main.bounds.size.width - 40, height: 30))
view.addSubview(textLabel8)
textLabel8.attributedText = str.hw_toAttribute()
.hw_addUnderLine(.styleSingle)
.hw_underLineColor(UIColor.yellow).hw_addAttribute("我是拼接的".hw_toAttribute().hw_color(UIColor.red))
实现
import UIKit
public extension NSMutableAttributedString {
/// 获取范围
func hw_allRange() -> NSRange {
return NSMakeRange(0,length)
}
/// 添加中划线
@discardableResult
func hw_addMidline(_ lineHeght: Int) -> NSMutableAttributedString {
addAttributes([.strikethroughStyle:lineHeght], range: hw_allRange())
return self
}
/// 中划线颜色
@discardableResult
func hw_midlineColor(_ color: UIColor) -> NSMutableAttributedString{
addAttributes([.strikethroughColor:color], range: hw_allRange())
return self
}
/// 给文字添加描边
///
/// - Parameter width: 描边宽带
/// - Returns:
@discardableResult
func hw_addStroke(_ width: CGFloat) -> NSMutableAttributedString {
addAttributes([.strokeWidth:width], range: hw_allRange())
return self
}
/// 描边颜色
@discardableResult
func hw_strokeColor(_ color: UIColor) -> NSMutableAttributedString {
addAttributes([.strokeColor:color], range: hw_allRange())
return self
}
/// 添加字间距
@discardableResult
func hw_addSpace(_ space: CGFloat) -> NSMutableAttributedString {
addAttributes([.kern:space], range: hw_allRange())
return self
}
/// 背景色
@discardableResult
func hw_backgroundColor(_ color: UIColor) -> NSMutableAttributedString {
addAttributes([.backgroundColor:color], range: hw_allRange())
return self
}
/// 文字颜色
@discardableResult
public func hw_color(_ color: UIColor) -> NSMutableAttributedString {
addAttributes([.foregroundColor:color], range: hw_allRange())
return self
}
/// 添加下划线
///
/// - Parameter style: <#style description#>
/// - Returns: <#return value description#>
@discardableResult
func hw_addUnderLine(_ style: NSUnderlineStyle) -> NSMutableAttributedString{
addAttributes([.underlineStyle:style.rawValue], range: hw_allRange())
return self
}
/// 下划线颜色
@discardableResult
func hw_underLineColor(_ color: UIColor) -> NSMutableAttributedString{
addAttributes([.underlineColor:color], range: hw_allRange())
return self
}
/// 字体
@discardableResult
func hw_font(_ font: UIFont) -> NSMutableAttributedString{
addAttributes([.font:font], range: hw_allRange())
return self
}
/// 系统字体大小
@discardableResult
func hw_fontSize(_ size: CGFloat)->NSMutableAttributedString{
addAttributes([.font:UIFont.systemFont(ofSize: size)], range: hw_allRange())
return self
}
/// 添加行间距
@discardableResult
func hw_addLineSpace(_ space: CGFloat) -> NSMutableAttributedString {
let style = NSMutableParagraphStyle()
style.lineSpacing = space
style.lineBreakMode = .byCharWrapping
addAttribute(.paragraphStyle, value: style, range: hw_allRange())
return self
}
/// 拼接富文本
@discardableResult
func hw_addAttribute(_ attribute: NSMutableAttributedString) -> NSMutableAttributedString {
append(attribute)
return self
}
/// 添加阴影
@discardableResult
func hw_addShadow(_ shadowOffset:CGSize? = nil ,_ color: UIColor? = nil) -> NSMutableAttributedString {
let shadow = NSShadow.init()
shadow.shadowColor = color == nil ? UIColor.black : color!
shadow.shadowOffset = shadowOffset == nil ? CGSize.init(width: 2, height: 2) : shadowOffset!
addAttributes([NSAttributedStringKey.shadow: shadow], range: hw_allRange())
return self
}
}
public extension String {
/// 字符串转富文本
func hw_toAttribute() -> NSMutableAttributedString {
return NSMutableAttributedString(string: self)
}
}
Dome
网友评论