init(LocalizedStringKey , tableName : String?, bundle: Bundle?, comment: StaticString ?)
init<S>(S)
init(verbatim: String)
Availability 有効性
Technology
@frozen struct Text
A text view draws a string in your app’s user interface using a body
font that’s appropriate for the current platform. You can choose a different standard font, like title
or caption
, using the font(_:)
view modifier.
Text("Hamlet")
.font(.title)
If you need finer control over the styling of the text, you can use the same modifier to configure a system font or choose a custom font.
あなたがより優れた制御をテキストのスタイル指定に対して必要とするならば、あなたは同じ修飾子を使ってシステムフォントを構成設定するまたはあつらえのフォントを選ぶことが可能です。
You can also apply view modifiers like bold()
or italic()
to further adjust the formatting.
Text("by William Shakespeare")
.font(.system(size: 12, weight: .light, design: .serif))
.italic()
To apply styling within specific portions of the text, you can create the text view from an Attributed
, which in turn allows you to use Markdown to style runs of text. You can mix string attributes and SwiftUI modifiers, with the string attributes taking priority.
let attributedString = try! AttributedString(
markdown: "_Hamlet_ by William Shakespeare")
var body: some View {
Text(attributedString)
.font(.system(size: 12, weight: .light, design: .serif))
}
A text view always uses exactly the amount of space it needs to display its rendered contents, but you can affect the view’s layout.
テキストビューは常に正確にそれの描出された内容を表示するのにそれが必要とする空きの量を使います、しかしあなたはビューのもつレイアウトに影響を与えられます。
For example, you can use the frame(width:
modifier to propose specific dimensions to the view. If the view accepts the proposal but the text doesn’t fit into the available space, the view uses a combination of wrapping, tightening, scaling, and truncation to make it fit.
ビューはその提案を受け入れるがテキストがその利用可能な空きへと収まらないならば、ビューは、自動改行(ワードラップ)、締める、拡大縮小、そして切り詰めるの組み合わせを使ってそれを収まるようにします。
With a width of 100
points but no constraint on the height, a text view might wrap a long string:
Text("To be, or not to be, that is the question:")
.frame(width: 100)
Use modifiers like line
, allows
, minimum
, and truncation
to configure how the view handles space constraints. For example, combining a fixed width and a line limit of 1
results in truncation for text that doesn’t fit in that space:
Text("Brevity is the soul of wit.")
.frame(width: 100)
.lineLimit(1)
If you initialize a text view with a string literal, the view uses the init(_:
initializer, which interprets the string as a localization key and searches for the key in the table you specify, or in the default table if you don’t specify one.
Text("pencil") // Searches the default table in the main bundle.
For an app localized in both English and Spanish, the above view displays “pencil” and “lápiz” for English and Spanish users, respectively. If the view can’t perform localization, it displays the key instead. For example, if the same app lacks Danish localization, the view displays “pencil” for users in that locale. Similarly, an app that lacks any localization information displays “pencil” in any locale. 英語とスペイン語の両方でローカライズされたアプリに対して、上のビューは “pencil” と “lápiz” を英語とスペイン語のユーザにそれぞれ表示します。ビューがローカライゼーションを実行できないならば、それはキーを代わりに表示します。例えば、同じアプリがデンマーク語ローカライゼーションを欠いているならば、ビューは “pencil” をユーザにそのロケールにおいて表示します。同様に、あらゆるローカライゼーション情報を欠いているアプリは、“pencil” をどんなロケールにおいても表示します。
To explicitly bypass localization for a string literal, use the init(verbatim:)
initializer.
Text(verbatim: "pencil") // Displays the string "pencil" in any locale.
If you intialize a text view with a variable value, the view uses the init(_:)
initializer, which doesn’t localize the string. However, you can request localization by creating a Localized
instance first, which triggers the init(_:
initializer instead:
// Don't localize a string variable...
Text(writingImplement)
// ...unless you explicitly convert it to a localized string key.
Text(LocalizedStringKey(writingImplement))
When localizing a string variable, you can use the default table by omitting the optional initialization parameters — as in the above example — just like you might for a string literal. ある文字列変数をローカライズする場合、あなたは随意である初期化パラメータを省略することによって省略時のテーブルを使用できます — 上の例でのように — あなたが文字列リテラルにしたかもしれないように。
init(LocalizedStringKey , tableName : String?, bundle: Bundle?, comment: StaticString ?)
init<S>(S)
init(verbatim: String)
init(AttributedString )
init(ClosedRange <Date>)
init(DateInterval )
init(Date, style: Text.DateStyle )
init<F>(F.FormatInput , format: F)
init<Subject>(Subject, formatter: Formatter)
init<Subject>(Subject, formatter: Formatter)
init(Image)
Image
, suitable for concatenating with other Text
func font(Font?) -> Text
func fontWeight (Font.Weight?) -> Text
func foregroundColor (Color?) -> Text
func bold() -> Text
func italic() -> Text
func strikethrough(Bool, color: Color?) -> Text
func underline(Bool, color: Color?) -> Text
func monospacedDigit () -> Text
func kerning(CGFloat) -> Text
func tracking(CGFloat) -> Text
func baselineOffset (CGFloat) -> Text
func textCase (Text.Case?) -> some View
func allowsTightening (Bool) -> some View
func minimumScaleFactor (CGFloat) -> some View
func truncationMode (Text.TruncationMode ) -> some View
func lineLimit (Int?) -> some View
func lineSpacing (CGFloat) -> some View
func multilineTextAlignment (TextAlignment ) -> some View
func flipsForRightToLeftLayoutDirection (Bool) -> some View
func speechAdjustedPitch (Double) -> Text
func speechAlwaysIncludesPunctuation (Bool) -> Text
func speechAnnouncementsQueued (Bool) -> Text
func speechSpellsOutCharacters (Bool) -> Text
func accessibilityHeading (AccessibilityHeadingLevel ) -> Text
func accessibilityLabel <S>(S) -> Text
func accessibilityLabel (Text) -> Text
func accessibilityLabel (LocalizedStringKey ) -> Text
func accessibilityTextContentType (AccessibilityTextContentType ) -> Text
static func + (Text, Text) -> Text
static func == (Text, Text) -> Bool
static func != (Self, Self) -> Bool
struct Label
protocol TextSelectability