我目前正在使用Visual Basic进行脚本编写的新闻广播图形程序。我的挑战是:
我有一个.xml文件,其中包含几个数据,其中一个需要添加到图形中(来自我们网站的新闻标题)。但是,这些标题对于图形来说太长了,并且需要添加换行符。我通过其余的图形软件成功地将这个标题加入到脚本中,并且它的temp.txt名称是BodyTxt(i).Text(其中(i)是脚本另一部分的循环的一部分,但是总是等于1,2或3)。在35个字符后,我需要一个换行符。这样做最简单的方法是什么?
为了将来参考,我可以看到这是为了在网页中创建一个类似的脚本,以便自动从RSS或.xml提要填充数据字段而不破坏模板并强制字体缩小到适合整个字段,或在单词的中间创建换行符。
答案 0 :(得分:3)
这是你要找的吗?
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
生成此输出:
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