Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 替换字符串中的粗体字,Swift_Ios_Swift_String_Substring_Uilabel - Fatal编程技术网

Ios 替换字符串中的粗体字,Swift

Ios 替换字符串中的粗体字,Swift,ios,swift,string,substring,uilabel,Ios,Swift,String,Substring,Uilabel,我有一个UILabel,上面有文字“hello world,hello”。有两个问候语。 我想将唯一的“粗体hello”替换为“Thank”,而不使用粗体 我使用以下代码: uiLabel1.text=“你好,世界,你好” 让target=“你好” 让我们来替换=“谢谢” uiLabel1.text.replacingOccurrences(of:target,with:replace,options: NSString.CompareOptions.literal,范围:nil) 结果是:“感

我有一个UILabel,上面有文字“hello world,hello”。有两个问候语。 我想将唯一的“粗体hello”替换为“Thank”,而不使用粗体

我使用以下代码:

uiLabel1.text=“你好,世界,你好

让target=“你好”

让我们来替换=“谢谢”

uiLabel1.text.replacingOccurrences(of:target,with:replace,options: NSString.CompareOptions.literal,范围:nil)

结果是:“感谢世界,感谢”

我想要的结果是:“你好,世界,谢谢”

这是代码。但实际上这是硬编码的。如果
目标
包含在
之间,它将工作

var text = "hello world, <b>hello</b>"
let target = "hello"
let replace = "thanks"

text = text.replacingOccurrences(of: "<b>\(target)</b>", with: replace, options: .literal, range: nil) //hello world, thanks
var text=“你好,世界你好”
让target=“你好”
让我们来替换=“谢谢”
text=text.replacingOccurrences(of:“\(target)”,with:replace,options:.literal,range:nil)//你好,谢谢

这是代码。但实际上这是硬编码的。如果
目标
包含在
之间,它将工作

var text = "hello world, <b>hello</b>"
let target = "hello"
let replace = "thanks"

text = text.replacingOccurrences(of: "<b>\(target)</b>", with: replace, options: .literal, range: nil) //hello world, thanks
var text=“你好,世界你好”
让target=“你好”
让我们来替换=“谢谢”
text=text.replacingOccurrences(of:“\(target)”,with:replace,options:.literal,range:nil)//你好,谢谢

好的,所以可能有一种更简单的方法

因此,我浏览了API(比如super quick)并查找了类似于
lastIndexOf
,这让我有了一个小线索,可以让你向后搜索,嗯,很有趣


这将返回一个
范围
。。。好吧,那我怎么用这个?!嗯,也许好吧,所以可能有一个更简单的方法

因此,我浏览了API(比如super quick)并查找了类似于
lastIndexOf
,这让我有了一个小线索,可以让你向后搜索,嗯,很有趣


这将返回一个
范围
。。。好吧,那我怎么用这个?!嗯,也许你只是在搜索和替换一个单词,所以代码会像我期望的那样。您需要检查每个单词的字符串属性,以确定哪个hello是粗体的,您的代码目前无法满足您的要求。
“hello world,hello”。replacingOccurrences(of:“hello”,with:“Thank”)
?Hi@MadProgrammer,我的错,我的代码有错,没有标记。我已经更新了上面的代码。@ImanuelPardosi它不完全是一行代码,但我已经发布了一种方法,您只需搜索并替换一个单词,因此代码就达到了我的预期效果。您需要检查每个单词的字符串属性,以确定哪个hello是粗体的,您的代码目前无法满足您的要求。
“hello world,hello”。replacingOccurrences(of:“hello”,with:“Thank”)
?Hi@MadProgrammer,我的错,我的代码有错,没有标记。我已经更新了上面的代码。@ImanuelPardosi这不完全是一行代码,但我已经发布了一种方法。@MadProgrammer,您的代码是替换最后一个问候词“谢谢”,对吗?但我的问题是用粗体属性替换hello,它可能在字符串的第一个、中间或结尾。那么您使用的是NSAttributed字符串吗?因为这是我知道你可以有一个粗体的attributeHi@MadProgrammer的唯一方法,你的代码是替换最后一个问候词“谢谢”,对吗?但我的问题是用粗体属性替换hello,它可能在字符串的第一个、中间或结尾。那么您使用的是NSAttributed字符串吗?因为我知道只有这样你才能有大胆的性格