SwiftUI—Text视图的填充属性
2020-07-10 本文已影响0人
anny_4243
原文链接:https://github.com/fzhlee/SwiftUI-Guide#4Text-Padding
内部填充属性,修改文字内容和文本视图边框之间的距离,即修改文本视图的上下左右的内边距。
复用padding特性,制作轮廓效果~
示例代码:
VStack{
Text("www.hdjc8.com")
.background(Color.black)
.foregroundColor(.white)
.padding(20) //设置填充属性的值为20,增加文本视图与文字内容的间距
Text("www.hdjc8.com")
.padding()
.background(Color.black)
.foregroundColor(.white) //链式调用的顺序由上而下,先设置内边距,再设置背景颜色和文字颜色,这时内边距也会拥有相应的背景颜色
Text("Swift User Interface")
.font(.largeTitle) //设置文字内容的样式为巨型标题样式,以突出显示文字内容
.foregroundColor(.black)
.padding(15)
.background(Color.yellow)
.padding(15) //在文本视图的外围再次增加15点的间距
.background(Color.orange) //给新的内边距设置填充颜色为橙色
.padding(10) //在文本视图的外围再次增加10点的间距
.background(Color.red) //给新的内边距设置填充颜色为红色
}

注:
链式编程的原理是调用一个属性或者方法的返回值是调用者本身。使得其可以继续调用本身中的方法或者属性。