Forum Archive

Unable to set label size in custom view

daveM

I'm using boilerplate code from the docs (http://omz-software.com/pythonista/docs/ios/ui.html#building-custom-views) and added just a label to display touch location. No matter what I define as the initial points of three label, it is always a fixed size on the screen.

No matter what settings I create the label, it is always defaulted to (0,0) and a small width. I do want it to be full width, the values shown here are for testing.

```
import ui

class MyView (ui.View):
def init(self):

    self.info=ui.Label(
        self.height/2,
        self.width/2,
        self.width/2, 
        self.height/4)
    self.info.corner_radius=9
    self.info.alignment=ui.ALIGN_CENTER
    self.info.background_color='#6bff6b'
    self.add_subview(self.info)
    self.background_color='#fff9d6'

def draw(self):
    w=self.width
    h=self.height
    l=w/4
    t=h/4
    w=w/2
    h=h/2
    path = ui.Path.rect(l,t,w,h)
    ui.set_color('blue')
    path.fill()
    img = ui.Image.named('iob:alert_256')
    img.draw(l,t,w,h) #self.width, self.height)

def touch_moved(self, touch):
    x,y=touch.location
    self.info.text = '{:04}-{:0=4}'.format(int(x),int(y))

v = MyView()
v.present('sheet')```

cvp

@daveM You forgot the keyword frame=(...)

        self.info=ui.Label(frame=(
            self.height/2,
            self.width/2,
            self.width/2, 
            self.height/4))
daveM

Thanks for that. It turns out there's a little more to my problem than that because as soon as I did it, my left and top position were now different (ie, not zero), but the width and height are wrong.

It turns out the superview's self attribute(s) aren't fully available or defined or set of other. Not sure what it is, but after putting in fixed values I got something closer to what I wanted. I'll work on refining it later. Thanks!!!