Difference between revisions of "TextField:getPointFromTextPosition"

From GiderosMobile
(Created page with "__NOTOC__ <languages /> '''<translate>Available since</translate>:''' Gideros 2019.5<br/> '''<translate>Class</translate>:''' TextField<br/> =...")
 
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
'''Available since:''' Gideros 2019.5<br/>
'''<translate>Available since</translate>:''' Gideros 2019.5<br/>
+
'''Class:''' [[TextField]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/TextField|TextField]]<br/>
+
 
=== <translate>Description</translate> ===
+
=== Description ===
<translate>Returns the coordinates from a given offset within the text<br /></translate>
+
Returns the coordinates from a given offset within the text.
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
(number),(number) = TextField:getPointFromTextPosition(offset)
 
(number),(number) = TextField:getPointFromTextPosition(offset)
</source>
+
</syntaxhighlight>
=== <translate>Parameters</translate> ===
+
 
'''offset''': (string) <translate>The byte ofsset into the text string.</translate> <br/>
+
=== Parameters ===
=== <translate>Return values</translate> ===
+
'''offset''': (string) The byte offset into the text string. <br/>
'''<translate>Returns</translate>''' (number) <translate>The X coordinate.</translate><br/>
+
 
'''<translate>Returns</translate>''' (number) <translate>The Y coordinate.</translate><br/>
+
=== Return values ===
 +
'''Returns''' (number) the X coordinate<br/>
 +
'''Returns''' (number) the Y coordinate<br/>
 +
 
 +
=== Example ===
 +
'''Draws a red underline under the 'is' word:'''
 +
<syntaxhighlight lang="lua">
 +
local font = TTFont.new("fonts/Tahoma.ttf", 32, "", true, 1)
 +
local text = TextField.new(font, "This is a text")
 +
text:setPosition(100, 100)
 +
local isS, isE = string.find(text:getText(), " is ") --Get 'is' word position
 +
local isSX, isSY = text:getPointFromTextPosition(isS) --Get 'i' starting point
 +
local isEX, isEY = text:getPointFromTextPosition(isE - 1) -- Get 's' end point
 +
-- Draw a red underline under the 'is' word
 +
local underline = Pixel.new(0xFF0000, 1, isEX - isSX, 3)
 +
underline:setPosition(100 + isSX, 100 + 2)
 +
stage:addChild(text)
 +
stage:addChild(underline)
 +
</syntaxhighlight>
 +
 
 +
{{TextField}}

Latest revision as of 08:09, 18 October 2024

Available since: Gideros 2019.5
Class: TextField

Description

Returns the coordinates from a given offset within the text.

(number),(number) = TextField:getPointFromTextPosition(offset)

Parameters

offset: (string) The byte offset into the text string.

Return values

Returns (number) the X coordinate
Returns (number) the Y coordinate

Example

Draws a red underline under the 'is' word:

local font = TTFont.new("fonts/Tahoma.ttf", 32, "", true, 1)
local text = TextField.new(font, "This is a text")
text:setPosition(100, 100)
local isS, isE = string.find(text:getText(), " is ") --Get 'is' word position
local isSX, isSY = text:getPointFromTextPosition(isS) --Get 'i' starting point
local isEX, isEY = text:getPointFromTextPosition(isE - 1) -- Get 's' end point
-- Draw a red underline under the 'is' word
local underline = Pixel.new(0xFF0000, 1, isEX - isSX, 3)
underline:setPosition(100 + isSX, 100 + 2)
stage:addChild(text)
stage:addChild(underline)