[英]how to add a rect around a word in corona sdk?
知道 Corona SDK 中一行内单词的框架的正确方法是什么? 换句话说,我想在特定单词上方添加一个矩形,我尝试使用 webview 代替并mark
标签,但 webview 在 iOS 上一直闪烁,因此 webview 解决方案被取消。
我已经在这个词中手动添加了一个矩形,但是我指定一个词并知道它的框架的最佳方法是什么,所以我用矩形突出显示它,比如将矩形移动到下一个词? 我使用的字体是 Arial,它不是等宽的。
local myRect = display.newRect(20,165,32,12.5)
myRect.alpha = 0.5
myRect:setFillColor(1,1,0)
myRect.anchorX = 0
local myString = "Word is highlighted"
local line = display.newText(myString, 0,165, "Arial", 12.5)
line.anchorX = 0
line.x = 20
多谢。
如果我是你,我会为此编写附加功能。 就像是:
function highlightedText(pre, high, post, x, y, font, size)
local text = display.newText("", x, y, font, size)
local dx, rectangle = 0
if pre then
local t = display.newText(pre, 0, 0, font, size)
text.text = pre
dx = t.width
-- We need to add line below according to Corona Docs
text.anchorX = 0 text.x = x text.y = y
t:removeSelf()
end
if high then
local t = display.newText(high, 0, 0, font, size)
rectangle = display.newRect(x+dx, y, t.width, t.height)
rectangle:toBack()
text.text = text.text .. high
-- We need to add line below according to Corona Docs
text.anchorX = 0 text.x = x text.y = y
t:removeSelf()
end
if post then
text.text = text.text .. post
-- We need to add line below according to Corona Docs
text.anchorX = 0 text.x = x text.y = y
end
return text, rectangle, dx -- `dx` passed in case of moving whole text along with rectangle
end
local text, rect, _ = highlightedText("The", "word", "is highlighted.", 10, 10, "Arial", 12.5)
rect.alpha = 0.5
rect:setFillColor(1,1,0)
rect.strokeWidth = 3
text:setFillColor(1,1,1)
我不是 Corona 方面的专家,但这应该适用于单行静态文本。
试试这个。
local player2 = display.newText("test221234567890", 210, 210 )
player2:setFillColor( 0.6, 0.4, 0.8 )
local myRectangle = display.newRect( player2.x, player2.y, player2.width, player2.height )
myRectangle.strokeWidth = 3
myRectangle:setFillColor( 0.5 )
myRectangle:toBack()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.