Created
October 8, 2014 14:46
-
-
Save tgroshon/7b0c862edf80bd44d7cb to your computer and use it in GitHub Desktop.
Extending NSAttributedString to have a "join" method like String.
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
// | |
// 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