Blame view
Pods/SwiftyAttributes/README.md
5.43 KB
|
d774f0637
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# SwiftyAttributes
#### *Swift extensions that make it a breeze to work with attributed strings.*

[](https://github.com/Carthage/Carthage)
[](https://img.shields.io/cocoapods/v/SwiftyAttributes.svg)
[](http://cocoapods.org/pods/SwiftyAttributes)
[](https://travis-ci.org/eddiekaiger/SwiftyAttributes.svg?branch=master)
[](http://codecov.io/github/eddiekaiger/SwiftyAttributes/coverage.svg?branch=master)
---
The **original** way to create an attributed string in Swift:
````swift
let attributes: [String: Any] = [
NSForegroundColorAttributeName: UIColor.blue,
NSUnderlineStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue)
]
let fancyString = NSAttributedString(string: "Hello World!", attributes: attributes)
````
With **SwiftyAttributes**, you can write the same thing like this:
````swift
let fancyString = "Hello World!".withTextColor(.blue).withUnderlineStyle(.styleSingle)
````
Alternatively, **SwiftyAttributes** provides an `Attribute` enum:
````swift
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:
````swift
let fancyString = "Hello".withFont(.systemFont(ofSize: 12)) + " World!".withFont(.systemFont(ofSize: 18))
````
**SwiftyAttributes** has support for *every* attribute that can be used in iOS.
# Requirements
* iOS 8.0+
# Installation
### With CocoaPods
#### For **Swift 3**:
`pod 'SwiftyAttributes'`
> For **Swift 2.3**:
> `pod 'SwiftyAttributes', '1.1'`
> If using Xcode 8, you may need to add this to end of your Podfile:
> ```swift
> post_install do |installer|
> installer.pods_project.targets.each do |target|
> target.build_configurations.each do |config|
> config.build_settings["SWIFT_VERSION"] = "2.3"
> end
> end
> end
> ```
### With Carthage
#### For **Swift 3**:
`github "eddiekaiger/SwiftyAttributes"`
> For **Swift 2.3**:
> `github "eddiekaiger/SwiftyAttributes" == 1.1.1`
# Usage
Initializing attributed strings in `SwiftyAttributes` can be done several ways:
- Using the `with[Attribute]` extensions:
````swift
"Hello World".withUnderlineColor(.red).withUnderlineStyle(.styleDouble)
````
- Using the `Attribute` enum extensions:
````swift
"Hello World".withAttributes([.underlineColor(.red), .underlineStyle(.styleDouble)])
````
- Using the `Attribute` enum in an initializer:
````swift
NSAttributedString(string: "Hello World", attributes: [.kern(5), .backgroundColor(.gray)])
````
You can retrieve the attribute at a specific location using an attribute name from the `Attribute.Name` enum:
````swift
let attr: Attribute? = myAttributedString.attribute(.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:
````swift
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: Attribute.Name, range: Range<Int>)
}
extension NSAttributedString {
convenience init(string str: String, attributes: [Attribute])
func withAttributes(_ attributes: [Attribute]) -> NSMutableAttributedString
func withAttribute(_ attribute: Attribute) -> NSMutableAttributedString
func attributedSubstring(from range: Range<Int>) -> NSAttributedString
func attribute(_ attrName: Attribute.Name, at location: Int, effectiveRange range: NSRangePointer? = nil) -> Attribute?
func attributes(in range: Range<Int>, options: NSAttributedString.EnumerationOptions = []) -> [([Attribute], Range<Int>)]
func enumerateAttributes(in enumerationRange: Range<Int>, options: NSAttributedString.EnumerationOptions = [], using block: (_ attrs: [Attribute], _ range: Range<Int>, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void)
func enumerateAttribute(_ attrName: Attribute.Name, 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.
|