ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - eddiekaiger/SwiftyAttributes: A Swifty API for attributed strings
A Swifty API for attributed strings. Contribute to eddiekaiger/SwiftyAttributes development by creating an account on GitHub.
Visit Site

GitHub - eddiekaiger/SwiftyAttributes: A Swifty API for attributed strings

GitHub - eddiekaiger/SwiftyAttributes: A Swifty API for attributed strings

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:

let fancyString = "Hello World!".withTextColor(.blue).withUnderlineStyle(.styleSingle)

Alternatively, use the Attribute enum:

let fancyString = "Hello World!".withAttributes([
    .backgroundColor(.magenta),
    .strokeColor(.orange),
    .strokeWidth(1),
    .baselineOffset(5.2)
])

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

let fancyString = "Hello".withFont(.systemFont(ofSize: 12)) + " World!".withFont(.systemFont(ofSize: 18))

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:

    "Hello World".withUnderlineColor(.red).withUnderlineStyle(.styleDouble)
    
  • Using the Attribute enum extensions:

    "Hello World".withAttributes([.underlineColor(.red), .underlineStyle(.styleDouble)])
    
  • Using the Attribute enum in an initializer:

    NSAttributedString(string: "Hello World", swiftyAttributes: [.kern(5), .backgroundColor(.gray)])
    

You can retrieve the attribute at a specific location using the built-in NSAttributedString.Key enum:

let attr: Attribute? = myAttributedString.swiftyAttribute(.shadow, at: 5)

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:

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

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

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

// ... and more!

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.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory