Is it possible to set multiple font attributes using NSAttributedString

I am trying to reduce the size of part of my string and use the code below. My conclusion is wrong. I only see that my first font attribute is used for the entire string, and not for 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]; 
+8
string ios nsattributedstring
source share
4 answers

There is nothing wrong with the code. Perhaps you are trying to use font names that do not exist. You checked if fonts really exist, for example. by po or NSLog?

What is output if you write the attributed string?

0
source share

I also had this problem. For the second choice of font, you need to set minus on the length of the line in the same way as where it starts.

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

but not:

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

My 2 cents;) Swift 3

 func attributedTexts(text1: String, attribs1: [String : Any]?, text2: String, attribs2: [String : Any]?) { let str = NSMutableAttributedString(string: text1, attributes: attribs1); str.append(NSAttributedString(string: text2, attributes: attribs2)) return str } 

Application:

 let attr1 = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 24)] let attr2 = [NSFontAttributeName: UIFont.systemFont(ofSize: 16)] let attributedString = self.attributedTexts(text1: "First Text", attribs1: attr1, text2: "\nSecond Text", attribs2: attr2) 
+1
source share

Please watch this site and video, you can get something useful

Rich text

0
source share

All Articles