catui icon indicating copy to clipboard operation
catui copied to clipboard

根据当前输出改变滚动条位置应该怎么写?

Open sharebophar opened this issue 6 years ago • 0 comments

我给UILabel加了一个自动高度的函数

-- 宽度不变,自动高度
function UILabel:setAutoHeight()
  local width,height = self:getSize()
  local autoHeight = self.drawable:getHeight()
  self:setSize(width, autoHeight)
  return autoHeight
end

然后初始化控件

function InitContenter()
  love.graphics.setBackgroundColor(35/255, 42/255, 50/255, 1)
  local mgr = UIManager:getInstance()
  local content = UIContent:new()
  content:setPos(10, 10)
  local win_width = love.graphics.getWidth()
  local win_height= love.graphics.getHeight()
  
  content:setSize(win_width, win_height)
  content:setContentSize(win_width, win_height)
  content.backgroundColor = {31/255, 36/255, 43/255, 0.5}
  -- content:removeChild(content.hBar) -- 移除控件的子对象使用这个
  --UIControl.removeChild(content, content.vBar) 
  UIControl.removeChild(content, content.hBar) -- 移除控件的原生对象用这个
  --content.isCross = true
  content.visible = false
  mgr.rootCtrl.coreContainer:addChild(content)
  
  local label = UILabel:new("catui/font/visat.ttf", "show text", 14)
  label:setAnchor(0, 0.5)
  label:setAutoSize(false)
  label:setSize(content:getWidth(),content:getHeight())
  label:setAnchor(0,0)
  label:setClip(false)
  label:setFontColor({1, 1, 0, 1})
  content:addChild(label)
  content:setClip(false)
  content:setPos(0,0)
  content.visible = true
  return mgr
end

之后显示文本

function ShowDebugText(show_text,...)
  local text = ""
  for i,v in ipairs({...}) do
      text = text .. " " .. tostring(v)
  end
  local root = UIManager:getInstance():getRootCtrl()
  local content = (root.coreContainer:getChildren())[1]
  local label = content.contentCtrl.children[1]
  show_text = show_text .."\n".. text
  label:setText(show_text)
  --print(show_text)
  local labelHeight = label:setAutoHeight()
  local contentHeight = content:getContentHeight()
  local barHeight = math.floor(contentHeight*math.min(1.0,contentHeight*1.0/labelHeight))
  -- 要根据当前显示的行数,改变滚动条的位置
  content.vBar.bar:setHeight(barHeight)
  content.contentCtrl:setY(math.min(0,contentHeight-labelHeight))
  print("update barHeight:"..tostring(barHeight))
  return show_text
end

sharebophar avatar Dec 18 '18 14:12 sharebophar