jsdw_ios/QuickLocation/Core/Extension/UILabel+Extension.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
}
}