sty

Stylized

Modern NSAttributedString

Showing:

Popularity

Downloads/wk

0

GitHub Stars

61

Maintenance

Last Commit

4yrs ago

Contributors

3

Package

Dependencies

0

License

MIT

Categories

Readme

stylize

Build Status

A funcitonal wrapper of NSAttributedString for easy string styling

Contents

Why does NSAttributedString need a wrapper?

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.

Integration

Cocoapods

Add pod "Stylized" to your Podfile

Manual

iOS 8+

  1. Add Stylize to you project as a submodule using git submodule add https://github.com/alexfish/stylize.git
  2. Open the Stylize folder & drag Stylize.xcodeproj into your project tree
  3. Add Stylize.framework to your target's Link Binary with Libraries Build Phase
  4. Import Stylize with import Stylize and you're ready to go

Usage

let string        = NSAttributedString(string: "Hello World")
let style         = Stylize.foreground(UIColor.redColor())
let styledString  = style(string)

Substrings

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)

Composing Styles

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)

Available Attributes

AttributeFunction
NSUnderlineStyleAttributeNameunderline(style: NSUnderlineStyle)
NSUnderlineColorAttributeNameunderline(color: UIColor)
NSForegroundColorAttributeNameforeground(color: UIColor)
NSBackgroundColorAttributeNamebackground(color: UIColor
NSStrikethroughStyleAttributeNamestrikethrough(style: NSUnderlineStyle)
NSStrikethroughColorAttributeNamestrikethrough(color: UIColor)
NSLinkAttributeNamelink(url: NSURL)
NSParagraphStyleAttributeNameparagraph(style: NSParagraphStyle)
NSKernAttributeNamekern(points: NSNumber)
NSBaselineOffsetAttributeNamebaseline(offset: NSNumber)
NSShadowAttributeNameshadow(shadow: NSShadow)
NSStrokeWidthAttributeNamestroke(width: NSNumber)
NSStrokeColorAttributeNamestroke(color: UIColor)
NSTextEffectAttributeNameletterpress()
NSFontAttributeNamefont(font: UIFont)
NSLigatureAttributeNameligatures(enabled: Bool)
NSObliquenessAttributeNameobliqueness(skew: NSNumber)
NSAttachmentAttributeNameattachment(attachement: NSTextAttachment)
NSExpansionAttributeNameexpand(log: NSNumber)
NSWritingDirectionAttributeNamedirection(direction: WritingDirection)

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial