Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set multiple font attributes using an NSAttributedString

I am trying to decrease the size of a portion of my string and am using the below code. My output is not correct. I am only seeing my first font attribute being used for the entire string, not the specified range.

    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:title];
    NSInteger _stringLength=[descriptionText length];
    [attString addAttribute:NSFontAttributeName
                      value:[UIFont fontWithName:@"Gotham-Bold" size:20.0]
                      range:NSMakeRange(0, 10)];
    [attString addAttribute:NSFontAttributeName
                      value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0]
                      range:NSMakeRange(11, _stringLength-1)];
    [self.description setAttributedText:attString];
like image 796
propstm Avatar asked Jan 08 '13 17:01

propstm


1 Answers

I've had this issue too. For the second font selection you need to set the minus on the string length to be the same as where it starts.

  [attString addAttribute:NSFontAttributeName
                    value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0]
                    range:NSMakeRange(11, _stringLength-11)];

rather than:

  [attString addAttribute:NSFontAttributeName
                    value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0]
                    range:NSMakeRange(11, _stringLength-1)];
like image 154
William Robinson Avatar answered Sep 21 '22 12:09

William Robinson