105 lines
3.8 KiB
Swift
105 lines
3.8 KiB
Swift
//
|
|
// UILabel+Extension.swift
|
|
// HealthyZG
|
|
//
|
|
// Created by 林 on 2020/7/21.
|
|
// Copyright © 2020 Lin. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
extension UILabel {
|
|
/// 删除线
|
|
public func setupStrikethroughStyle(_ withText: String="") {
|
|
guard let text = self.text else { return }
|
|
let attributeText = NSMutableAttributedString(string: text)
|
|
let nsString = NSString(string: text)
|
|
var range = nsString.range(of: text)
|
|
if !withText.isEmpty, text.contains(withText) {
|
|
range = nsString.range(of: withText)
|
|
}
|
|
attributeText.addAttribute(.strikethroughStyle, value: 1, range: range)
|
|
self.attributedText = attributeText
|
|
}
|
|
|
|
/// 设置¥或单位的字体样式
|
|
public func setupMoneyStyle(moneyValue: String, symbol: String, unit: String, fontStyle: UIFont) {
|
|
let nsString = NSString(string: moneyValue)
|
|
let symbolRange = nsString.range(of: symbol)
|
|
let unitRange = nsString.range(of: unit)
|
|
let attString = NSMutableAttributedString(string: moneyValue)
|
|
attString.addAttributes([NSAttributedString.Key.font: fontStyle], range: symbolRange)
|
|
attString.addAttributes([NSAttributedString.Key.font: fontStyle], range: unitRange)
|
|
self.attributedText = attString
|
|
}
|
|
|
|
/// 设置符号(比如¥)字体样式
|
|
public func setupMoneyStyle(symbol: String, fontStyle: UIFont) {
|
|
let text = self.text ?? ""
|
|
let nsString = NSString(string: text)
|
|
let symbolRange = nsString.range(of: symbol)
|
|
let attString = NSMutableAttributedString(string: text)
|
|
attString.addAttributes([NSAttributedString.Key.font: fontStyle], range: symbolRange)
|
|
self.attributedText = attString
|
|
}
|
|
|
|
/// 设置字体样式
|
|
public func setupTextStyle(text: String, font: UIFont, color: UIColor) {
|
|
if let labelText = self.text {
|
|
let nsString = NSString(string: labelText)
|
|
let textRange = nsString.range(of: text)
|
|
let attString = NSMutableAttributedString(string: labelText)
|
|
attString.addAttributes([NSAttributedString.Key.font: font,
|
|
NSAttributedString.Key.foregroundColor: color],
|
|
range: textRange)
|
|
self.attributedText = attString
|
|
}
|
|
}
|
|
|
|
public func setupTextStyle(text: String, color: UIColor) {
|
|
if let labelText = self.text {
|
|
let nsString = NSString(string: labelText)
|
|
let textRange = nsString.range(of: text)
|
|
let attString = NSMutableAttributedString(string: labelText)
|
|
attString.addAttributes([NSAttributedString.Key.foregroundColor: color],
|
|
range: textRange)
|
|
self.attributedText = attString
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UILabel {
|
|
/**
|
|
设置Label字体、颜色
|
|
*/
|
|
public func setupStyle(font: UIFont, textColor: UIColor) {
|
|
self.font = font
|
|
self.textColor = textColor
|
|
}
|
|
}
|
|
|
|
extension UILabel {
|
|
func setLineSpacing(lineSpacing: CGFloat = 0.0) {
|
|
guard let labelText = self.text else { return }
|
|
|
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
paragraphStyle.lineSpacing = lineSpacing
|
|
|
|
let attributedString: NSMutableAttributedString
|
|
if let labelAttributedText = self.attributedText {
|
|
attributedString = NSMutableAttributedString(attributedString: labelAttributedText)
|
|
} else {
|
|
attributedString = NSMutableAttributedString(string: labelText)
|
|
}
|
|
|
|
attributedString.addAttribute(
|
|
.paragraphStyle,
|
|
value: paragraphStyle,
|
|
range: NSMakeRange(0, attributedString.length)
|
|
)
|
|
|
|
self.attributedText = attributedString
|
|
}
|
|
}
|