简体   繁体   English

如何在NSString段落中加粗某些单词

[英]How to BOLD certain words within an NSString paragraph

I was wondering if it was possible to bold specific words within a text that is being held in an NSString. 我想知道是否有可能在NSString中保存的文本内加粗特定的单词。 I am able to bold, change the font of characters based on their location within the string using NSMutableAttributedString like this: 我能够使用NSMutableAttributedString粗体显示基于字符在字符串中位置的字体,如下所示:

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

UIFont *boldFont = [UIFont boldSystemFontOfSize:18];
NSRange boldedRange = NSMakeRange(0, 9);


[attrString beginEditing];

[attrString addAttribute:NSFontAttributeName
                   value:boldFont
                   range:boldedRange];

[attrString endEditing];

But this changes the font of the first 9 characters in the String. 但这会更改字符串中前9个字符的字体。

What I want is to put in Bold the following words should they be found within the string: "Questions", "Did you know" and "Further reading". 我想要的是在字符串中找到以下单词时,将它们加粗显示:“问题”,“您是否知道”和“进一步阅读”。 Is this possible? 这可能吗? The fact is that I don't know their position in the string. 事实是我不知道它们在字符串中的位置。

I did have a look at the question suggesting this is a duplicate, but my question is not exactly the same and the answers provided did not help. 我确实看到了一个暗示这是重复的问题,但我的问题并不完全相同,所提供的答案也无济于事。 I Need to find ranges within a string and then add them to an NSMutableAttributedString, and this is the bit I am asking help for. 我需要找到一个字符串中的范围,然后将它们添加到NSMutableAttributedString中,这就是我要寻求帮助的地方。 An answer has been provided that explains how to do that. 提供了一个答案,解释了如何执行此操作。

EDIT: The supposed duplicate and its answer DO NOT answer the question. 编辑:假定重复及其答案不回答问题。 This question is more than just finding specific words within a paragraph, it is also about how to format them using NSMutableAttributedString. 这个问题不仅仅是在段落中查找特定的单词,它还涉及如何使用NSMutableAttributedString格式化它们。 The answer provided below is the answer. 下面提供的答案就是答案。 Thanks! 谢谢!

If you don't know substring position then you can use NSRange to find position of substring and using position you can make changes to the substring using NSMutableAttributedString class. 如果您不知道子字符串的位置,则可以使用NSRange查找子字符串的位置,然后使用position可以使用NSMutableAttributedString类对子字符串进行更改。 example : 例如:

UIFont *fontForlabel1 = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
UIFont *fontForlabel2 = [UIFont fontWithName:@"Helvetica-Bold" size:19.0];
UIFont *fontForlabel3 = [UIFont fontWithName:@"Helvetica" size:12.0];
NSString *text = @"Do you know that or did you know that?";
NSRange range;
UILabel *setlable;
NSMutableAttributedString * attributedString= [[NSMutableAttributedString alloc] initWithString:text];

range = [text rangeOfString:@"Do you know"];
[attributedString addAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:fontForlabel1} range:range];

range = [text rangeOfString:[@"or"];
[attributedString addAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:fontForlabel2} range:range];

range = [text rangeOfString:@"did you know that"];
[attributedString addAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:fontForlabel3} range:range];

setlable.attributedText=attributedString;

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

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