I need to be able to display dates in a table, with the date displayed as say 22nd Sept 2014, where the nd is shown as small text to the top right of the 22 as in most date displayed formats. Using the tag works in Html but not in IOS, does this format work in iOS?
Asked
Active
Viewed 1,435 times
0
-
Use attributed string.. – Salman Zaidi Sep 08 '14 at 07:42
-
There seems to be a solution here http://stackoverflow.com/questions/21415963/nsattributedstring-superscript-styling – Satish P Sep 08 '14 at 07:55
1 Answers
1
Create an attributed string for example from a dateString
of type NSString
having format @"22nd Sept 2014"
(or whatever format, but "st", "nd" and "th" at 3rd and 4th index of string):
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:dateString
attributes:@{NSFontAttributeName:yourLabel.font}];
[attributedString setAttributes:@{NSFontAttributeName:[UIFont fontWithName:yourLabel.font.fontName size:8]
, NSBaselineOffsetAttributeName:@8} range:NSMakeRange(2, 2)];
yourLabel.attributedText = attributedString;

Salman Zaidi
- 9,342
- 12
- 44
- 61
-
Thank you for your reply, I think you are saying to set the (nd or th) separately by specifying them as 3rd and 4th index of string. How would I then get them to display as they do in Html using ? – user616076 Sep 08 '14 at 08:34
-
No you don't need to separate them out.. You just have to create an attributedString as explained above and assign that value to you label or textview.. just don't assign text value to that object – Salman Zaidi Sep 08 '14 at 09:35
-
Thank you Salman, I've tried with both the r Registered icon and with a small version of th/rd but it still displays as normal text. Have you a working version I could try? – user616076 Sep 08 '14 at 10:41