使用有限行截断尾部后的Swift UILabel放置按钮

问题描述

我是 Swift 新手,目前正在做一个项目。 我想模仿的一个例子是 youtube,当评论太长时,在截断尾部后会出现“阅读更多”按钮(?)。我想为截断的评论使用相同类型的按钮,但我找不到方法

我找到的最接近的答案是在这链接上,但我目前正在研究故事板和代码,所以我很难理解。 Decrease the width of the last line in multiline UILabel

Youtube comment capture

如果有人以前这样做过或知道如何做到这一点,那么分享您的技巧会很有帮助。

解决方法

struct ContentView: View {
    @State var showDetail = false
    var body: some View {
        HStack(alignment:.lastTextBaseline,spacing:-1){
            Text("There are many variations of passages of Lorem Ipsum available,but the majority have suffered alteration in some form,by injected humour,or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum,you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary,making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words,combined with a handful of model sentence structures,to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition,injected humour,or non-characteristic words etc.dadasdsadsadsadsadsadsadsadsadasdsad")
            Button(action: {
                self.showDetail.toggle()
            },label: {
                HStack(spacing:0){
                    
                    Text("More")
                        .font(.caption)
                        .foregroundColor(.black)
                    Image(systemName: "chevron.down")
                    
                }.font(.caption)
                .foregroundColor(.black)
            })
        }.frame(width: 300,height: self.showDetail == true ? 500 : 300)
        .animation(.spring())

    }
}