如何以编程方式在iOS Objective C中使文本标签的某些部分变为粗体?

时间:2017-08-02 07:27:04

标签: ios objective-c iphone xcode

enter image description here

我试过这样的

#define KPrivacyText @"您的隐私对我们非常重要。我们承诺通过保护您的个人信息来赢得您的信任。您提供给我们的所有信息都经过严格保密,并严格保密。未经您的同意,我们不会,也不会向您的组织提供任何个人信息。\ n \ n管理原则:\ n \ n \ n \ u2022 \ t信息严格保密且安全。 \ n \ n \ n \ u2022 \ t信息仅用于所述目的。\ n \ n \ n \ u2022 \ t信息仅在您同意的情况下共享。"

_privacyText.text = KPrivacyText;

我想要"治理原则:"粗体是否有任何像\ n下一行的东西,如同明智的大胆也。

2 个答案:

答案 0 :(得分:2)

你必须使用NSAttributedText来实现这一目标。

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(22, 4);

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];

[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName 
                   value:boldFontName
                   range:boldedRange];

[attrString endEditing];
//draw attrString here...

答案 1 :(得分:1)

//使用NSMutableAttributedString感谢

- (void)viewDidLoad {
[super viewDidLoad];
_isPolicyAccepted = NO;
//_privacyText.text = KPrivacyText;
self.title = @"Privacy Policy";

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Your privacy is very important to us. We are committed \nto earn  your trust by safeguarding your personal \ninformation. All the information you provide to us is \nsecurely maintained and is kept strictly confidential. We \ndo not, and will not provide any personal information to \nyour organisation without your consent.\n\nThe governing principles: \n\n\u2022\tInformation is kept strictly confidential and secure. \n\n\u2022\tInformation is only used for the purposes stated.\n\n\u2022\tInformation is only shared with your consent."];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0f weight:UIFontWeightBold] range:NSMakeRange(318, 27)];

_privacyText.attributedText=attributedString;

}