在 Brightscript 中检查按钮标题溢出

Checking for button title overflows in Brightscript

在 Roku 的库中,要将按钮添加到 spring 看板屏幕,您可以使用以下方法(参见 doc):

AddButton(buttonID as Integer, title as String) as Boolean

如果title超出按钮宽度,它会自动截断文本并附加“...”。但是是否可以通过编程方式检查是否存在溢出?

没有很好的方法来检查它,但作为解决方法,您有两个选择:检查字符串长度以获得最大安全长度或使用 GetOneLineWidth() 方法。

对于第二个选项,您必须知道按钮的宽度、字体系列和字体大小,例如:

button_width = 450    'px

reg = CreateObject("roFontRegistry")    
arialFont = reg.GetFont("Arial", reg.GetDefaultFontSize(), false, false)
title_width = arialFont.GetOneLineWidth(titleString, 1280)    'maxWidth set to screen width

if title_width > button_width then
    'do your logic here
end if