iphone - Updated substring in NSAttributedString -


how can replace substring in nsmutableattributedstring without adding static range number? have label text: @"12 friends", want replace 12 (number of friends) number (and use same attributes substring) once come server, , can not use below approach since number of digits unknown:

/*wrong approach*/ nsmutableattributedstring *mutableattributedstring = [[nsmutableattributedstring alloc] initwithattributedstring:label.attributedtext]; [mutableattributedstring replacecharactersinrange:nsmakerange(0, 2) withstring:counter]; [label setattributedtext:mutableattributedstring]; 

if label read "x friends" why not use formatted string , pass in number of friends parameter. of course make whole thing variable localization, etc. basic idea this:

nsinteger numberfromserver = ...  nsstring *string = [nsstring stringwithformat:@"%d friend%@",numberfromserver,((numberfromserver != 1) ? @"s" : @"")];  nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc] initwithstring:string];  [label setattributedtext:attributedstring]; 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -