Skip to content

Instantly share code, notes, and snippets.

@0xLeif
Created December 14, 2019 04:15
Show Gist options
  • Save 0xLeif/18c75aabcfcda3b8e01ef3512426597c to your computer and use it in GitHub Desktop.
Save 0xLeif/18c75aabcfcda3b8e01ef3512426597c to your computer and use it in GitHub Desktop.
//
// NSMutableAttributedString+Swift.swift
//
//
// Created by Zach Eriksen on 12/11/19.
//
import Foundation
public typealias AttributedString = NSMutableAttributedString
public typealias AttributedStringKey = NSAttributedString.Key
public typealias StringAttributes = [AttributedStringKey: Any]
public extension StringAttributes {
init(_ attributes: () -> [AttributedStringKey: Any]) {
self = attributes()
}
init(for key: AttributedStringKey, value: Any) {
self = [key: value]
}
@discardableResult
mutating func add(key: AttributedStringKey, value: Any) -> StringAttributes {
self[key] = value
return self
}
}
public extension NSMutableAttributedString {
@discardableResult
func set(attributes: StringAttributes, range: ClosedRange<Int>) -> Self {
self.setAttributes(attributes, range: NSRange(location: range.lowerBound, length: range.upperBound))
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment