Objective-c 如何在没有第三方库的情况下向我的字符串显示粗体和斜体
How to show Bold and Italic both to My NSString without ThirdParty Libary in Objective-c
这是我尝试的代码:
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];
输出:
应用程序功能仅供位于 美国
的用户使用
但我想要 America 的粗体和斜体。
请指导我添加我的字符串。
谢谢,
米希尔
可能没有第三方库,您可以在属性检查器中更改标签 属性 值,如下图所示。
这应该有效:
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];
附加输出:
也许你可以试试下面的代码:
//----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];
已经过测试,工作正常。
请使用以下代码。
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;
这是我尝试的代码:
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];
输出:
应用程序功能仅供位于 美国
但我想要 America 的粗体和斜体。
请指导我添加我的字符串。
谢谢,
米希尔
可能没有第三方库,您可以在属性检查器中更改标签 属性 值,如下图所示。
这应该有效:
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];
附加输出:
也许你可以试试下面的代码:
//----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];
已经过测试,工作正常。
请使用以下代码。
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;