我有一个UITextView,并且我要用粗体显示某些要用NSString stringWithFormat转换的单词。
我环顾了Stack Overflow,并尝试按照发布的内容进行操作,但我想我不太理解。
这是我一直在玩的东西:
NSRange boldedRange = NSMakeRange(0, 4);
NSString *boldFontName = [[UIFont fontWithName:@"Helvetica-Bold" size:100]fontName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.name];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
self.resultsTextView.attributedText = attrString;
self.resultsTextView.text = [NSString stringWithFormat:@"One day, %@ was taking a walk and saw a %@ boy. He was %@ a %@.", attrString, self.adjective, self.adverb, self.noun];
最佳答案
如果需要,还可以通过以下方式设置字典:将字典整体设置为属性
NSString *strTextView = @"This is some demo Text to set BOLD";
NSRange rangeBold = [strTextView rangeOfString:@"BOLD"];
UIFont *fontText = [UIFont boldSystemFontOfSize:10];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:strTextView];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];
[textViewTermsPolicy setAttributedText:mutAttrTextViewString];