gpt4 book ai didi

ios - 如何加粗 NSString 中一个字符的多个实例?

转载 作者:行者123 更新时间:2023-11-29 10:42:40 25 4
gpt4 key购买 nike

我有一个类似下面的字符串:

NSString *a = @"* This is a text String \n * Followed by another text String \n * Followed by a third"

我需要将它打印成三行。现在,我希望其中的 Asterix 点加粗。所以我尝试了:

NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:a];
[att addAddtribute:NSFontAttributeName value:SOMEBOLDFONT range:[a rangeOfString:@"*"]];

但这只会加粗第二个和第三个星号。我怎样才能把它们都加粗?

最佳答案

正如其他人所提到的,您需要遍历字符串以返回多个范围。这会起作用:

NSString *a = @"* This is a text String \n* Followed by another text String \n* Followed by a third";
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:a];
NSRange foundRange = [a rangeOfString:@"*"];

while (foundRange.location != NSNotFound)
{
[att addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20.0f] range:foundRange];

NSRange rangeToSearch;
rangeToSearch.location = foundRange.location + foundRange.length;
rangeToSearch.length = a.length - rangeToSearch.location;
foundRange = [a rangeOfString:@"*" options:0 range:rangeToSearch];
}

[[self textView] setAttributedText:att];

关于ios - 如何加粗 NSString 中一个字符的多个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23747164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com