Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulletpoint spacing and linebreak start position with NSHTMLTextDocumentType

WIth iOS 7 came NSHTMLTextDocumentType, which Im using the code below to parse html and show it in a UITextView. It works perfectly, except with bullet points.

How can I both change the spacing on each side of the bulletpoints(the space between the bulletpoint and the UItextView left border and the space between the bulletpoint and the text to the right of it)?

Also, more importantly. If the text continues on the next line, I also need it to continue at the same x position like the line above it where the bullet point text started. How can I achieve this? (Second line indentation)

I have tried using all kinds of css, but it seems that NSHTMLTextDocumentType is still fairly limited. All I managed to to is change the color of just lists.

All help is appreciated.

Thank you in advance!

Code:

_textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;

NSString *testText = @"<p><b>This is a test with:</b></p><ul><li>test test test
test test test test test test test test test test <li>test test test test test
test <li>test test test test test <li>test test test test test test test test
test test test test <li>test test test test test test test test <li>test test
test test test test <li>test test test test test test test test test
</li></ul>";

    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSData *htmlData = [testText dataUsingEncoding:NSUnicodeStringEncoding];

    _attributedString = [[NSMutableAttributedString alloc] initWithData:htmlData
    options:options documentAttributes:nil error:nil];

    // Paragraph style
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.paragraphSpacing = 0;
    paragraphStyle.lineHeightMultiple = 1.0f;
    paragraphStyle.maximumLineHeight = 30.0f;
    paragraphStyle.minimumLineHeight = 20.0f;

    // Adding attributes to attributedString
    [_attributedString addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle}
                              range:NSMakeRange(0,_attributedString.length)];
    [_attributedString addAttributes:@{NSFontAttributeName:font}
               range:NSMakeRange(0,_attributedString.length)];

    // Setting the attributed text
    _textView.attributedText = _attributedString;
like image 280
AlexanderN Avatar asked Nov 01 '13 08:11

AlexanderN


People also ask

How do I get rid of line space before and after bullet points?

In the Apply Styles pane (Ctrl+Shift+S), type in List Paragraph. Click the Modify button, and then click Format, Paragraph. Make sure that Spacing Before and After are both set to zero. Click OK.

How do I fix the bullet spacing in Powerpoint?

To change the bullet spacing:Select the lines you want to change, then go to the desired indent marker. In our example, we'll use the hanging indent marker. Click and drag the indent marker as needed. When you're done, the bullet spacing will be adjusted.

How do you put a space between bullets and text in CSS?

We use CSS padding-left property, to create a space between bullets and the text. It is used to set the padding area on the left of an element. HTML support ordered list, unordered list and HTML support ordered list, unordered list and we must use the <ul> tag, to create unordered list in HTML.


1 Answers

You should be able to cease a mutable paragraphStyle and then set its indents, like:

NSMutableParagraphStyle *const bulletParagraphStyle = [paragraphStyle mutableCopy];
bulletParagraphStyle.firstLineHeadIndent = 20;
bulletParagraphStyle.tailIndent =20;

then set this paragraph style on ONLY the range of the text that includes the bullet points.

(It's going to be a bit clumsy to try to start with the HTML, I'd think: if you want to stay with that route maybe break it up into two HTML strings, one of the bullet list and one for the header, so you can set the different properties more easily.)

like image 99
Wil Shipley Avatar answered Sep 20 '22 03:09

Wil Shipley