OStack程序员社区-中国程序员成长平台

标题: html - 如何在 iOS 中为 html 格式的字符串应用粗体? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:47
标题: html - 如何在 iOS 中为 html 格式的字符串应用粗体?

我想使用在我的应用程序的撰写邮件中打印的字符串以粗体打印文本

messageBody = [NSString stringWithFormat"<b>Background Information</b>"];



Best Answer-推荐答案


What you could do is use an NSAttributedString.

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...

or 

use this code 

 you do not want to bother with fonts (as not every variation of font contains "Bold"), here is another way to do this:

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:"Approximate Distance: 120m away"];
   [attrString beginEditing];
   [attrString applyFontTraits:NSBoldFontMask
                         range:NSMakeRange(22, 4)];
   [attrString endEditing];

关于html - 如何在 iOS 中为 html 格式的字符串应用粗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915376/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4