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;
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.
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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With