Last active
April 1, 2016 16:40
-
-
Save mmcbrear/f07be3049b3b565e05bbe120c2449362 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SwiftAttributedString: NSObject { | |
private var attributedString = NSMutableAttributedString() | |
private var attributes = [String: AnyObject]() | |
private var currentString = "" | |
private var currentRange = NSRange() | |
private var paragraphStyle = NSMutableParagraphStyle() | |
init(string str: String) { | |
super.init() | |
attributedString = NSMutableAttributedString(string: str) | |
currentString = str | |
currentRange = NSMakeRange(0, currentString.length) | |
} | |
func append(str: String) -> SwiftAttributedString { | |
currentString = str | |
currentRange = NSMakeRange(currentRange.length + currentRange.location, str.length) | |
attributedString.appendAttributedString(NSAttributedString(string: str, attributes: attributes)) | |
return self | |
} | |
func Color(value: UIColor) -> SwiftAttributedString { | |
return applyAttribute(NSForegroundColorAttributeName, value: value) | |
} | |
func BgColor(value: UIColor) -> SwiftAttributedString { | |
return applyAttribute(NSBackgroundColorAttributeName, value: value) | |
} | |
func Font(value: UIFont) -> SwiftAttributedString { | |
return applyAttribute(NSFontAttributeName, value: value) | |
} | |
func Shadow(value: NSShadow) -> SwiftAttributedString { | |
return applyAttribute(NSShadowAttributeName, value: value) | |
} | |
func StrikeThrough(value: Int) -> SwiftAttributedString { | |
return applyAttribute(NSStrikethroughStyleAttributeName, value: value) | |
} | |
func Underline(value: Int) -> SwiftAttributedString { | |
return applyAttribute(NSUnderlineStyleAttributeName, value: value) | |
} | |
func Stroke(width: Int) -> SwiftAttributedString { | |
return applyAttribute(NSStrokeWidthAttributeName, value: width) | |
} | |
func Kerning(value: Double) -> SwiftAttributedString { | |
return applyAttribute(NSKernAttributeName, value: value) | |
} | |
func LineSpacing(value: CGFloat) -> SwiftAttributedString { | |
paragraphStyle.lineSpacing = value | |
return applyAttribute(NSParagraphStyleAttributeName, value: paragraphStyle) | |
} | |
func string() -> NSAttributedString { | |
return attributedString | |
} | |
internal func applyAttribute(attribute: String, value: AnyObject) -> SwiftAttributedString { | |
attributes[attribute] = value | |
attributedString.addAttributes(attributes, range: currentRange) | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment