Skip to content

Instantly share code, notes, and snippets.

@sergii-frost
Created November 22, 2016 09:42
Show Gist options
  • Save sergii-frost/0d74cd727fa67f2fdf1267bc65de546e to your computer and use it in GitHub Desktop.
Save sergii-frost/0d74cd727fa67f2fdf1267bc65de546e to your computer and use it in GitHub Desktop.
NSMutableAttributedString (Swift) Playground
//: Playground - noun: a place where people can play
import UIKit
let creator = "Awesome Me"
let teamName = "Awesome Team"
let groupName = "Awesome Group"
func getHeader(creator: String, team: String, group: String? = nil) -> NSAttributedString {
let result: NSMutableAttributedString = NSMutableAttributedString()
if let group = group {
result.appendAttributedString(NSAttributedString(string: "\(creator) > ", attributes: [NSForegroundColorAttributeName: UIColor.blackColor()]))
result.appendAttributedString(NSAttributedString(string: " \(team) ", attributes: [NSForegroundColorAttributeName: UIColor.orangeColor()]))
result.appendAttributedString(NSAttributedString(string: " & ", attributes: [NSForegroundColorAttributeName: UIColor.blackColor()]))
result.appendAttributedString(NSAttributedString(string: "\(group)", attributes: [NSForegroundColorAttributeName: UIColor.orangeColor()]))
} else {
result.appendAttributedString(NSAttributedString(string: "\(creator) > ", attributes: [NSForegroundColorAttributeName: UIColor.blackColor()]))
result.appendAttributedString(NSAttributedString(string: " \(team) ", attributes: [NSForegroundColorAttributeName: UIColor.orangeColor()]))
}
return result
}
getHeader(creator, team: teamName, group: groupName)
getHeader(creator, team: teamName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment