Skip to content

Instantly share code, notes, and snippets.

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 nbashaw/861e7f7a286211d9276c7feb274e7745 to your computer and use it in GitHub Desktop.
Save nbashaw/861e7f7a286211d9276c7feb274e7745 to your computer and use it in GitHub Desktop.
Attributed string bullet points - Swift
import UIKit
import PlaygroundSupport
struct StringConstants {
static let bullet1 = "This is a small string."
static let bullet2 = "This is more of medium string with a few more words etc."
static let bullet3 = "Well this is certainly a longer string, with many more words than either of the previuos two strings."
}
typealias ParagraphData = (bullet: String, paragraph: String)
class MyViewController : UIViewController {
private let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupLabel()
let paragraphDataPairs: [ParagraphData] = [
("▶︎ ", StringConstants.bullet1),
("▶︎ ", StringConstants.bullet2),
("\u{0009}▶︎ ", StringConstants.bullet1),
("\u{0009}▶︎ ", StringConstants.bullet2),
("\u{0009}▶︎ ", StringConstants.bullet3),
("▶︎ ", StringConstants.bullet3),
]
let stringAttributes: [NSAttributedString.Key: Any] = [.font: label.font!]
let bulletedAttributedString = makeBulletedAttributedString(paragraphDataPairs: paragraphDataPairs,
attributes: stringAttributes)
label.attributedText = bulletedAttributedString
}
private func makeBulletedAttributedString(paragraphDataPairs: [ParagraphData],
attributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
let fullAttributedString = NSMutableAttributedString()
paragraphDataPairs.forEach { paragraphData in
let attributedString = makeBulletString(bullet: paragraphData.bullet,
content: paragraphData.paragraph,
attributes: attributes)
fullAttributedString.append(attributedString)
}
return fullAttributedString
}
private func makeBulletString(bullet: String,
content: String,
attributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
let formattedString: String = "\(bullet)\(content)\n"
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: formattedString,
attributes: attributes)
let headerIndent = (bullet as NSString).size(withAttributes: attributes).width
attributedString.addAttributes([.paragraphStyle: makeParagraphStyle(headIndent: headerIndent)],
range: NSMakeRange(0, attributedString.length))
return attributedString
}
private func makeParagraphStyle(headIndent: CGFloat) -> NSParagraphStyle {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.headIndent = headIndent
return paragraphStyle
}
private func setupLabel() {
view.addSubview(label)
label.numberOfLines = 0
label.font = UIFont.preferredFont(forTextStyle: .headline)
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
])
}
}
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment