init<S>(S)
init(verbatim: String)
Availability 有効性
Technology
init(_ key: LocalizedStringKey
, tableName: String
? = nil, bundle: Bundle
? = nil, comment: StaticString
? = nil)
key
The key for a string in the table identified by table
.
tableName
The name of the string table to search.
検索する文字列テーブルの名前。
If nil
, use the table in the Localizable
file.
bundle
The bundle containing the strings file.
文字列ファイルを含んでいるバンドル。
If nil
, use the main bundle.
comment
Contextual information about this key-value pair. このキー値ペアについての前後関係情報。
Use this initializer to look for the key
parameter in a localization table and display the associated string value in the initialized text view. If the initializer can’t find the key in the table, or if no table exists, the text view displays the string representation of the key instead.
イニシャライザがキーをテーブルの中で見つけられないならば、またはテーブルが存在しないならば、テキストビューはキーの文字列表現を代わりに表示します。
Text("pencil") // Localizes the key if possible, or displays "pencil" if not.
When you initialize a text view with a string literal, the view triggers this initializer because it assumes you want the string localized, even when you don’t explicitly specify a table, as in the above example. If you haven’t provided localization for a particular string, you still get reasonable behavior, because the initializer displays the key, which typically contains the unlocalized string. あなたがあるテキストビューを文字列リテラルで初期化する場合、ビューはこのイニシャライザを引き起こします、なぜならそれはあなたが望むのはローカライズされた文字列だと仮定するからです、あなたが明示的にあるテーブルを指定しない場合でさえ、上の例でのように。あなたがローカライゼーションを特定の文字列に対して提供しなかったとしても、あなたは依然として理にかなった挙動を得ます、なぜならイニシャライザはキーを表示するからです、それは概してローカライズされない文字列を含みます。
If you initialize a text view with a string variable rather than a string literal, the view triggers the init(_:)
initializer instead, because it assumes that you don’t want localization in that case. If you do want to localize the value stored in a string variable, you can choose to call the init(_:
initializer by first creating a Localized
instance from the string variable:
Text(LocalizedStringKey(someString)) // Localizes the contents of `someString`.
If you have a string literal that you don’t want to localize, use the init(verbatim:)
initializer instead.
If the localized string or the fallback key contains Markdown, the view displays the text with appropriate styling. For example, consider an app with the following entry in its Spanish localization file:
"_Please visit our [website](https://www.example.com)._" = "_Visita nuestro [sitio web](https://www.example.com)._";
You can create a Text
view with the Markdown-formatted base language version of the string as the localization key, like this:
Text("_Please visit our [website](https://www.example.com)._")
When viewed in a Spanish locale, the view uses the Spanish text from the strings file, applying the Markdown styling.
Important 重要
Text
doesn’t render all styling possible in Markdown. It doesn’t support line breaks, soft breaks, or any style of paragraph- or block-based formatting like lists, block quotes, code blocks, or tables. It also doesn’t support the image
attribute. Parsing with SwiftUI treats any whitespace in the Markdown string as described by the Attributed
parsing option.
init<S>(S)
init(verbatim: String)