如何在Objective-c中没有ThirdParty Libary的情况下向我的NSString显示Bold和Italic

时间:2016-07-01 08:26:15

标签: ios objective-c nsstring uifont nsmutableattributedstring

这是我尝试的代码:

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

请指导我添加我的字符​​串。

谢谢,
米希尔

4 个答案:

答案 0 :(得分:2)

可以使用第三方库,您可以在属性检查器中更改标签属性值,如下图所示。enter image description here

答案 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];  

附加输出:

enter image description here

答案 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;