Skip to content

Instantly share code, notes, and snippets.

@HarryGoodwin
Last active August 10, 2023 02:28
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save HarryGoodwin/4528d6419915258e54b2f8c804687808 to your computer and use it in GitHub Desktop.
Save HarryGoodwin/4528d6419915258e54b2f8c804687808 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()
@brascene
Copy link

Thanks man!

@gopinath0332
Copy link

Excellent!

@nithingeorgesonin
Copy link

Thanks Dear!

@Wassmd
Copy link

Wassmd commented Jun 16, 2021

Worked fine with number list too ;) Many thanks!

@JakfarShodiq
Copy link

Awsome, thanks men :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment