iOS - 在 Localizable.strings 中用粗体字符串强调

iOS - Emphasise with bold strings in Localizable.strings

有没有办法像这样在 Localizable 文件中加粗一些单词?

"Pending network connection" = "<b>Pending</b> network connection";

我里面有这个字符串,我只想强调某些单词:

 "camSave" = "To complete onboarding:\n1. Tap Save and disconnect to disconnect from the camera.\n2. Connect to the internet.\n3. Log in to Cloud.";

顺便说一句,我想在警报中使用它

您实际上可以将 HTML 标签与 NSAttributedString 一起使用。这是我用的:

static func convertFromHTMLString(_ input: String?) -> NSAttributedString? {
    guard let input = input else { return nil }

    guard let data = input.data(using: String.Encoding.unicode, allowLossyConversion: true) else { return nil  }
    return try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
}

所以我使用 UTF8 格式将输入字符串转换为原始数据。然后我使用属性字符串的构造函数和使用适当标志的数据 NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html.

您可以像下面这样实现:

  1. 将字符串分解为模板和参数格式。
  2. 提供个性化本地化。
  3. 从模板和参数中形成完整的字符串。
  4. 在完整字符串中查找参数并使用 NSAttributedString 应用格式。

因此在您的情况下,Localizable.string 文件将如下所示

"%@ network connection" = "%@ network connection";
"Pending" = "Pending";

现在形成完整的本地化字符串

let complete = String(format: NSLocalizedString("%@ network connection", ""), NSLocalizedString("Pending", ""))

然后在完整的字符串中找到参数化字符串的Range

let range = complete.range(of: NSLocalizedString("Pending", ""))

现在通过形成 NSAttributedString.

来应用您需要在此范围内应用的任何属性