Dan
Dan

Reputation: 11

How to insert a line break after "x" characters

I'm currenty working on a newscast graphics program that is using Visual Basic for scripting. My challenge is this:

I have a .xml file that contains several pieces of data within it, one of which needs to be added to the graphic (a news headline from our website). These headlines are too long for the graphic, however, and need a line break added to them. I have successfully gotten this headline into the script through the rest of the graphics software, and it's temp.txt name is BodyTxt(i).Text (where (i) is a part of a loop for another part of the script, but will always equal 1, 2, or 3). After 35 characters, I need a line break. What would be the simplest way of doing this?

For future reference, I could see this being used in order to create a similar script within a web page in order to automatically populate data fields from an RSS or .xml feed without breaking the template and either forcing a font to shrink to fit the entire field, or creating a line break in the middle of a word.

Upvotes: 1

Views: 821

Answers (1)

Sam Johnson
Sam Johnson

Reputation: 981

Is this what you're looking for?


Sub Main()
    Dim testMessage As String

    testMessage = "For future reference, I could see this being used in order to create a similar script within a web page in order to automatically populate data fields from an RSS or .xml feed without breaking the template and either forcing a font to shrink to fit the entire field, or creating a line break in the middle of a word."

    PrintMessage(testMessage, 30)

    Console.ReadLine()
End Sub

Sub PrintMessage(Message As String, Length As Integer)
    Dim currentLength = 0

    Dim words As Array

    words = Split(Message, " ")

    For Each word As String In words
        If currentLength + word.Length > Length Then
            Console.Write(ControlChars.Tab & currentLength)                                         
            Console.WriteLine()
            currentLength = 0
        End If

        Console.Write(word & " ")
        currentLength += word.Length
    Next
        Console.Write(ControlChars.Tab & currentLength)
End Sub 

Produces this output:

For future reference, I could see     28
this being used in order to create a  29
similar script within a web page in   29
order to automatically populate       28
data fields from an RSS or .xml feed  29
without breaking the template and     29
either forcing a font to shrink to    28
fit the entire field, or creating a   29
line break in the middle of a word.   29

Upvotes: 3

Related Questions