如何在 corona sdk 中的单词周围添加一个矩形?
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
非常感谢。
试试这个。
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()
如果我是你,我会为此编写额外的功能。类似于:
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 方面的专家,但这应该适用于 单行静态 文本。
在 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
非常感谢。
试试这个。
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()
如果我是你,我会为此编写额外的功能。类似于:
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 方面的专家,但这应该适用于 单行静态 文本。