2020-08-21 04:29:01 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
extension String {
|
2020-08-27 06:04:06 +02:00
|
|
|
private static let HTTPSPrefix = "https://"
|
|
|
|
|
|
|
|
func url() throws -> URL {
|
|
|
|
let url: URL?
|
|
|
|
|
|
|
|
if hasPrefix(Self.HTTPSPrefix) {
|
|
|
|
url = URL(string: self)
|
|
|
|
} else {
|
|
|
|
url = URL(string: Self.HTTPSPrefix + self)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let validURL = url else { throw URLError(.badURL) }
|
|
|
|
|
|
|
|
return validURL
|
|
|
|
}
|
|
|
|
|
2020-08-21 04:29:01 +02:00
|
|
|
func countEmphasizedAttributedString(count: Int, highlighted: Bool = false) -> NSAttributedString {
|
|
|
|
let countRange = (self as NSString).range(of: String.localizedStringWithFormat("%ld", count))
|
|
|
|
|
|
|
|
let attributed = NSMutableAttributedString(
|
|
|
|
string: self,
|
|
|
|
attributes: [
|
|
|
|
.font: UIFont.preferredFont(forTextStyle: .body),
|
|
|
|
.foregroundColor: highlighted ? UIColor.tertiaryLabel : UIColor.secondaryLabel
|
|
|
|
])
|
|
|
|
attributed.addAttributes(
|
|
|
|
[
|
|
|
|
.font: UIFont.preferredFont(forTextStyle: .headline),
|
|
|
|
.foregroundColor: highlighted ? UIColor.secondaryLabel : UIColor.label
|
|
|
|
],
|
|
|
|
range: countRange)
|
|
|
|
|
|
|
|
return attributed
|
|
|
|
}
|
|
|
|
}
|