Kivy TextInput 水平和垂直对齐(居中文本)
Kivy TextInput horizontal and vertical align (centering text)
如何在 Kivy 的 TextInput 中将文本水平居中?
我有以下屏幕:
但我想像这样集中我的文字:
这是我的 kv 语言的一部分:
BoxLayout:
orientation: 'vertical'
Label:
markup: True
text: '[b] Type something... [/b]'
size_hint: 1, 0.6
size: self.parent.size[0], 200
font_size: self.size[0] * 0.1
text_size: self.size
halign: 'center'
valign: 'middle'
canvas.before:
Color:
rgb: 0, 0, 204
Rectangle:
pos: self.pos
size: self.size
TextInput:
focus: True
如何使 TextInput 的文本居中?
Afaik,没有像 Label
中那样对齐的东西,但是,您可以使用 padding
将位置推到任何您想要的位置。请记住,更改文本的大小会影响居中,因此您需要重新计算大小的更改(例如,在使用多个设备、大小等时)。
或者甚至可以有一个解决方法,您可以让 TextInput
不可见,使用 Label
获取触摸事件以触发 TextInput
(这将打开键盘)并更改 Label
的文本更改 TextInput
的文本 属性。您将失去以这种方式使用光标的可能性,并且您将需要处理换行文本。
示例:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<Test>:
TextInput:
text: 't'
font_size: 60
# left, right
padding_x:
[self.center[0] - self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached) / 2.0,
0] if self.text else [self.center[0], 0]
# top, bottom
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
''')
class Test(BoxLayout):
pass
class TestApp(App):
def build(self):
return Test()
TestApp().run()
self._get_text_width(...)
显然是一个TextInput
的方法。它与小部件的核心一起工作,所以它可能不稳定(我发布的第一个例子是错误的,因为我的错误)^^
现在,如果 padding_x
的值从 left
和 right
填充,您将只需要左侧(不同之处仅在于在右侧使用加法和减法地方),所以让我们这样做:
- 获取
TextInput
中最长的子串
- 通过花哨的方法得到它的宽度(因为它不一致!)
- 从
center[0]
坐标中减去
当我们已经将 X 轴居中后,让我们转到 Y 轴。padding_y
的值为 top
和 bottom
:
- 向下填充小部件的一半
height
- 获取单行高度的一半
- 将数字乘以
TextInput
中的行数
- 从
self.height / 2.0
中减去数字
- 最下面是
0
,我们不管
注意:max()
需要一些参数,如果没有 text
,max()
会提高声音。我们将使用仅中心 padding_x
的替代左填充来关闭它:
<padding_x with max> if self.text else [self.center[0], 0]
只需在您的 TextInput
中使用 halign: "center"
@pete halign:'center' 加上 y 轴上的填充也只能完成这项工作
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<Test>:
TextInput:
text: 't'
halign: 'center'
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
''')
class Test(BoxLayout):
pass
class TestApp(App):
def build(self):
return Test()
TestApp().run()
如何在 Kivy 的 TextInput 中将文本水平居中?
我有以下屏幕:
但我想像这样集中我的文字:
这是我的 kv 语言的一部分:
BoxLayout:
orientation: 'vertical'
Label:
markup: True
text: '[b] Type something... [/b]'
size_hint: 1, 0.6
size: self.parent.size[0], 200
font_size: self.size[0] * 0.1
text_size: self.size
halign: 'center'
valign: 'middle'
canvas.before:
Color:
rgb: 0, 0, 204
Rectangle:
pos: self.pos
size: self.size
TextInput:
focus: True
如何使 TextInput 的文本居中?
Afaik,没有像 Label
中那样对齐的东西,但是,您可以使用 padding
将位置推到任何您想要的位置。请记住,更改文本的大小会影响居中,因此您需要重新计算大小的更改(例如,在使用多个设备、大小等时)。
或者甚至可以有一个解决方法,您可以让 TextInput
不可见,使用 Label
获取触摸事件以触发 TextInput
(这将打开键盘)并更改 Label
的文本更改 TextInput
的文本 属性。您将失去以这种方式使用光标的可能性,并且您将需要处理换行文本。
示例:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<Test>:
TextInput:
text: 't'
font_size: 60
# left, right
padding_x:
[self.center[0] - self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached) / 2.0,
0] if self.text else [self.center[0], 0]
# top, bottom
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
''')
class Test(BoxLayout):
pass
class TestApp(App):
def build(self):
return Test()
TestApp().run()
self._get_text_width(...)
显然是一个TextInput
的方法。它与小部件的核心一起工作,所以它可能不稳定(我发布的第一个例子是错误的,因为我的错误)^^
现在,如果 padding_x
的值从 left
和 right
填充,您将只需要左侧(不同之处仅在于在右侧使用加法和减法地方),所以让我们这样做:
- 获取
TextInput
中最长的子串
- 通过花哨的方法得到它的宽度(因为它不一致!)
- 从
center[0]
坐标中减去
当我们已经将 X 轴居中后,让我们转到 Y 轴。padding_y
的值为 top
和 bottom
:
- 向下填充小部件的一半
height
- 获取单行高度的一半
- 将数字乘以
TextInput
中的行数
- 从
self.height / 2.0
中减去数字 - 最下面是
0
,我们不管
注意:max()
需要一些参数,如果没有 text
,max()
会提高声音。我们将使用仅中心 padding_x
的替代左填充来关闭它:
<padding_x with max> if self.text else [self.center[0], 0]
只需在您的 TextInput
中使用halign: "center"
@pete halign:'center' 加上 y 轴上的填充也只能完成这项工作
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<Test>:
TextInput:
text: 't'
halign: 'center'
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
''')
class Test(BoxLayout):
pass
class TestApp(App):
def build(self):
return Test()
TestApp().run()