ios – Why frame() can be used in Text


How can I understand the relationship between modifier and struct/extension in SwiftUI?

Here is a simple code snippet using frame() to control the size of the View:

struct ContentView: View {
  var body: some View {
    Text("Hello World")
      .frame(width: 200, height: 100)
  }
}

When I trace the .frame() method in XCode to its definition, I find that it is a method defined in extension View:

extension View {
  // ...
  @inlinable public func frame(width: CGFloat? = nil, height: CGFloat? = nil, alignment: Alignment = .center) -> some View
}

My question is: Why can the .frame() method be used directly on Text()? I checked, and Text itself does not implement the View protocol:

@frozen public struct Text : Equatable, Sendable

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img