見出し画像

Swiftで行こう!--SwiftUIもっと!

もう少し詳しくSwiftUIみていきます。

世界は広い!すごくよくまとめられて勉強になります。

自分でもPlaygroundで試しながら、確認していきたいと思います。

まずテキストの処理です。基本の形です。

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
   var body: some View {
       Text("Hello!SwiftUI!!")
   }
}
let content = ContentView()
PlaygroundPage.current.liveView = UIHostingController(rootView: content)
import SwiftUI
import PlaygroundSupport

大事なインポートです。

let content = ContentView()
PlaygroundPage.current.liveView = UIHostingController(rootView: content)

最後の2行はプレビュー表示させるためのコードですね。

SwiftUIに関わるコードは

var body: some View {
Text("Hello!SwiftUI!!")
}

のみです。これで、iOS,appleWatch,appleTVについては同じように表示されるはずです。PlaygroundではiOSの形ですが、同じコードで3つのデバイスで同じように使えます。これがSwiftUIの良いところで、楽ができる仕様となっています。

 Text("This is an extremely long string that will never fit even the widest of Phones")
       .lineLimit(1)

と.lineLimit(1)とすると1行のみ表示されるようになります。.lineLimit(3)としてやると3行目まで表示されます。

Text("This is an extremely long string that will never fit even the widest of Phones")
       .truncationMode(.middle)

とすると真ん中を切り詰めます。"truncation"が切り詰めるという意味のようです。

こんな感じです。

あと細かい設定としては"."で繋いでいきます。

 var body: some View {
       Text("This is an extremely long string that will never fit even the widest of Phones")
       .font(.largeTitle)    // 大きく
       .bold()               // 太く
       .color(.red)          // 赤く
       .lineSpacing(10)      // 行間
       .lineLimit(nil)       // 行数指定
   }


この記事が気に入ったらサポートをしてみませんか?