I am trying to put textinput and label with same settings in one place so text would be at same place but textinput making strange indent while label doesnt.
I am calculating height of text with limited width using label's function _label.texture.size
and then applying this height and width to text input and label expecting two textes would be in same place for accurate label's text selection. But im getting this, firstly all is good but then text in textinput makes strange indent and all text then goes wrong.
Result:
Code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
class main(App):
def build(self):
f = FloatLayout()
text = "This is random text Every time you're online, you are bombarded by pictures, articles, links and videos trying to tell their story. Unfortunately, not all of these stories are true. Sometimes they want you to click on another story or advertisement at their own site, other times they want to upset people for political reasons. "
labeltocheckheight = Label(text=text, text_size=(200, None), markup=True,font_size="24", size=(400, 100), pos=(100, 100), size_hint=(1, 1), valign='top')
labeltocheckheight._label.refresh()
width, height = labeltocheckheight._label.texture.size
label = Label(text=text, text_size=(width, height), markup=True,font_size="24", size=(width, height), pos=(100, 100), size_hint=(None, None), valign='top')
textinput2 = TextInput(text=text, font_size="24", size=(width, height), cursor=(0,0),pos=(100, 100), size_hint=(None, None), readonly=True, foreground_color=(1, 1, 1, 0.5),background_color=(1, 0, 0, 0), padding=0)
f.add_widget(textinput2)
f.add_widget(label)
return f
main().run()
The Label
and the TextInput
use different algorithms for wrapping text, so it is not likely that they will agree. You can process the text from the labeltocheckheight
, extracting the individual lines, then setting them as the text for the TextInput
. Here is a modified version of your build()
method that uses this approach:
def build(self):
f = FloatLayout()
text = "This is random text Every time you're online, you are bombarded by pictures, articles, links and videos trying to tell their story. Unfortunately, not all of these stories are true. Sometimes they want you to click on another story or advertisement at their own site, other times they want to upset people for political reasons. "
labeltocheckheight = Label(text=text, text_size=(200, None), markup=True,font_size="24", size=(400, 100), pos=(100, 100), size_hint=(1, 1), valign='top')
labeltocheckheight._label.refresh()
width, height = labeltocheckheight._label.texture.size
text_for_ti = ''
num_line = len(labeltocheckheight._label._cached_lines)
for line_num in range(num_line):
line = labeltocheckheight._label._cached_lines[line_num]
if line_num == num_line - 1:
text_for_ti += line.words[0].text
else:
text_for_ti += line.words[0].text + '\n'
label = Label(text=text, text_size=(width, height), markup=True,font_size="24", size=(width, height), pos=(100, 100), size_hint=(None, None), valign='top')
textinput2 = TextInput(text=text_for_ti, font_size="24", size=(width, height), cursor=(0,0),pos=(100, 100), size_hint=(None, None), readonly=True, foreground_color=(1, 1, 1, 0.5),background_color=(1, 0, 0, 0), padding=0)
f.add_widget(textinput2)
f.add_widget(label)
return f
The only difference is the calculation of text_for_ti
, and the use of that for the text
property of the TextInput
. You might want to add do_wrap=False
to the TextInput
.