Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Adding additional labels during runtime
-
Hello,
I am having an issue in my code that has stumped me for a while.I want to be able to add labels to my view of varying length during runtime. The issue is that if a label I add exceeds a certain length, the label will not fit all of the contents that I want it to. This is only an issue if I am adding the labels during runtime as labels that are added before the view is presented will always display all of its contents.
It would be great if someone has an idea of why the label is size restricted while being added during runtime.
I was advised to use .size_to_fit() which does fix the sizing issue but for some reason, it changes the way the label fits in the coordinate system.
It would be equally great if someone could explain to me what is going on here as well.
Here is a demonstration of the problem I am experiencing:
import ui def addShortButton(sender): global numButtons label = ui.Label(text = "short title", center = (100, 240 + (20*numButtons)), flex = "w") numButtons+=1 view.add_subview(label) def addLongButton(sender): global numButtons label = ui.Label(text = "this is a long label title", center = (100, 240 + (20*numButtons)), flex = "w") #when this is uncommented, the label is not on the correct coordinate location #label.size_to_fit() numButtons+=1 view.add_subview(label) numButtons = 0 view = ui.View() view.background_color = "white" label = ui.Label(text = "short title", center = (100, 200), flex = "w") view.add_subview(label) label = ui.Label(text = "this is a long label title", center = (100, 220), flex = "w") view.add_subview(label) button = ui.Button(title = "click me to add a short button title", center = (50, 80), action = addShortButton) view.add_subview(button) button = ui.Button(title = "click me to add a long button title", center = (50, 110), action = addLongButton) view.add_subview(button) view.present()
-
@eli_sherwood not a solution but I already don't understand why your labels are not centered on x=100
-
@eli_sherwood try
def addShortButton(sender): global numButtons label = ui.Label(text = "short title")#, center = (100, 240 + (20*numButtons)), flex = "w") w,h = ui.measure_string(label.text, font=label.font) label.frame = (100-w/2,240 + (20*numButtons)-h/2,w,h) label.border_width = 1 label.border_color = 'lightgray' label.size_to_fit() numButtons+=1 view.add_subview(label) def addLongButton(sender): global numButtons label = ui.Label(text = "this is a long label title")#, center = (100, 240 + (20*numButtons)), flex = "w") #when this is uncommented, the label is not on the correct coordinate location #label.size_to_fit() #print(label.width) w,h = ui.measure_string(label.text, font=label.font) label.frame = (100-w/2,240 + (20*numButtons)-h/2,w,h) label.border_width = 1 label.border_color = 'lightgray' label.size_to_fit() numButtons+=1 view.add_subview(label) #print(label.width)
-
One reason why the original code didn't work is that ui doesn't let you set the anchor point -- if you want the center to be the anchor, you need to set the center again after calling size_to_fit.