如何使特定单词着色:来自iOS的本地化字符串文件

如何使特定单词着色:来自iOS的本地化字符串文件,ios,colors,localization,Ios,Colors,Localization,我想把特定的单词涂成这样的颜色: 英文文本:- 法语同一位置文本:- 这些文本来自本地化的字符串文件。我找到了一个解决方案,它使用字符串范围属性NSMakeRange(0,3)来表示属性文本。这是我不想要的,因为本地化语言字符串的文本会有所不同。仅供参考,我在UITextView中放置了文本。 那么,我如何通过简单的方法实现这一点呢?是否有任何解决方案可以解决本地化字符串文件本身的问题。下面的代码片段可能用于满足您的要求 UITextView *textView = //YourText

我想把特定的单词涂成这样的颜色:

英文文本:-

法语同一位置文本:-

这些文本来自本地化的字符串文件。我找到了一个解决方案,它使用字符串范围属性
NSMakeRange(0,3)
来表示属性文本。这是我不想要的,因为本地化语言字符串的文本会有所不同。仅供参考,我在UITextView中放置了文本。
那么,我如何通过简单的方法实现这一点呢?是否有任何解决方案可以解决本地化字符串文件本身的问题。

下面的代码片段可能用于满足您的要求

  UITextView *textView = //YourTextView

 NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString: textView.attributedText];
        [attributedText addAttribute:NSForegroundColorAttributeName
                               value:[UIColor redColor]
                               range:[textView.text rangeOfString:blueString]];

        [attributedText addAttribute:NSForegroundColorAttributeName
                               value:[UIColor blueColor]
                               range:[textView.text rangeOfString:redString]];
        [textView setAttributedText: attributedText];

我知道这个答案已经晚了几年,但我通过允许在可本地化字符串中使用HTML解决了这个问题。例如:

"emergency" = "This is a test of the <span style=\"color:red\">emergency</span> broadcast system.";
“紧急情况”=“这是对紧急广播系统的测试。”;
当然,单凭这一点并不能解决问题,因为IOS中的可本地化字符串文件本身并不支持任何类型的HTML/XML注释。因此,当您在swift代码中提取字符串时,您需要自己处理HTML。我是这样做的:

        // Get the localized string
        let localized = NSLocalizedString("emergency", comment: "")
        
        // Prepend an html header to the string select the primary font and color.
        // The settings on the UITextView or UILabel will be ignored, so you need to do this
        let htmlString = "<span style=\"color:yellow;font-size:24px;font-family:-apple-system\">" + localized
        
        // perform the magic to turn the html string into an NSAttributedString
        let data = NSString(string: htmlString).data(using: String.Encoding.utf8.rawValue)
        do {
            let attributedString = try NSAttributedString(data: data!,
                                          options: [
                                            .documentType: NSAttributedString.DocumentType.html,
                                            .characterEncoding: String.Encoding.utf8.rawValue],
                                          documentAttributes: nil)

            // Show the NSAttributedString in your help UITextView (or anywhere else)
            help.attributedText = attributedString
        } catch {
            print("There was a problem")
        }
//获取本地化字符串
let localized=NSLocalizedString(“紧急”,注释:“”)
//在字符串前添加html标题,选择主要字体和颜色。
//UITextView或UILabel上的设置将被忽略,因此需要执行此操作
让htmlString=”“+本地化
//执行将html字符串转换为NSAttributed字符串的魔术
让data=NSString(string:htmlString).data(使用:string.Encoding.utf8.rawValue)
做{
让attributedString=尝试NSAttributedString(数据:data!,
选项:[
.documentType:nsAttributeString.documentType.html,
.characterEncoding:String.Encoding.utf8.rawValue],
文件属性:无)
//在帮助UITextView(或其他任何地方)中显示NSAttributed字符串
help.attributedText=attributedString
}抓住{
打印(“出现问题”)
}

本例为您提供黄色、24点苹果系统字体文本。可本地化字符串将单词“emergency”标记为红色,并将在最终显示中显示为红色。

感谢您的回复,但正如我在问题中提到的,我无法使用NSMakerRange(),因为语言设置的文本会有所不同!。我希望你收到了。很抱歉,这是一个复制粘贴错误。请参阅我的编辑,其中redString是字符串参数。我认为此解决方案的关键是变量redString和blueString将表示本地化的language字符串。因此,iDev的代码将在英语中查找“红色”和“蓝色”,但在法语中查找并为“胭脂”和“bleu”着色。由于他正在调用
rangeOfString:
它将在本地化文本中的任何位置找到这些字符串。redColor不是确切的字符串,而是一个变量,其中的字符串需要使用红色。感谢您的回复,@iDEV:我如何识别英语中的文本“red”和“ruge”在法语中,应用整个字符串的颜色。是否有任何解决方案可以解决本地化字符串文件本身的问题??您必须将句子和单词(您希望使用颜色)分别本地化,以便在本地化后找到您的单词。这不是iOS问题,而是逻辑问题。