ios - Change color and size portion of NSString with NSMutableAttributedString -
browsed through few nsattributedstring examples can't seem working. trying change size , color of part of nsmutableattributedstring.
i've tried few variations of this:
nsmutableattributedstring *hinttext = [[nsmutableattributedstring alloc] initwithstring:@"this red , huge , not"]; //black , large [hinttext setattributes:@{nsfontattributename:[uifont fontwithname:@"futura-medium" size:20.0f], nsforegroundcolorattributename:[uicolor blackcolor]} range:nsmakerange(0, 11)]; //rest of text -- futura [hinttext setattributes:@{nsfontattributename:[uifont fontwithname:@"futura-medium" size:16.0f]} range:nsmakerange(12, ((hinttext.length -1) - 12))]; this changes size of text, not color. can point me in right direction?
edit: use so: myuilabel.attributedtext = hinttext;
when i'm trying this, find easier build individual pieces (each of nsattributedstring) , "glue" them following:
nsattributedstring *string1 = // red , large string here nsattributedstring *string2 = // futura string here nsmutableattributedstring *hinttext = [[nsmutableattributedstring alloc] init]; [hinttext appendattributedstring:string1]; [hinttext appendattributedstring:string2]; i find makes easier follow logical flow , i've never found way have performance limitations needed optimization.
update:
fwiw, got following code work believe op desires:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. nsmutableattributedstring *hinttext = [[nsmutableattributedstring alloc] initwithstring:@"this red , huge , not"]; //red , large [hinttext setattributes:@{nsfontattributename:[uifont fontwithname:@"futura-medium" size:20.0f], nsforegroundcolorattributename:[uicolor redcolor]} range:nsmakerange(0, 20)]; //rest of text -- futura [hinttext setattributes:@{nsfontattributename:[uifont fontwithname:@"futura-medium" size:16.0f]} range:nsmakerange(20, hinttext.length - 20)]; [self.button setattributedtitle:hinttext forstate:uicontrolstatenormal]; } note specifying [uicolor blackcolor] , updated [uicolor redcolor]. also, updated range calculations include of characters in "this red , huge".
Comments
Post a Comment