def draw(self):
# redraw button
def darken(color):
return tuple([0.5*x for x in color])
#set up button size to fit.
padding=10
textsize=ui.measure_string(string=self.title,max_width=0,font=self.font,alignment=ui.ALIGN_CENTER)
#draw border
ui.set_color(self.border_color)
path = ui.Path.rounded_rect(0, 0, self.width, self.height,self.corner_radius)
path.line_width=self.border_width
path.stroke()
#fill button, depending on touch state
if self.touched:
if self.doing_longtouch:
ui.set_color('blue')
else:
ui.set_color('grey')
else :
ui.set_color(self.bg_color)
path.fill()
# fill corner darker, if has children
if self.flow.subviews:
corner = ui.Path()
corner.move_to(self.width-1.5*self.corner_radius,0)
corner.line_to(self.width,0)
corner.line_to(self.width,1.5*self.corner_radius)
corner.line_to(self.width-1.5*self.corner_radius,0)
ui.set_color(darken(darken(self.bg_color)))
corner.stroke()
corner.fill()
# draw title, center vertically, horiz
rect=list(self.bounds)
rect[1]=(rect[3]-textsize[1])/2 #vert center
rect[2]=max(rect[2],textsize[0]+10)
ui.draw_string(self.title, rect=tuple(rect), font=self.font, color=self.tint_color, alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_WORD_WRAP)
if textsize[0]>self.bounds[2]:
self.width=textsize[0]+10
评论列表
文章目录