Created
February 5, 2022 09:17
-
-
Save Jegge/417f10052432d687c4c517b066f368cf to your computer and use it in GitHub Desktop.
Creates an unordered list with bullets in a NSAttributesString from an array of strings
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 Collection where Element == String { | |
func bulletize(indentation: CGFloat, font: UIFont, color: UIColor) -> NSAttributedString { | |
let style = NSMutableParagraphStyle() | |
style.defaultTabInterval = indentation | |
style.firstLineHeadIndent = 0 | |
style.lineSpacing = 0 | |
style.paragraphSpacing = 15 | |
style.headIndent = indentation | |
style.tabStops = [NSTextTab(textAlignment: .left, location: indentation, options: [:])] | |
let attributes: [NSAttributedString.Key: Any] = [ | |
.font: font, | |
.foregroundColor: color, | |
.paragraphStyle: style | |
] | |
let length = self.count | |
let buffer = NSMutableAttributedString(string: "") | |
self.enumerated().forEach { | |
let line = NSMutableAttributedString(string: "\u{2022}\t\($0.element)\($0.offset >= length - 1 ? "" : "\n")") | |
line.addAttributes(attributes, range: NSRange(location: 0, length: line.length)) | |
buffer.append(line) | |
} | |
return buffer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment