Even i was looking for something like this for my textView. What i did, just append above string with my string and pass it to my textView, same can be done for labels also.
import UIKit
class ViewController: UIViewController {
@IBOutlet var symbolView: SymbolTextLabel!
var testString = "Understanding the concept of sales"
var bulletSymbol = "\u{2022}"
var fontsize: CGFloat= 18
override func viewDidLoad() {
super.viewDidLoad()
//First way // Dynamically creating SymbolTextLabel object
let symbolTextLabel = SymbolTextLabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
symbolTextLabel.setText(text: testString, symbolCode: bulletSymbol) //setting text and symbol of text item
symbolTextLabel.setFontSize(textSize: fontsize) // setting font size
//symbolTextLabel.setSpacing(spacing: 5) // setting space between symbol and text
self.view.addSubview(symbolTextLabel)
//second way // from storyboard or interface builder
symbolView.setText(text: testString, symbolCode: bulletSymbol)
//setting text and symbol of text item
symbolView.setFontSize(textSize: fontsize) // setting font size
//symbolView.setSpacing(spacing: 5) // setting space between symbol and text
}
}
let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 600)
label.textColor = UIColor.lightGray
label.numberOfLines = 0
let arrayString = [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
]
label.attributedText = add(stringList: arrayString, font: label.font, bullet: "")
self.view.addSubview(label)
@IBOutlet weak var bulletLabel: UILabel!
let arrayOfLines = ["Eat egg for protein","You should Eat Ghee","Wheat is with high fiber","Avoid to eat Fish "]
for value in arrayOfLines {
bulletLabel.text = bulletLabel.text! + " • " + value + "\n"
}
If you want to align the text indenting for the bullet points as well, you can use the following method that builds a NSAttributedString with the proper indentation and spacing properties:
And you can use that method as follows, by passing an NSArray with the texts and providing you already have a UILabel:
NSArray *stringArray = @[@"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
@"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
@"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
@"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
];
label.attributedText = [self attributedStringForBulletTexts:stringArray
withFont:label.font
bulletString:@"•"
indentation:15
lineSpacing:2
paragraphSpacing:10
textColor:UIColor.blackColor
bulletColor:UIColor.grayColor];
extension Array where Element == String {
var bulletList: String {
var po = ""
for (index, item) in self.enumerated() {
if index != 0 {
po += "\n"
}
po += .bullet + item
}
return po
}
}
print(["get apples", "get oranges", "get a bannana"].bulletList)