iOS - Emphasise with bold strings in Localizable.strings
Asked Answered
R

2

7

Is there a way bold some words inside the Localizable file like so?

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

I have this string inside and i want to emphasis only certen words:

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

Btw i want to use it inside alert

Rica answered 11/2, 2019 at 12:47 Comment(0)
C
3

You can actually use HTML tags with NSAttributedString. This is what I used:

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)
}

So I convert an input string to raw data using UTF8 format. Then I use a constructor of attributed string with data using appropriate flags NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html.

Coupler answered 11/2, 2019 at 12:55 Comment(2)
How do you use it? i want to implement it inside ui alert titleRica
@Rica I don't think native alert views support this. You will have to create a custom alert view to do this. After that I assume you will use a label in it for title so label.attributedText = convertFromHTMLString(myLocalizedString).Coupler
C
5

You can achieve this like following:

  1. Break the string into template and parameter format.
  2. Provide individual localization.
  3. Form the complete string from the template and parameter.
  4. Find the parameter's within the complete string and apply formats using NSAttributedString.

So in your case, in the Localizable.string file will look like following

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

Now form the complete string which is localized

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

Then find the Range of the parameterized string within the complete string.

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

Now apply whatever attributes you need to apply in this range by forming NSAttributedString.

Compromise answered 11/2, 2019 at 13:0 Comment(0)
C
3

You can actually use HTML tags with NSAttributedString. This is what I used:

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)
}

So I convert an input string to raw data using UTF8 format. Then I use a constructor of attributed string with data using appropriate flags NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html.

Coupler answered 11/2, 2019 at 12:55 Comment(2)
How do you use it? i want to implement it inside ui alert titleRica
@Rica I don't think native alert views support this. You will have to create a custom alert view to do this. After that I assume you will use a label in it for title so label.attributedText = convertFromHTMLString(myLocalizedString).Coupler

© 2022 - 2024 — McMap. All rights reserved.