Skip to content

Instantly share code, notes, and snippets.

@tgroshon
Created October 8, 2014 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgroshon/7b0c862edf80bd44d7cb to your computer and use it in GitHub Desktop.
Save tgroshon/7b0c862edf80bd44d7cb to your computer and use it in GitHub Desktop.
Extending NSAttributedString to have a "join" method like String.
//
// Extensions.swift
//
// Created by Thomas Groshong on 10/8/14.
// Copyright (c) 2014 IS 543. All rights reserved.
//
import Foundation
// Simple usage to join with spaces:
// let arrayOfAttributedStrings = [NSAttributedString(string: "Bob"), NSAttributedString(string: "John")]
// NSAttributedString(string: " ").join(arrayOfAttributedStrings)
//
extension NSAttributedString {
func join(sequence: [NSAttributedString]) -> NSAttributedString {
var mutableString = NSMutableAttributedString(attributedString: sequence[0])
for index in 1 ..< sequence.count {
mutableString.appendAttributedString(self)
mutableString.appendAttributedString(sequence[index])
}
return NSAttributedString(attributedString: mutableString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment