The magic of fixed size modifier in SwiftUI

栏目: IT技术 · 发布时间: 5年前

内容简介:You might saw some examples of fixed size modifier usages while trying to fix the issue with multiline text inWe try to simulate the rendering of very long text. I also use the frame modifier to limit available space. As you can see in the example above, t

You might saw some examples of fixed size modifier usages while trying to fix the issue with multiline text in SwiftUI . But do you know what exactly fixed size modifier does? How does it work? Today I want to talk about all the magic and power behind the fixed size modifier.

Basics

SwiftUI documentation says that the fixed size modifier fixes this view at its ideal size. I think the best way to understand how the fixed size modifier works is the visual example. Let’s take a look at a small sample.

struct Example1: View {
    var body: some View {
        Text("Very very very long text!")
            .font(.title)
            .frame(width: 100, height: 100, alignment: .center)
            .border(Color.red)
    }
}

We try to simulate the rendering of very long text. I also use the frame modifier to limit available space. As you can see in the example above, the text doesn’t fit the available space, and the SwiftUI layout system decides to truncate it.

The magic of fixed size modifier in SwiftUI

To learn more about the logic behind the layout system of SwiftUI, take a look at my “Layout priorities in SwiftUI” post .

Now let’s take a look at the same example but with the fixed size modifier attached to the text component.

struct Example1: View {
    var body: some View {
        Text("Very very very long text!")
            .font(.title)
            .fixedSize()
            .frame(width: 100, height: 100, alignment: .center)
            .border(Color.red)
    }
}

As you can see in the code sample above, the fixed size modifier allows to text to ignore the frame and use as much space as needed to render the content. The text component simply exceeds the provided frame by continuing the rendering outside of the frame. You can use the clipped modifier after the frame modifier to cut the content inside the provided rectangle.

The magic of fixed size modifier in SwiftUI

To learn more about clipping and changing the shape of a view, take a look at my “Customizing the shape of views in SwiftUI” post .

SwiftUI provides us two versions of fixed size modifier. The first one, which we already used in previous examples, doesn’t have any parameters and allows the view to grow both vertically and horizontally. The second variant of the fixed size modifier accepts two boolean parameters. These parameters describe the axis of expansion.

struct Example1: View {
    var body: some View {
        Text("Very very very long text!")
            .font(.title)
            .fixedSize(horizontal: true, vertical: false)
            .frame(width: 100, height: 100, alignment: .center)
            .border(Color.red)
    }
}

Here we use the fixed size modifier to allow the view to expand horizontally. This level of control lets us use the fixed size modifier as an alternative to layout priorities in SwiftUI . Let’s take a look at another example.

struct Example2: View {
    var body: some View {
        HStack {
            Text("Hello World!")
                .font(.headline)
            Text("Hello World!")
                .font(.largeTitle)
            Text("Hello World!")
                .font(.title)
        }.lineLimit(1)
    }
}

In the example above, we have the horizontal stack that contains three text labels. The layout system can’t render all of them without truncating because labels have pretty big font sizes. Assume that we want to display the second label without truncating and allow to SwiftUI cut other labels. We have at least two solutions here. The first one is layout priority, and the second one is the fixed size modifier.

struct Example2: View {
    var body: some View {
        HStack {
            Text("Hello World!")
                .font(.headline)
            Text("Hello World!")
                .font(.largeTitle)
                .fixedSize(horizontal: true, vertical: false)
            Text("Hello World!")
                .font(.title)
        }.lineLimit(1)
    }
}

Conclusion

As you can see, the fixed size modifier provides us much more control over the layout system than layout priorities. Sometimes it can be more beneficial when you need to control the growth of the view in some specific direction. I hope you enjoy the post. Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading, and see you next week!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

点击的奥秘:运用说服心理术提升在线影响力(全彩)

点击的奥秘:运用说服心理术提升在线影响力(全彩)

Nathalie Nahai(娜塔莉.纳海) / 陈旭 / 电子工业出版社 / 2014-9-1 / 75.00元

用户的每一次点击,不管是在虚拟商店购物,还是在浏览企业网站,或是漫无目的地把玩手机,都蕴藏着基于心理学的无穷奥秘。《点击的奥秘:运用说服心理术提升在线影响力》作者为全球知名的网络心理学家,其在《点击的奥秘:运用说服心理术提升在线影响力》中将心理学、神经科学及行为经济学巧妙地结合在一起,挖掘和提炼出一套行之有效的网络用户引导策略——既涵盖在线说服最新研究动向,也包括最前沿的科技成果,以及其他诸多惊人......一起来看看 《点击的奥秘:运用说服心理术提升在线影响力(全彩)》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具