Last active
August 10, 2023 02:28
-
-
Save HarryGoodwin/4528d6419915258e54b2f8c804687808 to your computer and use it in GitHub Desktop.
Attributed string bullet points - Swift
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
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() |
Excellent!
Thanks Dear!
Worked fine with number list too ;) Many thanks!
Awsome, thanks men :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man!