A
A
Alex2015-10-21 16:08:44
PHP
Alex, 2015-10-21 16:08:44

How to align text when overlaying an image?

Let's say I have a string: aaa bbb ccccccapproximately 200px wide. And I want to overlay this line on the image. But there is a moment! The text width needs to be 100px. Those. so that the text is overlaid like this:

aaa bbb
cccccc
Those. so that what does not fit into the given width is wrapped on a new line.
Is there any way to implement something like this?
In theory, you need me to specify the start coordinate, the text alignment relative to the coordinate, and the maximum width. And the text that would go beyond the specified width was transferred to a new line.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivanq, 2015-10-21
@Ivanq

I will describe the algorithm.

length = 0
str = "aaa bbb cccccc"
resstr = ""
curstr = ""
for i=0; i<strlen(str); i++
    curstr .= str[i];
    if вычислить_ширину(curstr) > 100
        resstr .= curstr . "\n"
        curstr = ""
    end if
end for
resstr .= curstr

Something like this. PHP I know, I forgot only the name of the function that calculates the size of the string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question