2015-07-19 158 views
1

我想基於窗口高度縮放按鈕或標籤內的文本,但受窗口寬度限制。以下作品:Kivy基於窗口高度和寬度縮放文本

font_size: self.height - dp(15) 

但是文本可以超過按鈕或標籤的寬度,所以我想的東西,如限制它:

font_size: self.height - dp(15) if self.texture_size[0] < self.width else (self.width*2)/(self.height+0.1) 

不幸的是這有問題,就是當> = texture_size [0]它會變小,但是當文本再次變小時,它是< self.width。所以最終會陷入循環,導致[CRITICAL] [Clock]錯誤。

爲了給出一個更清晰的圖像,在紅色條的文字應該是儘可能大,但不能超過按鍵寬度: Kivy_text-scaling

回答

2

可以使用大規模改造,縮小文本,如果它是太大,這將避免競賽:

<[email protected]>: 
    _scale: 1. if self.texture_size[0] < self.width else float(self.width)/self.texture_size[0] 
    canvas.before: 
     PushMatrix 
     Scale: 
      origin: self.center 
      x: self._scale or 1. 
      y: self._scale or 1. 
    canvas.after: 
     PopMatrix 

但是,這不會縮放畫布上的所有內容。因此,如果您想要繪製背景或其他東西,請確保它不在PushMatrix/PopMatrix之外。例如,如果你想用Button利用這一點,你可以重寫Button的KV規則:

<[email protected]>: 
    state_image: self.background_normal if self.state == 'normal' else self.background_down 
    disabled_image: self.background_disabled_normal if self.state == 'normal' else self.background_disabled_down 
    _scale: 1. if self.texture_size[0] < self.width else float(self.width)/self.texture_size[0] 
    canvas: 
     Color: 
      rgba: self.background_color 
     BorderImage: 
      border: self.border 
      pos: self.pos 
      size: self.size 
      source: self.disabled_image if self.disabled else self.state_image 
     PushMatrix 
     Scale: 
      origin: self.center 
      x: self._scale or 1. 
      y: self._scale or 1. 
     Color: 
      rgba: self.disabled_color if self.disabled else self.color 
     Rectangle: 
      texture: self.texture 
      size: self.texture_size 
      pos: int(self.center_x - self.texture_size[0]/2.), int(self.center_y - self.texture_size[1]/2.) 
     PopMatrix 

我創建了一個使用例子作爲依據:https://gist.github.com/kived/862db38078170ec0ef83

相關問題