【问题标题】:How to make particular word colored : coming from localized string file of iOS如何使特定的单词着色:来自 iOS 的本地化字符串文件
【发布时间】:2014-08-06 11:55:45
【问题描述】:

我想让特定的单词像这样着色:

英文文本:-

法语中的相同位置文本:-

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

【问题讨论】:

  • 您必须将句子和单词(要着色)分别本地化,以便在本地化后找到您的单词。这不是 iOS 问题,这是逻辑问题。

标签: ios colors localization


【解决方案1】:

我知道这个答案晚了几年,但我通过在我的可本地化字符串中允许 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")
        }

本示例为您提供黄色、24 点、苹果系统字体文本。可本地化的字符串将“emergency”这个词标记为红色,最终显示时会以红色显示。

【讨论】:

    【解决方案2】:

    下面的sn-p可以用来达到你的要求

      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];
    

    【讨论】:

    • 感谢您的回复,但正如我在问题中提到的,我不能使用 NSMakeRange() 因为文本会因语言设置而异!。我希望你明白了。
    • 抱歉,这是一个复制粘贴错误。查看我的编辑其中 redString 是您的字符串参数
    • 我认为这个解决方案的关键是变量 redString 和 blueString 将代表本地化的语言字符串。所以 iDev 的代码会在英语中寻找“red”和“blue”,但会在法语中寻找并着色“rouge”和“bleu”。由于他正在调用rangeOfString:,因此它会在本地化文本中的任何位置找到这些字符串。
    • redColor 不是确切的字符串,它是一个变量,其中的字符串需要为红色
    • 感谢您的回复,@iDEV:如何识别英文文本“red”和法文文本“rouge”以应用整个字符串中的颜色? .是否有任何解决方案可以解决本地化字符串文件本身的问题??
    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    相关资源
    最近更新 更多