Letter Spacing: Is it possible?

I’m fairly certain the answer is no, but wanted to verify. 

Is there any Corona/Lua equivalent to CSS’s letter spacing property? 

Hi @ericmarston,

At this time, the text APIs don’t have this capability. If it’s not a vast amount of letters, however, you could just render each letter as a separate text object, spaced apart from the one before it. I worked up a function for that if you’d like me to paste it (but again, it’s not intended for long sentences or multi-line or anything like that, and it was fairly customized for one app I worked on, which may not be what you need).

Brent

That would be great, thanks. 

Hi @ericmarston,

Here’s the function I wrote for spacing out words (or letters):

[lua]

local function modText( dispGroup, sentence, size, startX, baseline, font, flow, tblRGB )

    local num = tonumber ; local st_len = string.len ; local st_gmatch = string.gmatch ; local st_match = string.match

    local nextX = startX ; local dm = 1

    for word, space in st_gmatch( sentence, “([%w%d%p]+)(%s?%|?%d?%d?%|?%s?)” ) do

        local thisWord = display.newText( dispGroup, word, 0, 0, font, size )

        local pxs = 0

        if ( flow == “left” ) then

            thisWord.anchorX = 1 ; thisWord.anchorY = 1 ; dm = -1

        else

            thisWord.anchorX = 0 ; thisWord.anchorY = 1

        end

        thisWord.y = baseline ; thisWord.x = nextX

        if ( tblRGB ) then thisWord:setFillColor( tblRGB[1],tblRGB[2],tblRGB[3] ) end

        if ( st_len(space) > 0 ) then

            local sp = st_match( space, “%s%|(%d+)%|%s” )
            if ( sp ) then pxs = num(sp) end

        end

        nextX = thisWord.x + thisWord.width*dm + pxs*dm ; pxs = nil

    end

end

[/lua]

Then, to use it, call the function like this, for example:

[lua]

modText( titleGroup, “Hello |12| World |12| !!!”, 28, 20, 100, “Arial”, “right”, {1,0,0.2} )

modText( titleGroup, “H |6| e |6| l |6| l |6| o”, 28, 20, 200, “Arial”, “right”, {1,0.2,0} )

[/lua]

As you see, this allows you to put a custom pixel distance between each word (or letter) as defined by the numbers within " || " sets. If you are only concerned with having the same pixel space between each word or letter, you could simplify this function.

Changing the second-to-last parameter from “right” to “left” will make the text flow from right to left… for example, the first word in the “sentence” parameter will have its right edge at the “startX” position. Then, the next word in “sentence” will have its right edge |x| pixels away from the left edge of the first word… and so on.

Like I said, you may use this function for what it’s worth to your project(s), if anything.  :slight_smile:

Best regards,

Brent

1 Like

Hi @ericmarston,

At this time, the text APIs don’t have this capability. If it’s not a vast amount of letters, however, you could just render each letter as a separate text object, spaced apart from the one before it. I worked up a function for that if you’d like me to paste it (but again, it’s not intended for long sentences or multi-line or anything like that, and it was fairly customized for one app I worked on, which may not be what you need).

Brent

That would be great, thanks. 

Hi @ericmarston,

Here’s the function I wrote for spacing out words (or letters):

[lua]

local function modText( dispGroup, sentence, size, startX, baseline, font, flow, tblRGB )

    local num = tonumber ; local st_len = string.len ; local st_gmatch = string.gmatch ; local st_match = string.match

    local nextX = startX ; local dm = 1

    for word, space in st_gmatch( sentence, “([%w%d%p]+)(%s?%|?%d?%d?%|?%s?)” ) do

        local thisWord = display.newText( dispGroup, word, 0, 0, font, size )

        local pxs = 0

        if ( flow == “left” ) then

            thisWord.anchorX = 1 ; thisWord.anchorY = 1 ; dm = -1

        else

            thisWord.anchorX = 0 ; thisWord.anchorY = 1

        end

        thisWord.y = baseline ; thisWord.x = nextX

        if ( tblRGB ) then thisWord:setFillColor( tblRGB[1],tblRGB[2],tblRGB[3] ) end

        if ( st_len(space) > 0 ) then

            local sp = st_match( space, “%s%|(%d+)%|%s” )
            if ( sp ) then pxs = num(sp) end

        end

        nextX = thisWord.x + thisWord.width*dm + pxs*dm ; pxs = nil

    end

end

[/lua]

Then, to use it, call the function like this, for example:

[lua]

modText( titleGroup, “Hello |12| World |12| !!!”, 28, 20, 100, “Arial”, “right”, {1,0,0.2} )

modText( titleGroup, “H |6| e |6| l |6| l |6| o”, 28, 20, 200, “Arial”, “right”, {1,0.2,0} )

[/lua]

As you see, this allows you to put a custom pixel distance between each word (or letter) as defined by the numbers within " || " sets. If you are only concerned with having the same pixel space between each word or letter, you could simplify this function.

Changing the second-to-last parameter from “right” to “left” will make the text flow from right to left… for example, the first word in the “sentence” parameter will have its right edge at the “startX” position. Then, the next word in “sentence” will have its right edge |x| pixels away from the left edge of the first word… and so on.

Like I said, you may use this function for what it’s worth to your project(s), if anything.  :slight_smile:

Best regards,

Brent

Thanks a lot!

Recentlry I wrote a sub to put text lines justified all along the width of the defined borders. Each word (not letter, of course), was a separate text object… A page with 100 paragpraphs (i.e. 100 text objects) would be opened for ~1 sec on my 4 year old phone… which is reasonable. I do not have the exact numbers now with me, but let’s think that each paragraph has from 15 to 30 words… So, the number of text objects got multiplied so much! It took 10 secs for a page to get opened.
So, I suggest using letter (and word) spacing only for short texts.