So what I am trying to do is create a label template that has a 40 character “limit.” Not really a limit per say but where I can put 40 characters within the text field and have it formatted at 10 character intervals correctly.
At first I tried to do it in expression language which was not working very well at all. This is the code.
switch(
20 <= len({textField.Text})>10,
30 <= len({textField.Text})>20,
40 <= len({textField.Text})>30,
event.source.text = "<html>"+ left({textField.Text},10)+"
<br>"+right(left({textField.Text},len({textField.Text})), 20-
len({textField.Text})),
event.source.text = "<html>"+ left({textField.Text},10)+"<br>"+right(left({textField.Text},20), 10)+"
<br>"+right(left({textField.Text},len({textField.Text})), 30-len({textField.Text})),
event.source.text = "<html>"+ left({textField.Text},10)+"<br>"+right(left({textField.Text},20), 10)+"
<br>"+right(left({textField.Text},30), 10)+"<br>"+right(left({textField.Text},len({textField.Text})), 40-
len({textField.Text})),
event.source.text = "<html>"+ left({textField.Text},10))
Note that the each of the return lines were in single lines in the code. Also the errors that I got from this were either that I can’t use 7 arguments with a switch function or that the left cannot use 3 arguments. The code was slightly different each time as I had tried to modify it in between to solve my problem so I can’t be sure this is the exact code that tripped said error. I do know when there was the 7 arguments error I hadn’t defined event.source.text to equal the formatted text field though I can’t be sure that the 7 arg. error didn’t throw afterwards as it was a constant flurry of errors and I can’t check the errors without copying my code, removing the binding, and exiting the expression popup.
After I banged my head on that for a bit I tried scripting. Both property change scripting and through a runScript() function in the expression binding. The property change scripting didn’t do a whole lot as the binding to whatever property change was being used didn’t trigger when I put text into the template parameter.
The closest I seemed to get was through the runScript() in the expression binding but I have the problems where my designer froze and I am lost as to what I am doing wrong with my code.
Script code:
def labelWrap(input, output):
# On line 1 I defined the 2 variables. Input is where the string is coming from and
# output is where the string is being written to in formatted form
text = input
strLength = len(text)
# In lines 4-6 I defined the variables to be used and took the len, or string length, of the input
# string.
if 20 < strLength > 10:
output = "<html>"+text[:10]+"<br>"+text[11:20]
elif 30 < strLength > 20:
output = "<html>"+text[:10]+"<br>"+text[11:20]+"<br>"+text[21:30]
elif strLength > 30:
output = "<html>"+text[:10]+"<br>"+text[11:20]+"<br>"+text[21:30]+"<br>"+text[31:]
else:
output = "<html>"+text[:10]
# The rest of this code just wraps the text in html format depending on the length of the string.
# Any string above 40 characters long will make the breaks act funky so don't do that.
template text field code(bound through expression property:
runScript("labelTextWrap.labelWrap", 0, {textField.Text}, {textField.Label.text})
If there is anything that I need to elaborate on or if there is simply a better way to do this lmk.