Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Created July 8, 2016 05:13
Show Gist options
  • Save nathanborror/7a4ddff9ca0d0cb2d95f18973493ff67 to your computer and use it in GitHub Desktop.
Save nathanborror/7a4ddff9ca0d0cb2d95f18973493ff67 to your computer and use it in GitHub Desktop.
extension NSMutableAttributedString {
func add(attribute: Attribute, range: NSRange) throws {
guard (range.location + range.length) <= length else {
throw AttributedStringError.InvalidRange(range: range)
}
addAttribute(attribute.name, value: attribute.value, range: range)
}
}
enum AttributedStringError: ErrorType {
case InvalidRange(range: NSRange)
}
enum Attribute {
case Font(UIFont)
case ForegroundColor(UIColor)
case BackgroundColor(UIColor)
case ParagraphStyle(NSParagraphStyle)
case Ligatures(Bool)
case Kerning(Float)
case StrikethroughStyle(Int)
case StrikethroughColor(UIColor)
case UnderlineStyle(Int)
case UnderlineColor(UIColor)
case StrokeWidth(Float)
case StrokeColor(UIColor)
case Shadow(NSShadow)
case TextEffect(String)
case Attachment(NSTextAttachment)
case Link(NSURL)
case BaselineOffset(Float)
case Obliqueness(Float)
case Expansion(Float)
var name: String {
switch self {
case .Font: return NSFontAttributeName
case .ForegroundColor: return NSForegroundColorAttributeName
case .BackgroundColor: return NSBackgroundColorAttributeName
case .ParagraphStyle: return NSParagraphStyleAttributeName
case .Ligatures: return NSLigatureAttributeName
case .Kerning: return NSKernAttributeName
case .StrikethroughStyle: return NSStrikethroughStyleAttributeName
case .UnderlineStyle: return NSUnderlineStyleAttributeName
case .UnderlineColor: return NSUnderlineColorAttributeName
case .StrokeWidth: return NSStrokeWidthAttributeName
case .StrokeColor: return NSStrokeColorAttributeName
case .Shadow: return NSShadowAttributeName
case .TextEffect: return NSTextEffectAttributeName
case .Attachment: return NSAttachmentAttributeName
case .Link: return NSLinkAttributeName
case .BaselineOffset: return NSBaselineOffsetAttributeName
case .StrikethroughColor: return NSStrikethroughColorAttributeName
case .Obliqueness: return NSObliquenessAttributeName
case .Expansion: return NSExpansionAttributeName
}
}
var value: AnyObject {
switch self {
case .Font(let font): return font
case .ForegroundColor(let color): return color
case .BackgroundColor(let color): return color
case .ParagraphStyle(let style): return style
case .Ligatures(let val): return NSNumber(bool: val)
case .Kerning(let val): return NSNumber(float: val)
case .StrikethroughStyle(let val): return NSNumber(integer: val)
case .StrikethroughColor(let color): return color
case .UnderlineStyle(let val): return NSNumber(integer: val)
case .UnderlineColor(let color): return color
case .StrokeWidth(let val): return NSNumber(float: val)
case .StrokeColor(let color): return color
case .Shadow(let shadow): return shadow
case .TextEffect(let effect): return effect
case .Attachment(let attachment): return attachment
case .Link(let url): return url
case .BaselineOffset(let val): return NSNumber(float: val)
case .Obliqueness(let val): return NSNumber(float: val)
case .Expansion(let val): return NSNumber(float: val)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment