"margin" and "padding" properties of View

The "margin" property determines the outer margins from this View to its neighbors.The "padding" property sets the padding from the border of the View to the content.The values of the "margin" and "padding" properties are stored as the BoundsProperty interface,which implements the Properties interface (see above). BoundsProperty has 4 SizeUnit properties:

Property Constant Description
"top" rui.Top Top padding
"right" rui.Right Right padding
"bottom" rui.Bottom Bottom padding
"left" rui.Left Left padding

The NewBoundsProperty function is used to create the BoundsProperty interface. Example

view.Set(rui.Margin, NewBoundsProperty(rui.Params {
	rui.Top:  rui.Px(8),
	rui.Left: "@topMargin",
	"right":   "1.5em",
	"bottom":  rui.Inch(0.3),
})))

Accordingly, if you request the "margin" or "padding" property using the Get method, the BoundsProperty interface will return:

if value := view.Get(rui.Margin); value != nil {
	margin := value.(BoundsProperty)
}

BoundsProperty using the "Bounds (session Session) Bounds" function of the BoundsProperty interfacecan be converted to a more convenient Bounds structure:

type Bounds struct {
	Top, Right, Bottom, Left SizeUnit
}

Global functions can also be used for this:

func GetMargin(view View, subviewID ...string) Bounds
func GetPadding(view View, subviewID ...string) Bounds

The textual representation of the BoundsProperty is as follows:

"_{ top = , right = , bottom = , left =  }"

The value of the "margin" and "padding" properties can be passed to the Set method:

  • BoundsProperty interface or its textual representation;
  • Bounds structure;
  • SizeUnit or the name of a constant of type SizeUnit, in which case this value is set to all indents. Those.
view.Set(rui.Margin, rui.Px(8))

equivalent to

view.Set(rui.Margin, rui.Bounds{Top: rui.Px(8), Right: rui.Px(8), Bottom: rui.Px(8), Left: rui.Px(8)})

Since the value of the "margin" and "padding" property is always stored as the BoundsProperty interface,if you read the "margin" or "padding" property set by the Bounds or SizeUnit with the Get function,then you get the BoundsProperty, not the Bounds or SizeUnit.

The "margin" and "padding" properties are used to set four margins at once.The following properties are used to set individual paddings:

Property Constant Description
"margin-top" rui.MarginTop The top margin
"margin-right" rui.MarginRight The right margin
"margin-bottom" rui.MarginBottom The bottom margin
"margin-left" rui.MarginLeft The left margin
"padding-top" rui.PaddingTop The top padding
"padding-right" rui.PaddingRight The right padding
"padding-bottom" rui.PaddingBottom The bottom padding
"padding-left" rui.PaddingLeft The left padding

Example

view.Set(rui.Margin, rui.Px(8))
view.Set(rui.TopMargin, rui.Px(12))

equivalent to

view.Set(rui.Margin, rui.Bounds{Top: rui.Px(12), Right: rui.Px(8), Bottom: rui.Px(8), Left: rui.Px(8)})