微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用有限行截断尾部后的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())

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。