0

在此处输入图像描述

任何人都知道如何在边框和助手/计数器视图之间添加“空格”?我一直在网上搜索,找不到任何答案。

我正在使用 MaterialComponents 72.2.0

更新

var noteController: MDCTextInputControllerOutlined!
...
override func viewDidLoad() {
    super.viewDidLoad()
    ...
    let scheme = MDCContainerScheme()
    scheme.colorScheme.primaryColor = .orange
    noteController = MDCTextInputControllerOutlined(textInput: noteField)
    noteController.applyTheme(withScheme: scheme)
    noteController.characterCountMax = 60
    noteController.characterCountViewMode = .always
    noteController.helperText = "Hello World"
    ...
}

noteController.applyTheme(withScheme: scheme)我到处玩,发现如果我删除助手字符计数器会正确显示。我只是按照这里MDCContainerScheme建议MDCTextInputController.applyTheme(withScheme:)将自定义主题应用于MDCTextField.

我发现的另一件事是,如果我禁用字符计数器并且只显示它正在正确显示的助手。

4

2 回答 2

0

现在不需要使用material-components-ios. 我们可以使用SwiftUI下面的简单代码创建它

import SwiftUI

struct ContentView: View {
    let labels = ["First Name", "Last Name", "Phone number", "Email address", "Postal Address"]
    @State private var values = Array(repeating:"", count: 5)
    
    var body: some View {
        List(0..<5){ index in
            FloatingTextField(title: self.labels[index], text: $values[index])
        }.listStyle(InsetGroupedListStyle())
    }
}

struct FloatingTextField: View {
    let title: String
    let text: Binding<String>
    
    var body: some View {
        ZStack(alignment: .leading, content: {
            Text(title)
                .foregroundColor(Color(.placeholderText))
                .opacity(text.wrappedValue.isEmpty ? 0 : 1)
                .offset(y: text.wrappedValue.isEmpty ? 0 : -25)
                .scaleEffect(text.wrappedValue.isEmpty ? 1: 0.8, anchor: .leading)
            TextField(title, text: text)
        })
        .padding(.top, 15)
        .animation(.spring(response: 0.4, dampingFraction: 0.3))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

于 2020-11-10T08:03:09.880 回答
0

现在我刚刚删除noteController.applyTheme(withScheme: scheme)并替换为:

noteController.activeColor = .orange
noteController.floatingPlaceholderActiveColor = .orange

我不知道为什么,但如果我使用它,它工作正常。

于 2020-10-22T06:50:15.350 回答