Add NSAttributedString extensions
This commit is contained in:
parent
ee4e321e40
commit
c7fe7b20e0
|
@ -14,6 +14,12 @@ This version also introduces a new `StoreKit` namespace with handy utils for man
|
|||
* `Bundle` has a new `displayName` extension.
|
||||
* `Collection` has new `asyncCompactMap` and `asyncMap` extensions.
|
||||
* `Date` has a new `components` extension for retrieving year, month, hour etc.
|
||||
* `NSAttributedString` has a new `init(keyedArchiveData:)` that can initialize an attributed string from `NSKeyedArchiver` generated data.
|
||||
* `NSAttributedString` has a new `init(plainText:)` that can initialize an attributed string from plain .utf8 text data.
|
||||
* `NSAttributedString` has a new `init(rtfData:)` that can initialize an attributed string from RTF data.
|
||||
* `NSAttributedString` has a new `getKeyedArchiveData()` function that can be used to generate RTF formatted data from an attributed string.
|
||||
* `NSAttributedString` has a new `getPlainTextData()` function that can be used to generate plain .utf8 formatted text data from an attributed string.
|
||||
* `NSAttributedString` has a new `getRtfData()` function that can be used to generate RTF formatted data from an attributed string.
|
||||
* `String` has new `boolValue` extension.
|
||||
|
||||
* `StoreService` is a new protocol for managing StoreKit products and purchases.
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
//
|
||||
// NSAttributedString+Archive.swift
|
||||
// SwiftKit
|
||||
//
|
||||
// Created by Daniel Saidi on 2021-11-22.
|
||||
// Copyright © 2021 Daniel Saidi. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension NSAttributedString {
|
||||
|
||||
/**
|
||||
Try to create an attributed string with `data` that was
|
||||
created with an `NSKeyedArchiver`.
|
||||
*/
|
||||
convenience init?(keyedArchiveData data: Data) throws {
|
||||
let res = try NSKeyedUnarchiver.unarchivedObject(
|
||||
ofClass: NSAttributedString.self,
|
||||
from: data)
|
||||
guard let string = res else { return nil }
|
||||
self.init(attributedString: string)
|
||||
}
|
||||
|
||||
/**
|
||||
Try to generate `NSKeyedArchiver` data from the string.
|
||||
*/
|
||||
func getKeyedArchiveData() throws -> Data {
|
||||
try NSKeyedArchiver.archivedData(
|
||||
withRootObject: self,
|
||||
requiringSecureCoding: false)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
//
|
||||
// NSAttributedString+Rtf.swift
|
||||
// SwiftKit
|
||||
//
|
||||
// Created by Daniel Saidi on 2021-11-22.
|
||||
// Copyright © 2021 Daniel Saidi. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension NSAttributedString {
|
||||
|
||||
/**
|
||||
Try to create an attributed string with `data` that has
|
||||
RTF formatted string content.
|
||||
|
||||
This extension aims to simplify the chore of creating a
|
||||
proper attributed string from RTF data, since the Swift
|
||||
api:s are old and requires a lot or bridging.
|
||||
*/
|
||||
convenience init(rtfData data: Data) throws {
|
||||
let docTypeKey = NSAttributedString.DocumentReadingOptionKey.documentType
|
||||
let rtfDocument = NSAttributedString.DocumentType.rtf
|
||||
var attributes = [docTypeKey: rtfDocument] as NSDictionary?
|
||||
try self.init(
|
||||
data: data,
|
||||
options: [.characterEncoding: String.Encoding.utf8.rawValue],
|
||||
documentAttributes: &attributes)
|
||||
}
|
||||
|
||||
/**
|
||||
Try to generate RTF data from the attributed string.
|
||||
*/
|
||||
func getRtfData() throws -> Data {
|
||||
let docTypeKey = NSAttributedString.DocumentAttributeKey.documentType
|
||||
let rtfDocument = NSAttributedString.DocumentType.rtf
|
||||
let attributes = [docTypeKey: rtfDocument]
|
||||
let data = try data(
|
||||
from: NSRange(location: 0, length: length),
|
||||
documentAttributes: attributes)
|
||||
return data
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
//
|
||||
// NSAttributedString+Text.swift
|
||||
// SwiftKit
|
||||
//
|
||||
// Created by Daniel Saidi on 2021-11-22.
|
||||
// Copyright © 2021 Daniel Saidi. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension NSAttributedString {
|
||||
|
||||
/**
|
||||
Try to create an attributed string with `data` that has
|
||||
plain, .utf8 encoded string content.
|
||||
*/
|
||||
convenience init?(plainTextData data: Data) throws {
|
||||
let decoded = String(data: data, encoding: .utf8)
|
||||
guard let string = decoded else { return nil }
|
||||
let attributed = NSAttributedString(string: string)
|
||||
self.init(attributedString: attributed)
|
||||
}
|
||||
|
||||
/**
|
||||
Try to generate plain text data from the string.
|
||||
*/
|
||||
func getPlainTextData() throws -> Data {
|
||||
guard let data = string.data(using: .utf8) else {
|
||||
throw NSAttributedStringPlainTextError
|
||||
.invalidPlainTextData(inString: string)
|
||||
}
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
public enum NSAttributedStringPlainTextError: Error {
|
||||
|
||||
case invalidPlainTextData(inString: String)
|
||||
}
|
Loading…
Reference in New Issue