加粗 nstring

Bolding an nsstring

我在网上寻找加粗 NSString 的方法,但它要么不存在,要么我似乎找不到。我只是想在 NSString 中加粗这个词,但到目前为止,我所做的还没有实现:

 NSString *player1Name = [[UIFont boldSystemFontOfSize:12] fontName];

我依靠它来使 "player1Name" 变得粗体,但它根本不起作用。 当我 运行 我得到这个:

提前感谢任何帮助我找出解决方案的人。

试试这个:

NSString *myString = @"My string!";
NSAttributedString *myBoldString = [[NSAttributedString alloc] initWithString:myString
                                                             attributes:@{ NSFontAttributeName: [UIFont boldSystemFontOfSize:35.0] }];

您还可以为特定文本设置范围,例如:

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

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

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

[attrString endEditing];

您不能将 NSString 设为粗体。您可以将 UILabel 文本改为粗体。因为 UI 标签用于 UI 表示 & NSStrings 只是对象。因此,将 UILabel 设为粗体

[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];

你不能加粗 NSString...试试 NSAttributedString...

NSAttributedString *player1Name = [[NSAttributedString alloc] initWithString:@"name" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:12]}];

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/

fontName 是 UIFont

上的 属性
[[UIFont boldSystemFontOfSize:12] fontName]

returns字体名称HelveticaNeueInterface-MediumP4