You can use the GeometryReader
and Text
modifiers to get the width of a Text
component in SwiftUI. Here's an example:
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
Text("Hello, World!")
.font(.largeTitle)
.background(Color.yellow)
.onAppear {
print("Width of Text component: \(geometry.size.width)")
}
}
}
}
In this example, we use the GeometryReader
to get the size of the containing view, which we can then use to get the width of the Text
component. We add a background color to the Text
component using the .background
modifier to make it easier to see the component's bounds. Finally, we use the .onAppear
modifier to print the width of the component when it appears on the screen.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-05 06:54:46 +0000
Seen: 12 times
Last updated: Jun 05 '23
How to add a custom button in SwiftUI for iOS13 that enables going back between screens?
How can the update of properties from both ParentViewModel and ChildViewModel in SwiftUI be solved?
Can SwiftUI be utilized to restrict the touch region for a drag gesture?
How can the color of a selected element (Picker) be modified in SwiftUI for WatchOS?
How to make a calendar layout that is horizontal using Swiftui?
What is the process to add a radio button with a check mark on a TableView cell using Swift?
How to limit UITextField input to only accept numerical values?