Jetpack Compose自定义textField文本间距问题

huangapple go评论58阅读模式
英文:

Jetpack Compose custom textField text spacing issue

问题

I need to make a custom textfield but i can't get the right spacing above the text. I do not want padding in the textfield but i want spacing above the text so that when i scroll in the textfield the first and last line will not be stopped at the edge of the field.

我需要创建一个自定义文本字段,但我无法得到正确的文本上方间距。我不希望在文本字段中添加内边距,但我希望在文本上方有间距,这样当我在文本字段中滚动时,第一行和最后一行不会停在字段的边缘。

I do want the text to scroll through the border as it does now.

我确实希望文本像现在一样滚动穿过边框。

How can i add spacing to the text?

如何为文本添加间距?

I am using a custom BasicTextField:

我正在使用自定义的 BasicTextField:

BasicTextField(
value = value,
modifier = modifier
.background(Color.Transparent, shape = shape)
.indicatorLine(enabled, isError, interactionSource, colors),
onValueChange = onValueChange,
enabled = enabled,
readOnly = readOnly,
textStyle = mergedTextStyle,
cursorBrush = SolidColor(colors.cursorColor(isError).value),
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
interactionSource = interactionSource,
singleLine = singleLine,
maxLines = maxLines,
minLines = minLines,
decorationBox = @Composable { innerTextField ->
// places leading icon, text field with label and placeholder, trailing icon

    TextFieldDefaults.TextFieldDecorationBox(
        value = value,
        visualTransformation = visualTransformation,
        innerTextField = innerTextField,
        placeholder = placeholder,
        label = label,
        leadingIcon = leadingIcon,
        trailingIcon = trailingIcon,
        singleLine = singleLine,
        enabled = enabled,
        isError = isError,
        interactionSource = interactionSource,
        colors = colors,
        contentPadding = TextFieldDefaults.outlinedTextFieldPadding(
            start = Spacing.spacing2,
            top = 0.dp,
            end = Spacing.spacing2,
            bottom = 0.dp
        )
    )
}

)

This is how far i've come.

这就是我目前的进展。
Jetpack Compose自定义textField文本间距问题

这是我已经完成的部分。

英文:

I need to make a custom textfield but i can't get the right spacing above the text. I do not want padding in the textfield but i want spacing above the text so that when i scroll in the textfield the first and last line will not be stopped at the edge of the field.

I do want the text to scroll through the border as it does now.

How can i add spacing to the text?

I am using a custom BasicTextField:

BasicTextField(
    value = value,
    modifier = modifier
        .background(Color.Transparent, shape = shape)
        .indicatorLine(enabled, isError, interactionSource, colors),
    onValueChange = onValueChange,
    enabled = enabled,
    readOnly = readOnly,
    textStyle = mergedTextStyle,
    cursorBrush = SolidColor(colors.cursorColor(isError).value),
    visualTransformation = visualTransformation,
    keyboardOptions = keyboardOptions,
    keyboardActions = keyboardActions,
    interactionSource = interactionSource,
    singleLine = singleLine,
    maxLines = maxLines,
    minLines = minLines,
    decorationBox = @Composable { innerTextField ->
        // places leading icon, text field with label and placeholder, trailing icon

        TextFieldDefaults.TextFieldDecorationBox(
            value = value,
            visualTransformation = visualTransformation,
            innerTextField = innerTextField,
            placeholder = placeholder,
            label = label,
            leadingIcon = leadingIcon,
            trailingIcon = trailingIcon,
            singleLine = singleLine,
            enabled = enabled,
            isError = isError,
            interactionSource = interactionSource,
            colors = colors,
            contentPadding = TextFieldDefaults.outlinedTextFieldPadding(
                start = Spacing.spacing2,
                top = 0.dp,
                end = Spacing.spacing2,
                bottom = 0.dp
            )
        )
    }

This is how far i've come.
Jetpack Compose自定义textField文本间距问题

答案1

得分: 1

以下是您要翻译的内容:

"不确定是否将可滚动列放入装饰框中是最好的解决方案,但我已经试验了一段时间,这是我想出的方法。

内容正在像您希望的那样通过边框滚动,而且也不会在边缘结束。这是简化版本:

val text = remember { mutableStateOf("") }
val interactionSource = remember { MutableInteractionSource() }

BasicTextField(
    value = text.value,
    onValueChange = { targetValue ->
        text.value = targetValue
    },
    modifier = Modifier
        .fillMaxWidth()
        .height(48.dp),
    interactionSource = interactionSource,
    decorationBox = @Composable { innerTextField ->
        Column(
            modifier = Modifier
                .verticalScroll(rememberScrollState())
                .padding(all = 16.dp),
        ) {
            innerTextField()
        }
    }
)

这个解决方案的核心是我没有指定最大行数,而是将高度限制为48.dp(或其他任何值)。这样,innerTextfield 组件实际上是完全布局的,如果有必要,它会超出父容器的大小。

不过,这种解决方案的问题是在输入时,文本不会自动滚动。但我认为,如果在值更改时加入一些滚动逻辑,它可能会起作用。

希望这至少有点帮助。 :)"

英文:

Not sure, if putting a scrollable column into the decoration box is the nicest solution but I was playing with it for a while now and this is what I came up with.

The content is scrolling through the border as you wished and it also does not end at the edge. Here is the simplified version:

val text = remember { mutableStateOf("") }
val interactionSource = remember { MutableInteractionSource() }

BasicTextField(
    value = text.value,
    onValueChange = { targetValue ->
        text.value = targetValue
    },
    modifier = Modifier
        .fillMaxWidth()
        .height(48.dp),
    interactionSource = interactionSource,
    decorationBox = @Composable { innerTextField ->
        Column(
            modifier = Modifier
                .verticalScroll(rememberScrollState())
                .padding(all = 16.dp),
        ) {
            innerTextField()
        }
    }
)

The core of this solution is that I am not specifying max number of lines but I constraint the height to be 48.dp (or any other) instead. This way, the innerTextfield composable is actually fully laid out and is overflowing the parent's size if that makes sense.

The problem with this solution though is that when you are typing, the text is not automatically scrolling. But I think it could work, if you put some scrolling logic whenever the value is changed.

Hope this helps at least a litte. Jetpack Compose自定义textField文本间距问题

huangapple
  • 本文由 发表于 2023年5月11日 15:58:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定