Last active
March 8, 2018 10:04
-
-
Save tonespy/7917c99bff49bd592fea037afb7f1cb7 to your computer and use it in GitHub Desktop.
NSMutableAttributedString Implementation
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
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