eddiekaiger
SwiftyAttributes
Swift

A Swifty API for attributed strings

Last updated Jun 18, 2026
1.6k
Stars
83
Forks
3
Issues
0
Stars/day
Attention Score
65
Language breakdown
Swift 99.0%
Ruby 0.8%
Objective-C 0.2%
โ–ธ Files click to expand
README

SwiftyAttributes

A Swifty API for attributed strings.

Swift Version Swift Version Carthage compatible CocoaPods Compatible Platform Travis CI codecov.io


With SwiftyAttributes, you can create attributed strings like so:

swift
let fancyString = &quot;Hello World!&quot;.withTextColor(.blue).withUnderlineStyle(.styleSingle)</code></pre>

Alternatively, use the Attribute enum:

swift let fancyString = &quot;Hello World!&quot;.withAttributes([     .backgroundColor(.magenta),     .strokeColor(.orange),     .strokeWidth(1),     .baselineOffset(5.2) ])</code></pre>

You can also easily combine attributed strings using a plus sign:

swift
let fancyString = &quot;Hello&quot;.withFont(.systemFont(ofSize: 12)) + &quot; World!&quot;.withFont(.systemFont(ofSize: 18))</code></pre>

SwiftyAttributes has support for every attribute available in Cocoa and Cocoa Touch.

Requirements

  • iOS 8.0+, macOS 10.11+, watchOS 2.0+, tvOS 9.0+
  • Swift 4.2+
  • Xcode 10.0+

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/eddiekaiger/SwiftyAttributes.git", from: "5.3.0")
]

With CocoaPods

pod 'SwiftyAttributes'

With Carthage

github "eddiekaiger/SwiftyAttributes"

Usage

Initializing attributed strings in SwiftyAttributes can be done several ways:

  • Using the with[Attribute] extensions:
swift
    &quot;Hello World&quot;.withUnderlineColor(.red).withUnderlineStyle(.styleDouble)</code></pre>
  • Using the Attribute enum extensions:
swift
    &quot;Hello World&quot;.withAttributes([.underlineColor(.red), .underlineStyle(.styleDouble)])</code></pre>
  • Using the Attribute enum in an initializer:
swift
    NSAttributedString(string: &quot;Hello World&quot;, swiftyAttributes: [.kern(5), .backgroundColor(.gray)])</code></pre>
    
You can retrieve the attribute at a specific location using the built-in NSAttributedString.Key enum:
swift
let attr: Attribute? = myAttributedString.swiftyAttribute(.shadow, at: 5)</code></pre>

Several API methods are provided to use these new enums as well as Swift's Range type instead of NSRange. Some of the method signatures include:

swift
extension NSMutableAttributedString {
    func addAttributes(_ attributes: [Attribute], range: Range&lt;Int&gt;)
    func addAttributes(_ attributes: [Attribute], range: NSRange)
    func setAttributes(_ attributes: [Attribute], range: Range&lt;Int&gt;)
    func setAttributes(_ attributes: [Attribute], range: NSRange)
    func replaceCharacters(in range: Range&lt;Int&gt;, with str: String)
    func replaceCharacters(in range: Range&lt;Int&gt;, with attrString: NSAttributedString)
    func deleteCharacters(in range: Range&lt;Int&gt;)
    func removeAttribute(_ name: NSAttributedStringKey, range: Range&lt;Int&gt;)
}

extension NSAttributedString { convenience init(string str: String, swiftyAttributes: [Attribute]) func withAttributes(_ attributes: [Attribute]) -&gt; NSMutableAttributedString func withAttribute(_ attribute: Attribute) -&gt; NSMutableAttributedString func attributedSubstring(from range: Range&lt;Int&gt;) -&gt; NSAttributedString func swiftyAttribute(_ attrName: NSAttributedStringKey, at location: Int, effectiveRange range: NSRangePointer? = nil) -&gt; Attribute? func swiftyAttributes(in range: Range&lt;Int&gt;, options: NSAttributedString.EnumerationOptions = []) -&gt; [([Attribute], Range&lt;Int&gt;)] func enumerateSwiftyAttributes(in enumerationRange: Range&lt;Int&gt;, options: NSAttributedString.EnumerationOptions = [], using block: ( attrs: [Attribute], range: Range&lt;Int&gt;, _ stop: UnsafeMutablePointer&lt;ObjCBool&gt;) -&gt; Void) func enumerateSwiftyAttribute( attrName: NSAttributedStringKey, in enumerationRange: Range&lt;Int&gt;, options: NSAttributedString.EnumerationOptions = [], using block: ( value: Any?, range: Range&lt;Int&gt;, stop: UnsafeMutablePointer&lt;ObjCBool&gt;) -&gt; Void) }

extension String { var attributedString: NSMutableAttributedString func withAttributes(_ attributes: [Attribute]) -&gt; NSMutableAttributedString func withAttribute(_ attribute: Attribute) -&gt; NSMutableAttributedString }

// ... and more!</code></pre>

Support

For questions, support, and suggestions, please open up an issue.

License

SwiftyAttributes is available under the MIT license. See the LICENSE file for more info.

ยฉ 2026 GitRepoTrend ยท eddiekaiger/SwiftyAttributes ยท Updated daily from GitHub