I'm using SwiftUI but I've created a representable UITextView
.
I want to set the insets to the system default padding so it matches the rest of the app.
Here is what I'm using right now:
UITextView().textContainerInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
This looks decent but I would really prefer to utilize the default system padding, if possible.
The documentation for padding()
states the following:
length
The amount to inset this view on each edge. If nil, the amount is the system default amount.
How do I obtain the default system padding CGFloat
value?
Padding values are set using lengths or percentages, and cannot accept negative values. The initial, or default, value for all padding properties is 0 .
If you set the value to nil , SwiftUI uses a platform-specific default amount. The default value of this parameter is nil .
By utilizing .padding() on a UI element, Swift UI will place a system calculated amount of padding around the entire object. If you want to place padding on a specific side of an object, lets say the top of the object, you can do so using the following example: struct SomeView: View { var body: some view { VStack {
The object for defining space around the content in a table cell.
So, I made a rectangle with width/height as 0 and the default padding on top, then I read the height of the rectangle (which is the size of the default padding) and store it in a bound CGFloat. It also hides itself.
You could then pass that value into your UITextView wrapper.
struct Defaults: View {
@Binding var padding: CGFloat
@State var isHidden = false
@ViewBuilder
var body: some View {
if !isHidden {
Rectangle()
.frame(width: 0, height: 0)
.padding(.top)
.background(GeometryReader { geometry in
Rectangle().onAppear {
self.padding = geometry.size.height
self.isHidden = true
}
})
}
}
}
struct Example: View {
@State var padding = CGFloat()
@ViewBuilder
var body: some View {
Defaults(padding: $padding)
Rectangle().frame(width: padding, height: padding)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With