Is it possible to add left and right alignments to different parts of the string?
I tried to add alignment attribute to the right part:
NSMutable
Building on @Verglas's answer...
The way you'd normally do something like this in HTML is via floating. Something like this::
Left
Right
It would be great if you could transform this into a NSAttributedString and have it work:
NSString* html = @"Left
Right
";
NSData* d = [html dataUsingEncoding: NSUTF8StringEncoding];
NSAttributedString* as = [[NSMutableAttributedString alloc] initWithData: d
options: @{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)
}
documentAttributes: nil
error: nil];
Sadly, it does not work.
For a second attempt, we can try using a HTML table:
html = @"Left Right
";
Curiously, this works as intended. What's even more curious are the attributes it generates:
2014-08-27 14:27:31.443 testParagraphStyles[2095:60b] range: {0, 5} attributes: {
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n \"\"\n), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
2014-08-27 14:27:31.444 testParagraphStyles[2095:60b] range: {5, 6} attributes: {
NSParagraphStyle = "Alignment 2, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n \"\"\n), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}
Scroll to the right and notice the reference to NSTextTableBlock. NSTextTable isn't a public API on iOS, but NSAttributedString initWithData:options:documentAttributes:error: used it to generate our attributed string from HTML. This is painful because it means we can't construct a NSAttributedString by hand (we must generate it fro HTML using this API).
Building attributed strings from HTML is slow and largely undocumented. I avoid it whenever I can.