这是我尝试的代码:
NSString *strFirst = [NSString stringWithFormat:@"The App feature is only available to users while at "];
NSString *strAreaName = kAreaName;
NSRange boldedRange = [strAreaName rangeOfString:strAreaName];
NSMutableAttributedString *newAttString = [[NSMutableAttributedString alloc] initWithString:strFirst];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:strAreaName];
UIFont *fontText = [UIFont boldSystemFontOfSize:17.0];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
[attrString setAttributes:dictBoldText range:boldedRange];
[newAttString appendAttributedString:attrString];
[lblFirst setAttributedText:newAttString];
输出:
应用功能仅适用于美国
但是我想要Bold和Italic中的 America 。
请指导我添加我的字符串。
谢谢,
的米希尔
答案 0 :(得分:2)
答案 1 :(得分:1)
这应该有效:
NSString *strAreaName = @"America";
NSString *html = [NSString stringWithFormat:@"<html><font size='7' face='Helvetica'>The App feature is only available to users while at <b><i>%@</i></b></font><html>", strAreaName];
NSError *err = nil;
lbl.attributedText =
[[NSAttributedString alloc]
initWithData: [html dataUsingEncoding:NSUTF8StringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: &err];
附加输出:
答案 2 :(得分:1)
可以尝试以下代码:
//----Creating font dictionary for bold-italic text
UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFontDescriptor *fontDescriptorNew;
NSDictionary *dictBoldItalicText;
uint32_t NewStyles = [fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic;
fontDescriptorNew = [fontDescriptor fontDescriptorWithSymbolicTraits:NewStyles];
UIFont *updatedFont = [UIFont fontWithDescriptor:fontDescriptorNew size:17.0];//set your font size
dictBoldItalicText = @{ NSFontAttributeName : updatedFont };
//----------
NSString *strFirst = [NSString stringWithFormat:@"The App feature is only available to users while at "];
NSString *strAreaName=@"America";
NSRange boldedRange = [strAreaName rangeOfString:strAreaName];
NSMutableAttributedString *newAttString = [[NSMutableAttributedString alloc] initWithString:strFirst];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:strAreaName];
[attrString setAttributes:dictBoldItalicText range:boldedRange];//Assigning bold-italic attribute dictionary
[newAttString appendAttributedString:attrString];
[lblFirst setAttributedText:newAttString];
经过测试并正常工作。
答案 3 :(得分:1)
请使用以下代码。
UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFontDescriptor *changedFontDescriptor;
NSDictionary *attributes;
uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic;
changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
UIFont *updatedFont = [UIFont fontWithDescriptor:changedFontDescriptor size:0.0];
attributes = @{ NSFontAttributeName : updatedFont };
NSAttributedString * newAttStringd = [[NSAttributedString alloc] initWithString:@"America" attributes:attributes];
lbl.attributedText = newAttStringd;