Last active
March 20, 2017 18:00
-
-
Save JasonCanCode/55bfc86161af0191a44b9614240368f9 to your computer and use it in GitHub Desktop.
Assigns attributed text to the label by iterating through an array of strings with associated FontTypes.
This file contains hidden or 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
/// Used to identify the intended format of a string when using the `attributedTextFromArray:` func of a UILabel extension. | |
enum FontType { | |
case system, bold, italic, underlined | |
} | |
extension UILabel { | |
/** | |
Assigns attributed text to the label by iterating through an array of strings with associated FontTypes. | |
Example use: | |
thoughtBubbleLabel.attributedTextFromArray([ | |
( "\"What a handy extension,\" ", .Italic ), | |
( "he thought to himself. ", .System ), | |
( "\n", .System ), | |
( "Indeed. ", .Bold ), | |
( "It was.", .Underlined ), | |
]) | |
When used with the String extension: | |
thoughtBubbleLabel.attributedTextFromArray([ | |
"\"What a handy extension,\" ".Italic, | |
"he thought to himself. ".System, | |
"\n".System, | |
"Indeed. ".Bold, | |
"It was.".Underlined | |
]) | |
**NOTE:** This assumes that the desired label font size is consistant throughout and has already been set. | |
- parameter components: A tuple array of strings with associated FontTypes. | |
*/ | |
func attributedTextFromArray(_ components: [(String, FontType)]) { | |
let size = font.pointSize | |
let attrString = NSMutableAttributedString() | |
for component in components { | |
let (text, attr) = component | |
let font: UIFont | |
switch attr { | |
case .system: | |
font = UIFont.systemFont(ofSize: size) | |
case .bold: | |
font = UIFont.boldSystemFont(ofSize: size) | |
case .italic: | |
font = UIFont.italicSystemFont(ofSize: size) | |
case .underlined: | |
attrString.append(NSAttributedString(string: text, attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])) | |
continue | |
} | |
attrString.append(NSAttributedString(string: text, attributes: [NSFontAttributeName: font])) | |
} | |
attributedText = attrString | |
} | |
} | |
// swiftlint:disable variable_name | |
extension String { | |
/// Convenient way to instantly convert a string to a `(String, FontType)` object for use with the `attributedTextFromArray:` func of a UILabel extension. | |
var Bold: (String, FontType) { | |
return ( self, .bold ) | |
} | |
/// Convenient way to instantly convert a string to a `(String, FontType)` object for use with the `attributedTextFromArray:` func of a UILabel extension. | |
var Italic: (String, FontType) { | |
return ( self, .italic ) | |
} | |
/// Convenient way to instantly convert a string to a `(String, FontType)` object for use with the `attributedTextFromArray:` func of a UILabel extension. | |
var Underlined: (String, FontType) { | |
return ( self, .underlined ) | |
} | |
/// Convenient way to instantly convert a string to a `(String, FontType)` object for use with the `attributedTextFromArray:` func of a UILabel extension. | |
var System: (String, FontType) { | |
return ( self, .system ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment