Created
December 20, 2015 00:18
-
-
Save hyperspacemark/f0ba52e459610306ebd3 to your computer and use it in GitHub Desktop.
This file contains 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
struct AttributedStringBuilder { | |
// MARK: - Types | |
enum Attribute { | |
case Font(UIFont) | |
case ParagraphStyle(NSParagraphStyle) | |
case ForegroundColor(UIColor) | |
case BackgroundColor(UIColor) | |
case Ligature(UInt) | |
case Kerning(Double) | |
case StrikethroughStyle(NSNumber) | |
case UnderlineStyle(NSNumber) | |
case StrokeColor(UIColor) | |
case StrokeWidth(Double) | |
case Shadow(NSShadow) | |
case TextEffect(String) | |
case Attachment(NSTextAttachment) | |
case Link(NSURL) | |
case BaselineOffset(Double) | |
case UnderlineColor(UIColor) | |
case StrikethroughColor(UIColor) | |
case Obliqueness(Double) | |
case Expansion(Double) | |
case WritingDirections([UInt]) | |
case VerticalGlyphForm(UInt) | |
var value: AnyObject { | |
switch self { | |
case let .Font(font): return font | |
case let .ParagraphStyle(paragraphStyle): return paragraphStyle | |
case let .ForegroundColor(foregroundColor): return foregroundColor | |
case let .BackgroundColor(backgroundColor): return backgroundColor | |
case let .Ligature(ligature): return ligature | |
case let .Kerning(kerning): return kerning | |
case let .StrikethroughStyle(strikethroughStyle): return strikethroughStyle | |
case let .UnderlineStyle(underlineStyle): return underlineStyle | |
case let .StrokeColor(strokeColor): return strokeColor | |
case let .StrokeWidth(strokeWidth): return strokeWidth | |
case let .Shadow(shadow): return shadow | |
case let .TextEffect(textEffect): return textEffect | |
case let .Attachment(attachment): return attachment | |
case let .Link(link): return link | |
case let .BaselineOffset(baselineOffset): return baselineOffset | |
case let .UnderlineColor(underlineColor): return underlineColor | |
case let .StrikethroughColor(strikethroughColor): return strikethroughColor | |
case let .Obliqueness(obliqueness): return obliqueness | |
case let .Expansion(expansion): return expansion | |
case let .WritingDirections(writingDirections): return writingDirections | |
case let .VerticalGlyphForm(verticalGlyphForm): return verticalGlyphForm | |
} | |
} | |
var name: String { | |
switch self { | |
case .Font(_): return NSFontAttributeName | |
case .ParagraphStyle(_): return NSParagraphStyleAttributeName | |
case .ForegroundColor(_): return NSForegroundColorAttributeName | |
case .BackgroundColor(_): return NSBackgroundColorAttributeName | |
case .Ligature(_): return NSLigatureAttributeName | |
case .Kerning(_): return NSKernAttributeName | |
case .StrikethroughStyle(_): return NSStrikethroughStyleAttributeName | |
case .UnderlineStyle(_): return NSUnderlineStyleAttributeName | |
case .StrokeColor(_): return NSStrokeColorAttributeName | |
case .StrokeWidth(_): return NSStrokeWidthAttributeName | |
case .Shadow(_): return NSShadowAttributeName | |
case .TextEffect(_): return NSTextEffectAttributeName | |
case .Attachment(_): return NSAttachmentAttributeName | |
case .Link(_): return NSLinkAttributeName | |
case .BaselineOffset(_): return NSBaselineOffsetAttributeName | |
case .UnderlineColor(_): return NSUnderlineColorAttributeName | |
case .StrikethroughColor(_): return NSStrikethroughColorAttributeName | |
case .Obliqueness(_): return NSObliquenessAttributeName | |
case .Expansion(_): return NSExpansionAttributeName | |
case .WritingDirections(_): return NSWritingDirectionAttributeName | |
case .VerticalGlyphForm(_): return NSVerticalGlyphFormAttributeName | |
} | |
} | |
} | |
// MARK: - Initialization | |
init(string: String) { | |
workingString = NSMutableAttributedString(string: string) | |
fullRange = NSRange(location: 0, length: NSString(string: workingString.string).length) | |
} | |
// MARK: - Methods | |
func set(attribute: Attribute, range: NSRange? = nil) -> AttributedStringBuilder { | |
addValue(attribute.value, forAttribute: attribute.name, onRange: range) | |
return self | |
} | |
func build() -> NSAttributedString { | |
return NSAttributedString(attributedString: workingString) | |
} | |
// MARK: - Private | |
private let workingString: NSMutableAttributedString | |
private let fullRange: NSRange | |
private func addValue(value: AnyObject, forAttribute attribute: String, onRange range: NSRange? = nil) { | |
workingString.addAttribute(attribute, value: value, range: range ?? fullRange) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment