7

Is there any way to make a bullet point list in iphone?

If you copy and paste a bullet point list into a UITextView in IB then it shows up. Is there anyway to do this programatically?

Thank you

Tom

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Yes, it works - and if you copy and paste from word this also works (i'm assuming because its the same character), but because there is no formatting and no guarantee for support I decided yours was the best and "most true" answer. However, in my app I have used the second answer. – Thomas Clayson Sep 28 '10 at 16:06
  • https://codeload.github.com/eyalc/ECListView/zip/master – Hemang Oct 23 '13 at 17:28

4 Answers4

38

The "bullet" character is at Unicode code point U+2022. You can use it in a string with @"\u2022" or [NSString stringWithFormat:@"%C", 0x2022].

The "line feed" character is at Unicode code point U+000A, and is used as UIKit's newline character. You can use it in a string with @"\n".

For example, if you had an array of strings, you could make a bulleted list with something like this:

NSArray * items = ...;
NSMutableString * bulletList = [NSMutableString stringWithCapacity:items.count*30];
for (NSString * s in items)
{
  [bulletList appendFormat:@"\u2022 %@\n", s];
}
textView.text = bulletList;

It won't indent lines like a "proper" word processor. "Bad things" will happen if your list items include newline characters (but what did you expect?).

(Apple doesn't guarantee that "\uXXXX" escapes work in NSString literals, but in practice they do if you use Apple's compiler.)

tc.
  • 33,468
  • 5
  • 78
  • 96
  • I wasn't able to get the indentation working properly. The bullets are just printed out on the same line: "* Text * Text2". Is there a fix for this? – Piper Mar 11 '13 at 14:35
  • 3
    @PolyShell The code above makes no attempt to do proper indentation. If you mean the line breaks, try setting `label.numberOfLines = 0` – tc. Mar 11 '13 at 18:18
4

As far as I know the only way to achieve this (and almost any other formatted rich-text) on the iPhone is to use a UIWebView and insert HTML-Code like this:

<ul>
     <li>Bullet</li>
</ul>

In response to your comment, UIWebViews can be "beautified" by the following peace of code:

for(UIView* v in webView.subviews){
    if([v isKindOfClass:[UIScrollView class] ]){

        //disable bouncing
        UIScrollView* sv = (UIScrollView*) v;
        sv.alwaysBounceVertical = NO;
        sv.alwaysBounceHorizontal = NO;

        //disable scroll-shadows
        for (UIView *subView in [sv subviews])
            if ([[[subView class] description] isEqualToString:@"UIImageView"])
                subView.hidden = YES;
    }
}

I haven't submitted this yet but I guess it should be "AppStore safe".

Perception
  • 79,279
  • 19
  • 185
  • 195
Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
3

Swift-5

Just writing the @tc answer in Swift

let items: [String] = ["Point-1", "Point-2", "Point-3"]
var bulleLists: [String] = []
for item in items {
     bulleLists.append("\u{2022}" + item + "\n")
}
textview.text = bulleLists.joined()
Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52
1
var bulletLists: [String] = ["Apple", "Banana"]
let bullet = "\u{2022} " // your bullet style
bulletLists = bulletLists.map { bullet + $0 }
yourTextView.adjustsFontForContentSizeCategory = true

// set font attributes
var attributes = [NSAttributedString.Key: Any]()
attributes[.font] = remarkTextView.font
attributes[.foregroundColor] = UIColor(.enterpriseGray)

// calculate indent size
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = (bullet as NSString).size(withAttributes: attributes).width
attributes[.paragraphStyle] = paragraphStyle

// add attributes to your textView
yourTextView.attributedText = NSAttributedString(string: bulletLists.joined(separator: "\n"), attributes: attributes)

result:

• Apple
• Banana
Jam
  • 101
  • 4