Wrap text within a panel I am trying to write some text on a panel that I created, like this:

box.label("Some long text that is out of borders")


but because the text is too long, the panel only shows the text in one line and does not make the text stop at the edge of the panel and start a new line.

So it appears like this:


  Some long text...ers


How can I make it contained within the panel's width?
I know I'am answering my own question, because I knew how to do it only after posting the question, so for anyone who may need this answer here it goes.

I used a wrap function that I made my self, like this:

def wrap(width, text):

  lines = []

  arr = text.split()
  lengthSum = 0

  strSum = ""

  for var in arr:
    lengthSum+=len(var) + 1
    if lengthSum &lt;= width:
      strSum += " " + var
    else:
      lines.append(strSum)
      lengthSum = 0
      strSum = var

  lines.append(" " + arr[len(arr) - 1])

  return lines


The width argument is the width of the panel in terms of number of characters that fit in there.
Then in the draw function I got the width of the panel like this:

tool_shelf = None
area = bpy.context.area

for region in area.regions:
  if region.type == 'TOOLS':
    tool_shelf = region


Where tool_shelf is now the panel I created, then I call the wrap function with the width and text like this:

lines = wrap(math.ceil(tool_shelf.width / 9), "Some long text that is out of borders")


I divided by 9 because tool_shelf.width gives back the width in pixels, and I assumed 9 pixels for each character, now lines is an array where every element is a line.
Then I write the lines in the panel like this:

for var in lines:
  row = layout.row(align = True)
  row.alignment = 'EXPAND'
  row.label(var)


I know this is not the optimal way, but anybody can get the main idea from this code and optimize it to their needs.

Note: function wrap is not in any class, but all the other codes are inside the draw function of the class Panel.