Skip to content

Instantly share code, notes, and snippets.

@fmo91
Created November 18, 2015 17:59
Show Gist options
  • Save fmo91/f570309c18247dd128f5 to your computer and use it in GitHub Desktop.
Save fmo91/f570309c18247dd128f5 to your computer and use it in GitHub Desktop.
//
// FOAttributedStringBuilder.swift
//
// Created by Fernando Ortiz on 18/11/15.
// Copyright © 2015 Fernando Ortiz. All rights reserved.
//
import Foundation
class FOAttributedStringBuilder {
// MARK: - Private
private var currentStringSpace = ""
private var currentAttributesSpace = [String: AnyObject]()
private var attributedString = NSMutableAttributedString()
// MARK: - Init
init() {}
// MARK: - Append methods
func append(string: String) -> FOAttributedStringBuilder {
self.currentStringSpace = string
return self
}
// MARK: - Convenience methods
/**
Adds an space in the attributed mutable string.
*/
func addSpace() -> FOAttributedStringBuilder {
return self.append(" ")
.and()
}
func setAttributesManually(attributes: [String: AnyObject]) -> FOAttributedStringBuilder {
self.currentAttributesSpace = attributes
return self
}
// MARK: - NSMutableAttributedString getters
func finish() -> NSMutableAttributedString {
return self.attributedString
}
// MARK: - Attributes setters
func withAttachment(attachment: NSTextAttachment) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(attachment, forKey: NSAttachmentAttributeName)
return self
}
func withBackgroundColor(color: UIColor) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(color, forKey: NSBackgroundColorAttributeName)
return self
}
func withBaselineOffset(baselineOffset: Float) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(baselineOffset, forKey: NSBaselineOffsetAttributeName)
return self
}
func withFont(font: UIFont) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(font, forKey: NSFontAttributeName)
return self
}
func withTextColor(color: UIColor) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(color, forKey: NSForegroundColorAttributeName)
return self
}
func withKern(kern: Float) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(kern, forKey: NSKernAttributeName)
return self
}
func withLigature(ligature: Int) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(ligature, forKey: NSLigatureAttributeName)
return self
}
func withParagraphStyle(style: NSParagraphStyle) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(style, forKey: NSParagraphStyleAttributeName)
return self
}
func withUnderlineStyle(style: NSUnderlineStyle) -> FOAttributedStringBuilder {
currentAttributesSpace.updateValue(style.rawValue, forKey: NSUnderlineStyleAttributeName)
return self
}
// MARK: - Flux management
func and() -> FOAttributedStringBuilder {
let stringToAppend = NSMutableAttributedString(string: currentStringSpace, attributes: currentAttributesSpace)
attributedString.appendAttributedString(stringToAppend)
self.currentStringSpace = ""
self.currentAttributesSpace = [String: AnyObject]()
return self
}
}
@fmo91
Copy link
Author

fmo91 commented Nov 18, 2015

for example

self.lblNotification.attributedText =
FOAttributedStringBuilder()
.append(username)
.withTextColor(HHAColorPalette.notificationLabelColor)
.and()
.addSpace()
.append(notificationDescription)
.withTextColor(HHAColorPalette.notificationGrayColor)
.and()
.addSpace()
.append(dateDescription)
.withTextColor(HHAColorPalette.darkGrayColor)
.and()
.finish()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment