SwiftUI: How do I make TextField fit multi-line content?

吃可爱长大的小学妹 提交于 2020-03-23 06:54:08

问题


In the attached code example I get a lot of extra top-spacing in my TextField. If I change the content to only be a single line, say "content", then it fits snugly. How can I get the same tight-fitting behaviour the single line has for a multi-line text?

Previews and code were made with Xcode 11.1 / Swift 5.1

import SwiftUI

struct TextFieldDemo: View {
    var content: Binding<String>

    init(content: Binding<String>) {
        self.content = content
    }

    var body: some View {
        TextField("Custom placeholder", text: content)
            .background(Color.yellow)
    }
}

#if DEBUG
struct TextInputRowPreviews: PreviewProvider {

    static var previews: some View {
        let content = "content\ncontent\ncontent\ncontent\ncontent\ncontent"
        return TextFieldDemo(content: .constant(content))
                .previewLayout(.sizeThatFits)
    }
}
#endif

Here is the example if I change the "let content" line to

let content = "content"


回答1:


It seems there's no direct argument to manage multiline padding correctly. They are maybe underdevelopping. But the following will give you a straight workaround solution to what you are expecting.

extension String{
    var extraLines : String{ get{
         return self +  String(repeating:"\n",  count: self.components(separatedBy:  "\n").count - 1)
    }}
 }


struct TextFieldDemo: View {
var content: Binding<String>

init(content: Binding<String>) {
    self.content = content
}

@State var height : CGFloat? //current height

let constHeightRatio : CGFloat = 0.55 //use for assembly with other fonts.
let defaultHeight : CGFloat = 250 //use for assembly with other views.

var body: some View {
    TextField("Custom placeholder", text: content).environment(\.multilineTextAlignment, .center).alignmentGuide(.bottom) { (ViewDimensions) -> CGFloat in
        if self.height == nil {self.height = ViewDimensions.height}
            return  ViewDimensions.height
    }.frame( height: (height ?? defaultHeight) * constHeightRatio, alignment: .bottom).background(Color.yellow)
}
}



#if DEBUG
struct TextInputRowPreviews: PreviewProvider {

static var previews: some View {
    let content = "content\ncontent\ncontent".extraLines
    return
         TextFieldDemo(content: .constant(content))
}
}
#endif

This works fine for single view. If view assembly is required (with other stacking views, etc), you may adjust defaultHeight and/or constHeightRatio to achieve what you want. Hopefully it works for you too.



来源:https://stackoverflow.com/questions/58555358/swiftui-how-do-i-make-textfield-fit-multi-line-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!