2015-12-29 35 views
0

我已经在网上寻找加粗NSString的方法,要么它不在那里,要么我找不到它。我只是想大胆NSString的字,但到目前为止,我所做的是不是沃金:绑定一个nsstring

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

我一直依靠这使“player1Name”大胆,但它并没有在所有的工作。提前

enter image description here

感谢的人谁帮助我找出解决办法: 当我运行它,我得到这个。

+0

你想要在uilabel中显示nsstring或其他? – kb920

+0

NSString只是一串字符,没有属性,你可以用很多不同的方式显示粗体字符串,但最简单的是使用支持NSAttrebutedString的东西... –

+0

使用NSAttributedString * player1Name = [[NSAttributedString alloc] initWithString: @“TEST_STRING”属性:@ {NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}]; – nikhil84

回答

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]; 
0

你不能让NSString的大胆:

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

您也可以为特定的文本等设定的范围内。除此之外,您可以将UILabel文本变为粗体。因为UILabels用于UI表示& NSStrings只是对象。所以,让你的UILabel大胆

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

你不能大胆一的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

属性

返回字体名称HelveticaNeueInterface-MediumP4

+0

我试过这个....但它没有出来作为粗体 –

+0

直到你将它设置在屏幕上的UI元素上的“属性文本”(而不是“文本”)属性中才会看到它。比如UILabel,UITextField,UITextView ......你把它放在哪里? –