Skip to content

Instantly share code, notes, and snippets.

@Jegge
Created February 5, 2022 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jegge/417f10052432d687c4c517b066f368cf to your computer and use it in GitHub Desktop.
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
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