Skip to content

Instantly share code, notes, and snippets.

@tonespy
Last active March 8, 2018 10:04
Show Gist options
  • Save tonespy/7917c99bff49bd592fea037afb7f1cb7 to your computer and use it in GitHub Desktop.
Save tonespy/7917c99bff49bd592fea037afb7f1cb7 to your computer and use it in GitHub Desktop.
NSMutableAttributedString Implementation
import UIKit
extension NSMutableAttributedString {
func generateMultipleColorString(sentences: [String], colors: [UIColor]) -> NSMutableAttributedString? {
let attributedText = NSMutableAttributedString()
var count = 0
while count < sentences.count {
let minAttr = NSMutableAttributedString(string: sentences[count])
minAttr.addAttribute(NSAttributedStringKey.foregroundColor,
value: colors[count],
range: NSMakeRange(0, minAttr.length))
attributedText.append(minAttr)
count += 1
}
//Center align
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
attributedText.addAttribute(NSAttributedStringKey.paragraphStyle,
value: paragraphStyle,
range: NSMakeRange(0, attributedText.length))
return attributedText
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment