Bark/Common/String+Extension.swift
2024-12-26 09:34:42 +08:00

56 lines
1.6 KiB
Swift

//
// String+Extension.swift
// Bark
//
// Created by huangfeng on 2018/6/26.
// Copyright © 2018 Fin. All rights reserved.
//
import UIKit
extension String {
// urlurl
func urlEncoded() -> String {
let encodeUrlString = self.addingPercentEncoding(withAllowedCharacters:
.urlQueryAllowed)
return encodeUrlString ?? ""
}
// urlurl
func urlDecoded() -> String {
return self.removingPercentEncoding ?? ""
}
}
// MARK: - NSAttributedString
extension String {
var bold: NSAttributedString {
return NSMutableAttributedString(string: self, attributes: [.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)])
}
var underline: NSAttributedString {
return NSAttributedString(string: self, attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
}
var strikethrough: NSAttributedString {
return NSAttributedString(string: self, attributes: [.strikethroughStyle: NSNumber(value: NSUnderlineStyle.single.rawValue as Int)])
}
var italic: NSAttributedString {
return NSMutableAttributedString(string: self, attributes: [.font: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])
}
func colored(with color: UIColor) -> NSAttributedString {
return NSMutableAttributedString(string: self, attributes: [.foregroundColor: color])
}
}
// MARK: - Format
extension String {
func format(_ arguments: any CVarArg...) -> String {
return String(format: self, arguments)
}
}