iphone - How to create multicolor text in UILabel for iOS 5.1 and below -
i want create label contain text have different color. this,
i have done in ios 6 using nsmutableattributedstring
nsmutableattributedstring *text = [[nsmutableattributedstring alloc] initwithattributedstring: self.exerciselbl.attributedtext]; [text addattribute: nsforegroundcolorattributename value: [uicolor colorwithred:(187/255.0) green:(57/255.0) blue:(38/255.0) alpha:1] range: nsmakerange(0, 2)]; [self.exerciselbl setattributedtext: text];
but not work ios 5.1 , below. how achieve same result in ios 5.
/**(1)** build nsattributedstring *******/ nsmutableattributedstring* attrstr = [nsmutableattributedstring attributedstringwithstring:@"hello world!"]; // calls don't specify range affects whole string [attrstr setfont:[uifont systemfontofsize:12]]; [attrstr settextcolor:[uicolor graycolor]]; // change color of "hello" [attrstr settextcolor:[uicolor redcolor] range:nsmakerange(0,5)]; /**(2)** affect nsattributedstring ohattributedlabel *******/ myattributedlabel.attributedtext = attrstr;` // use "justified" alignment `myattributedlabel.textalignment = uitextalignmentjustify;`
Comments
Post a Comment