SweeterSwift/Source/NSAttributedString+Sweeter....

36 lines
1.3 KiB
Swift

//
// NSAttributedString+Sweeter.swift
//
// Created by Yonat Sharon on 2019-02-08.
//
import Foundation
public extension NSAttributedString {
/// Sweeter: Create attributed string from HTML
convenience init?(htmlString: String, defaultAttributes: [NSAttributedString.Key: Any]? = nil) {
guard let data = htmlString.data(using: .utf8) else { return nil }
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue,
.defaultAttributes: defaultAttributes ?? [:],
]
try? self.init(data: data, options: options, documentAttributes: nil)
}
}
public extension NSMutableAttributedString {
/// Sweeter: Make part of the string into a link.
///
/// - Parameters:
/// - url: link address.
/// - anchorText: substring to make into a link.
func link(anchorText: String, url: String) {
guard let urlObject = URL(string: url) else { return }
let anchorRange = mutableString.range(of: anchorText, options: [.caseInsensitive, .diacriticInsensitive, .widthInsensitive])
guard NSNotFound != anchorRange.location else { return }
addAttribute(.link, value: urlObject, range: anchorRange)
}
}