.kv文件中如何根据屏幕动态调整文字大小
How to dynamically adjust the size of text according to the screen in .kv file
所以我需要在屏幕上打印一个列表。此列表的大小可以更改,并且有 15 个元素的限制,即 15 行 (\n)。每一条新线都有一个新名称。此列表将打印在必须覆盖整个列表的大按钮上方。我可以设法在按钮中整齐地对齐文本,但是一旦屏幕尺寸发生变化,文本就会关闭,因为按钮会根据屏幕尺寸动态变化。
下面您可以看到我针对该特定 class 的代码,虽然我认为这没有任何用处,但我认为无论如何我都会包含它
class RecordData(Screen):
def printNames(self):
with open("Lipo names.txt") as f:
lineList = f.readlines()
lipoNames = ("".join(map(str, lineList)))
self.names.text = lipoNames
self.remove_widget(self.remove)
如您所见,我一直在玩弄 font_size,现在它会动态变化,但即使屏幕仅在 y 轴上拉伸,它也会同时调整文本的 x 和 y 大小.我希望文本的 y 大小在屏幕的 y 改变时改变,而不是 x 和 y 都改变。
<RecordData>
name: "record"
names: names
remove: remove
Button:
background_color: 255,255,255,1
pos_hint: {"x": 0.05, "y": 0.05}
size_hint: 0.2, 0.9
Button:
id: remove
pos_hint: {"x": 0.09, "y": 0.5}
size_hint: 0.2, 0.1
text: "View lipos"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.printNames()
Label:
id: names
text: ""
color: 0,0,0,1
font_size: (root.width**2 + root.height**1.9) / 13**4
pos_hint:{"x": 0.0, "y": 0.32}
size_hint:0.3, 0.15
Here is a image of what currently prints with the default tab open
Here is a picture when I put the tab into full screen, as you can see the words have become massive because of the multiplier that I have added. But If I don't add this the words remain small
我希望这是有道理的。这是我的第一个 kivy 应用程序,所以我对这种 kv 语言还是很陌生,所以请您保持简单的解释。预先感谢您的所有帮助!
您至少可以通过两种不同的方式计算字体大小。最简单的就是像您所做的那样使用公式。我发现以下似乎有效:
Button:
id: butt # added id
background_color: 255,255,255,1
pos_hint: {"x": 0.05, "y": 0.05}
size_hint: 0.2, 0.9
Label:
id: names
text: ""
color: 0,0,0,1
pos_hint: butt.pos_hint # same position as the Button
size_hint: 0.3, butt.size_hint_y # same height as the Button
# set text area to same size as the Label
text_size: self.size
# position the text in the text area
valign: 'center'
halign: 'left'
font_size: (self.height / 15) * 0.75
另一种方法是使用kivy.core.text.Label
计算字体大小,通过设置触发器在文本或标签大小更改时计算字体大小:
<RecordData>
name: "record"
names: names
remove: remove
Button:
id: butt # added id
background_color: 255,255,255,1
pos_hint: {"x": 0.05, "y": 0.05}
size_hint: 0.2, 0.9
Button:
id: remove
pos_hint: {"x": 0.09, "y": 0.5}
size_hint: 0.2, 0.1
text: "View lipos"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.printNames()
Label:
id: names
text: ""
color: 0,0,0,1
pos_hint: butt.pos_hint # same position as the Button
size_hint: 0.3, butt.size_hint_y # same height as the Button
# set text area to same size as the Label
text_size: self.size
# position the text in the text area
valign: 'center'
halign: 'left'
# recalculate font size when text or size changes
on_size: root.adjust_font_size()
on_text: root.adjust_font_size()
然后,adjust_font_size()
方法添加到RecordData
class:
def adjust_font_size(self):
names = self.ids.names
if names.text == '':
return
font_size = 15
tmp_label = CoreLabel(text=names.text, font_size=font_size, font_name=names.font_name)
tmp_label.refresh()
# maximize the font size
while( tmp_label.height <= names.height):
font_size += 1
tmp_label = CoreLabel(text=names.text, font_size=font_size, font_name=names.font_name)
tmp_label.refresh()
# at this point font size will be a bit too big
# reduce font size just until text fits in the names Label
while( tmp_label.height > names.height):
font_size -= 1
tmp_label = CoreLabel(text=names.text, font_size=font_size, font_name=names.font_name)
tmp_label.refresh()
# set the calculated font size in the names Label
names.font_size = font_size
此代码仅考虑 Label
的高度,但可以扩展为也考虑宽度。
所以我需要在屏幕上打印一个列表。此列表的大小可以更改,并且有 15 个元素的限制,即 15 行 (\n)。每一条新线都有一个新名称。此列表将打印在必须覆盖整个列表的大按钮上方。我可以设法在按钮中整齐地对齐文本,但是一旦屏幕尺寸发生变化,文本就会关闭,因为按钮会根据屏幕尺寸动态变化。
下面您可以看到我针对该特定 class 的代码,虽然我认为这没有任何用处,但我认为无论如何我都会包含它
class RecordData(Screen):
def printNames(self):
with open("Lipo names.txt") as f:
lineList = f.readlines()
lipoNames = ("".join(map(str, lineList)))
self.names.text = lipoNames
self.remove_widget(self.remove)
如您所见,我一直在玩弄 font_size,现在它会动态变化,但即使屏幕仅在 y 轴上拉伸,它也会同时调整文本的 x 和 y 大小.我希望文本的 y 大小在屏幕的 y 改变时改变,而不是 x 和 y 都改变。
<RecordData>
name: "record"
names: names
remove: remove
Button:
background_color: 255,255,255,1
pos_hint: {"x": 0.05, "y": 0.05}
size_hint: 0.2, 0.9
Button:
id: remove
pos_hint: {"x": 0.09, "y": 0.5}
size_hint: 0.2, 0.1
text: "View lipos"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.printNames()
Label:
id: names
text: ""
color: 0,0,0,1
font_size: (root.width**2 + root.height**1.9) / 13**4
pos_hint:{"x": 0.0, "y": 0.32}
size_hint:0.3, 0.15
Here is a image of what currently prints with the default tab open
Here is a picture when I put the tab into full screen, as you can see the words have become massive because of the multiplier that I have added. But If I don't add this the words remain small
我希望这是有道理的。这是我的第一个 kivy 应用程序,所以我对这种 kv 语言还是很陌生,所以请您保持简单的解释。预先感谢您的所有帮助!
您至少可以通过两种不同的方式计算字体大小。最简单的就是像您所做的那样使用公式。我发现以下似乎有效:
Button:
id: butt # added id
background_color: 255,255,255,1
pos_hint: {"x": 0.05, "y": 0.05}
size_hint: 0.2, 0.9
Label:
id: names
text: ""
color: 0,0,0,1
pos_hint: butt.pos_hint # same position as the Button
size_hint: 0.3, butt.size_hint_y # same height as the Button
# set text area to same size as the Label
text_size: self.size
# position the text in the text area
valign: 'center'
halign: 'left'
font_size: (self.height / 15) * 0.75
另一种方法是使用kivy.core.text.Label
计算字体大小,通过设置触发器在文本或标签大小更改时计算字体大小:
<RecordData>
name: "record"
names: names
remove: remove
Button:
id: butt # added id
background_color: 255,255,255,1
pos_hint: {"x": 0.05, "y": 0.05}
size_hint: 0.2, 0.9
Button:
id: remove
pos_hint: {"x": 0.09, "y": 0.5}
size_hint: 0.2, 0.1
text: "View lipos"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.printNames()
Label:
id: names
text: ""
color: 0,0,0,1
pos_hint: butt.pos_hint # same position as the Button
size_hint: 0.3, butt.size_hint_y # same height as the Button
# set text area to same size as the Label
text_size: self.size
# position the text in the text area
valign: 'center'
halign: 'left'
# recalculate font size when text or size changes
on_size: root.adjust_font_size()
on_text: root.adjust_font_size()
然后,adjust_font_size()
方法添加到RecordData
class:
def adjust_font_size(self):
names = self.ids.names
if names.text == '':
return
font_size = 15
tmp_label = CoreLabel(text=names.text, font_size=font_size, font_name=names.font_name)
tmp_label.refresh()
# maximize the font size
while( tmp_label.height <= names.height):
font_size += 1
tmp_label = CoreLabel(text=names.text, font_size=font_size, font_name=names.font_name)
tmp_label.refresh()
# at this point font size will be a bit too big
# reduce font size just until text fits in the names Label
while( tmp_label.height > names.height):
font_size -= 1
tmp_label = CoreLabel(text=names.text, font_size=font_size, font_name=names.font_name)
tmp_label.refresh()
# set the calculated font size in the names Label
names.font_size = font_size
此代码仅考虑 Label
的高度,但可以扩展为也考虑宽度。