NSAttributedString inserting a bullet point?

aryaxt picture aryaxt · Jul 11, 2011 · Viewed 21.1k times · Source

So I have an NSAttributedString I want to insert a bullet point at the beginning of a portion of text. How can I do this? How do I create a CTPAragraphStyle that creates this bullet point when I display the text?

Edit: Should be available on iOS

Answer

Tijn picture Tijn · Sep 12, 2014

Here's a more modern approach that works from iOS6:

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"•\texample bullet fill out the text to check what happens on the second line and make sure it is lining up OK"];

NSMutableParagraphStyle *paragraphStyle;
paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setTabStops:@[[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:15 options:nil]]];
[paragraphStyle setDefaultTabInterval:15];
[paragraphStyle setFirstLineHeadIndent:0];
[paragraphStyle setHeadIndent:15];

[string addAttributes:@{NSParagraphStyleAttributeName: paragraphStyle} range:NSMakeRange(0,[string length])];