Styling various parts of localized string with parameter

Viewed 296

I am trying to apply different styles on parameters or different parts of localised strings. Let me give you two examples:

  1. I have component with text saying 100 users already is already playing where I would like to 100 to be bold and different font size while this whole string should be localised. Therefore number 100 is parameter of localised string.

  2. I have another component which contains text If you choose to continue, you agree to our privacy policy and to our terms and conditions, where both privacy policy and terms and conditions are suppose to be bold and underlined while whole text will be again localised.

Is there a nice way how to achieve this using SwiftUI to make it work on iOS 14+?

1 Answers

You can include markdown in your localised strings like this:

"%lld users playing" = "**%lld** users already are already playing";
"legal disclaimer" = "If you choose to continue, you agree to our *Privacy Policy* and *Terms and Conditions*";

and then all you can use them directly in Text initialiser:

Text("\(100) users playing")
Text("legal disclaimer")

which looks like this:

enter image description here

Related