CocoaPods trunk is moving to be read-only. Read more on the blog, there are 12 months to go.
| TestsTested | ✗ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Sep 2017 |
| SwiftSwift Version | 4.0 |
| SPMSupports SPM | ✗ |
Maintained by Alex Fish.
A funcitonal wrapper of NSAttributedString for easy string styling
Styling strings with NSAttributedString requires a lot of painful and ugly boiler plate code, for example changing the color of a substring and underlining it requires:
let string = NSMutableAttributedString(string: "Hello")
string.addAttribute(NSforegroundAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 5))
string.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, string.length))Ouch!
This quickly builds into a giant chunk of code that is a pain to read and maintain. Using stylize our code looks like this:
let string = NSAttributedString(string: "Hello World")
let foregroundStyle = Stylize.foreground(UIColor.redColor(), range: NSMakeRange(0, 5))
let underlineStyle = Stylize.underline(NSUnderlineStyle.StyleSingle)
let style = Stylize.compose(foregroundStyle, underlineStyle)
let styledString = style(string)That's better.
git submodule add https://github.com/alexfish/stylize.git
Stylize folder & drag Stylize.xcodeproj into your project treeStylize.framework to your target's Link Binary with Libraries Build Phaseimport Stylize and you're ready to golet string = NSAttributedString(string: "Hello World")
let style = Stylize.foreground(UIColor.redColor())
let styledString = style(string)By default styles will be applied to the entire string, if you need to apply a style to a subsstring an optional range paramater is available for each style:
let string = NSAttributedString(string: "Hello World")
let style = Stylize.foreground(UIColor.redColor(), range: NSMakeRange(0, 5))
let styledString = style(string)stylize has a compose function that can compose a style from multiple styles
let string = NSAttributedString(string: "Hello World")
let foregroundStyle = Stylize.foreground(UIColor.redColor())
let backgroundStyle = Stylize.background(UIColor.orangeColor())
let underlineStyle = Stylize.underline(NSUnderlineStyle.StyleSingle)
let style = Stylize.compose(foregroundStyle, backgroundStyle, underlineStyle)
let styledString = style(string)| Attribute | Function |
|---|---|
| NSUnderlineStyleAttributeName | underline(style: NSUnderlineStyle) |
| NSUnderlineColorAttributeName | underline(color: UIColor) |
| NSForegroundColorAttributeName | foreground(color: UIColor) |
| NSBackgroundColorAttributeName | background(color: UIColor |
| NSStrikethroughStyleAttributeName | strikethrough(style: NSUnderlineStyle) |
| NSStrikethroughColorAttributeName | strikethrough(color: UIColor) |
| NSLinkAttributeName | link(url: NSURL) |
| NSParagraphStyleAttributeName | paragraph(style: NSParagraphStyle) |
| NSKernAttributeName | kern(points: NSNumber) |
| NSBaselineOffsetAttributeName | baseline(offset: NSNumber) |
| NSShadowAttributeName | shadow(shadow: NSShadow) |
| NSStrokeWidthAttributeName | stroke(width: NSNumber) |
| NSStrokeColorAttributeName | stroke(color: UIColor) |
| NSTextEffectAttributeName | letterpress() |
| NSFontAttributeName | font(font: UIFont) |
| NSLigatureAttributeName | ligatures(enabled: Bool) |
| NSObliquenessAttributeName | obliqueness(skew: NSNumber) |
| NSAttachmentAttributeName | attachment(attachement: NSTextAttachment) |
| NSExpansionAttributeName | expand(log: NSNumber) |
| NSWritingDirectionAttributeName | direction(direction: WritingDirection) |