objective c - Setting a NSMutableAttributedString left aligned and right aligned text -


i attempting use nsmutableattributedstring format text left aligned , right aligned on same piece of text

i've reviewed similar answer @ ios multiple right , left align on same line not tab stops work correctly.

my own solution following.

have copy + add physical tabs (ie: \t) end of copy

then create paragraph style chat time string right aligned , append end of string.

i've got work;

enter image description here

but problem time isn't right aligned @ all; seems stuck left aligned.

i'd time text on right hand side.

here code setting time part;

    nsattributedstring *attributedstring = [[nsmutableattributedstring alloc] initwithstring:message.body attributes:attributes];  nsmutableattributedstring *finalizedcopy = [attributedstring mutablecopy];     if (message.sender.remoteid) [finalizedcopy appendattributedstring:tabstops];       // add time chat bubble     nsstring *messagetime = [[jsqmessagestimestampformatter sharedformatter] timefordate:message.date];     nsattributedstring *chatmessagetime = [[nsattributedstring alloc] initwithstring:messagetime attributes:attributes];      nsmutableparagraphstyle *timeparagraphstyle = [[nsmutableparagraphstyle alloc] init];     timeparagraphstyle.alignment = nstextalignmentright;      nsmutableattributedstring *datestring = [[nsmutableattributedstring alloc] initwithattributedstring:chatmessagetime];     [datestring setattributes:@{ nsfontattributename : [uifont vic_fontofsize:10],                                  nsforegroundcolorattributename: [uicolor vic_darktextcolor]                                  }                         range:nsmakerange(0, datestring.length)];      [datestring addattribute:nsparagraphstyleattributename value:timeparagraphstyle range:nsmakerange(0, datestring.length)];      [finalizedcopy appendattributedstring:datestring];       attributedstring = [finalizedcopy copy]; 

i wondering how best achieve desired effect.

many thanks

also, if use nstabstops -- put them, @ end of left-hand string or @ start of right-hand string?

ie:

nstexttab *tabstop = [[nstexttab alloc] initwithtextalignment:nstextalignmentright location:150 options:nil]; [paragraph settabstops:@[tabstop]]; [finalizedcopy addattribute:nsparagraphstyleattributename value:paragraph range:nsmakerange(0, finalizedcopy.length)];

is @ end of left-hand string see no results visually.

here's generic swift code want think. same approach should work in objective-c well:

    // paragraph left , right aligned text  let paragraph = nsmutableparagraphstyle() paragraph.alignment = .left tabstop in paragraph.tabstops {     paragraph.removetabstop(tabstop) } paragraph.addtabstop(nstexttab(textalignment: .right, location: 240.0, options: [:]))  let text = nsmutableattributedstring()  let leftattributes = [     nsfontattributename: uifont.systemfont(ofsize: 10.0),     nsforegroundcolorattributename: uicolor.white,     nsparagraphstyleattributename: paragraph ]  let rightattributes = [     nsfontattributename: uifont.boldsystemfont(ofsize: 20.0),     nsforegroundcolorattributename: uicolor.white,     nsparagraphstyleattributename: paragraph ]  let data = [     "carrots": 23,     "pears": 453,     "apples ": 2350 ]  (key, value) in data {     text.append(nsattributedstring(string: key, attributes: leftattributes))     text.append(nsattributedstring(string: "\t\(value)\n", attributes: rightattributes)) } 

Comments