简体   繁体   English

如何使粗体字体 NSString 值?

[英]How to make bold font NSString value?

In my iOS application, how can I make bold font in NSString value?在我的 iOS 应用程序中,如何在 NSString 值中制作粗体字体? I used below code for making bold font text in NSString value, but showing error我使用下面的代码在 NSString 值中制作粗体文本,但显示错误

like this:像这样:

Undefined symbols for architecture i386: "_kCIAttributeName",架构 i386 的未定义符号:“_kCIAttributeName”,

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = [NSString stringWithFormat:@"%@%@%@",key,@":",@" "];
NSRange boldedRange = NSMakeRange(22, 4);

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

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

[attrString endEditing];

NSString *string = [NSString stringWithFormat:@"%@%@",attrString,[dic objectForKey:key]];

How do I make bold font in string?如何在字符串中制作粗体字体?

the NSString is for store a simple texts without any font formatting. NSString用于存储没有任何字体格式的简单文本

the UILabel has a font property, you can use it to set the visible text's font on the UI , but the UILabel can work with one format only and the whole text gets the same font and style. UILabel有一个font属性,您可以使用它来设置UI上可见文本的字体,但是UILabel只能使用一种格式并且整个文本具有相同的字体和样式。


if you really want to use a kind of rich text document, you should create a UIWebView class and you can show any document inside with the standard HTML tags.如果你真的想使用一种富文本文档,你应该创建一个UIWebView类,你可以在里面显示任何带有标准HTML标签的文档。


UPDATE更新

the UILabel can store attributed string since iOS6, which gives you a chance to show rich-formatted text in one single UILabel instance, even using various fonts or sizes, etc... UILabel可以存储自 iOS6 以来的属性字符串,这使您有机会在一个UILabel实例中显示格式丰富的文本,甚至可以使用各种字体或大小等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM