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:
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setAlignment:NSTextAlignmentRight];
[mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:rangeOfDate];
But the whole string is aligned to the left.
Building on @Verglas's answer...
The way you'd normally do something like this in HTML is via floating. Something like this::
It would be great if you could transform this into a NSAttributedString and have it work:
Sadly, it does not work.
For a second attempt, we can try using a HTML table:
Curiously, this works as intended. What's even more curious are the attributes it generates:
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.
What you do in your code is to set the paragraph style (the text from \n to \n) and it is not possible to have multiple text alignments in the same paragraph. I had the same problem and ended up using multiple UILabels in iOS 6 :-(
However, in iOS 7 I noticed TabStops for ParagraphStyle. This actually makes it possible, but I find the documentation quite inadequate. However, I ended up getting it to work. You can set a right aligned TapStop, which causes your text to get rendered to the left of your specified stop (until that space is filled). In order to get my simple example to work I had to add at least one more attribute besides the paragraph one - it could have been just a clear UIColor for the background though :-)
The following is a simple example of drawRect in a UIView:
That code will render the following: