Padding, Spacer, Initializer 的使用

news/2024/7/20 22:14:28 标签: iOS, Swift, UI

1. Padding 的使用

  1.1 样式一

    1) 实现

Swift">func testText1()-> some View{
    Text("Hello, World!")
        .background(Color.yellow) // 背景颜色
        //.padding()              // 默认间距
        .padding(.all, 10)        // 所有的间距
        .padding(.leading, 20)    // 开始的间距
        .background(Color.blue)   // 背景
}

    2) 效果图:

  1.2 文本样式 2

    1) 实现

Swift">/// 文本样式 2
func testText2()-> some View{
    Text("Hell, World!")
        .font(.largeTitle)     // 字体大小
        .fontWeight(.semibold) // 权重
        .frame(maxWidth: .infinity, alignment: .leading) // 宽,对齐方式
        .background(Color.red) // 颜色
        .padding(.leading, 30) // 间距
}

    2) 效果图:

  1.3 文本样式 3

    1) 实现

Swift">/// 文本样式 3
func testTest3()->some View{
    VStack(alignment: .leading) {
        Text("Hell, World!")
            .font(.largeTitle)     // 字体大小
            .fontWeight(.semibold) // 权重样式
            .padding(.bottom, 20)  // 向下间距
        
        Text("This is the description of what we will do on this screen. It is multiple lines and we will align the text to the leading edge.") // 文本
    }
    .padding() // 默认间距
    .padding(.vertical, 10) // 垂直间距
    .background(
        Color.white
            .cornerRadius(10)
            .shadow(color: Color.black.opacity(0.3), radius: 10, x: 0.0, y: 10)
    ) // 背景: 圆角,阴影
    .padding(.horizontal, 10) // 水平间距
}

    2) 效果图:

2. Spacer 的使用

  2.1 垫片水平堆栈

    1) 实现

Swift">// 垫片水平堆栈
func spacerHStack() -> some View{
    HStack(spacing: 0){
        //垫片,将控件向两边推 nil == 8/10
        Spacer(minLength: 0)
            .frame(height: 10)
            .background(Color.orange)
        Rectangle()
            .frame(width: 50, height: 50) // 矩形
        Spacer()
            .frame(height: 10)
            .background(Color.orange)
        Rectangle()
            .fill(Color.red)
            .frame(width: 50, height: 50)
        Spacer()
            .frame(height: 10)
            .background(Color.orange)
        Rectangle()
            .fill(Color.green)
            .frame(width: 50, height: 50)
        //垫片,将控件向两边推
        Spacer()
            .frame(height: 10)
            .background(Color.orange)
    }
    .background(Color.yellow)
    //.padding(.horizontal, 200)
    .background(Color.blue)
}

    2) 效果图:

  2.2 垫片 VH 堆栈

    1) 实现

Swift">// 垫片 VH 堆栈
func spacerVHStack() -> some View{
    VStack {
        HStack(spacing: 0){
            Image(systemName: "xmark") // 图片
            Spacer()// 垫片
                .frame(height: 2)
                .background(Color.orange)
            Image(systemName: "gear")  // 图片
        }
        .font(.title)
        //.background(Color.yellow)
        .padding(.horizontal)
        //.background(Color.blue)
        
        Spacer()// 垫片
            .frame(width: 2)
            .background(Color.orange)
        
        Rectangle()
            .frame(height: 55)
    }
    //.background(Color.yellow)
}

    2) 效果图:

3. Initializer 的使用

  3.1 实现

Swift">/// 初始化与枚举
struct InitializerBootcamp: View {
    let backgroundColor: Color
    let count: Int
    let title: String
    
    init(count: Int, fruit: Fruit) {
        self.count = count;
        if fruit == .apple{
            self.title = "Apples"
            self.backgroundColor = .red
        }else{
            self.title = "Oranges"
            self.backgroundColor = .orange
        }
    }
    
    /// 枚举
    enum Fruit{
        case apple
        case orange
    }
    
    var body: some View {
        // 垂直堆栈
        VStack(spacing: 15){
            Text("\(count)")
                .font(.largeTitle)
                .foregroundColor(.white)
                .underline()
            
            Text(title)
                .font(.headline)
                .foregroundColor(.white)
        }
        .frame(width: 150, height: 150)
        .background(backgroundColor)
        .cornerRadius(10)
    }
}

struct InitializerBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        HStack {
            InitializerBootcamp(count: 58, fruit: .orange)
            InitializerBootcamp(count: 23, fruit: .apple)
        }
    }
}

  3.2 效果图:


http://www.niftyadmin.cn/n/339748.html

相关文章

Java IO 基础知识总结

Java IO基础知识总结 1、IO流简介 IO即 Input/Output ,输入和输出。数据输入到计算机内存的过程即输入,反之输出到外部存储(比如数据库、文件、远程主机)的过程即输出。数据传输过程类似于水流,因此称为IO流。IO流在…

Yolov5轻量化:EMO,结合 CNN 和 Transformer 的现代倒残差移动模块设计,性能优于EdgeViT、Mobile-former等网络

论文: https://arxiv.org/pdf/2301.01146.pdf 🏆🏆🏆🏆🏆🏆Yolo轻量化模型🏆🏆🏆🏆🏆🏆 重新思考了 MobileNetv2 中高效的倒残差模块 Inverted Residual Block 和 ViT 中的有效 Transformer 的本质统一,归纳抽象了 MetaMobile Block 的一般概念。受这…

ubuntu18安装中文环境

如果你在Ubuntu 18.04安装过程中选择了中文语言环境,但是在启动后却出现了英文界面,可能是因为Ubuntu的语言设置没有被正确识别。你可以尝试以下方法来解决这个问题: 修改语言环境设置: 打开终端(Terminal&#xff0…

数据结构篇六:二叉树

文章目录 前言1. 树的概念及结构1.1 树的概念1.2 树的相关概念1.3 树的结构 2. 二叉树的概念及结构2.1 二叉树的概念2.2 特殊的二叉树2.3 二叉树的性质2.4 二叉树的存储结构 3. 二叉树的顺序结构及实现3.1 二叉树的顺序结构3.2 堆的概念及结构3.3 堆的实现3.3.1 堆的创建3.3.2 …

【K哥爬虫普法】你很会写爬虫吗?10秒抢票、10秒入狱,了解一下?

我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K 哥特设了“K哥爬虫普法”专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识,知晓如何合法合规利用…

(5.12-5.18)【大数据新闻速递】

关 注gzh“大数据食铁兽”,了解更多大数据快讯 【打造全国首个数据要素产业集聚区!浦东数据要素产业规模2025年将达1000亿元】 5月16日,“数启浦东”2023浦东新区数据要素产业主题系列活动启动。记者获悉,《张江数据要素产业集聚…

pytorch rpc如何实现分物理机器实现model parallel

因为业务需要,最近接到一项任务,是如何利用pytorch实现model parallel以及distributed training。搜罗了网上很多资料,以及阅读了pytorch官方的教程,都没有可参考的案例。讲的比较多的是data parallel,关于model paral…

上门家教预约小程序开发 良师就在你身边

社会的发展科技的进步让人们对教育的重视度也逐渐升高,很多家长可以说是为了孩子的教育操碎了心。在学校还好有老师辅导,节假日在家的时候,很多家长自己本身文化知识有限或者工作繁忙没有时间辅导,送去辅导班来回接送又很麻烦&…