Brian Dunning's FileMaker Custom Functions

WordWrap ( Text ; Size )

Wraps text to specified max line character count, without breaking words

  Average rating: 3.9 (45 votes) Log in to vote

Jonathan Stark   Jonathan Stark
Jonathan Stark Consulting
http://jonathanstark.com/

Share on Facebook Share on Twitter

  Sample input:
This is a really long text string that I need to display narrowly in a huge text field. Or some other weird reason like that.
  Sample output:
This is a
really long
text string
that I need
to display
narrowly in a
huge text
field. Or
some other
weird reason
like that.

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

Wraps text to specified max line character count, without breaking words. I used this function for compiling invoices as text for output as email, like so:


Qty Description Price
=======================================
3 Here is the long $34.00
item description
that I needed to
wrap.
---------------------------------------
2 Next item $37.00
---------------------------------------
2 Next item $37.00
=======================================



UPDATE:

Thank you to all the folks who emailed me about the "question mark" result (undefined result).

Yes, I am aware of the question mark. But, I feel that is appropriate for the function to return "undefined" because you are asking to do something impossible. IOW - the function can't wrap a line to 10 characters without breaking a word, if there is a word on that line that is 12 characters long.

HTH,
j

 

Comments

Anthony   Anthony, Philadelphia
Jan 29, 2011
I would like to truncate with an ellipsis rather than wrap text, when the text length exceeds the portal size. Can you help?
 
Tanya   Tanya, Sydney
Apr 13, 2011
Thanks - great function. Here is my slightly modified version, if you don't want to remove any existing carriage returns in your text, but just truncate the lines that are longer than your specified "size". I use this for things like the plain text version of emails which I want to add a hard word wrap at 60 characters, but retain all existing headings and carriage returns.

Let ([
TruncatedText = Left ( Text ; Size + 1 ) ;
CountOfSpaces = PatternCount ( TruncatedText ; " " ) ;
CountOfReturns = PatternCount ( TruncatedText ; "¶" ) ;
PositionOfLastSpace = If ( CountOfReturns > 0 ; Position ( TruncatedText ; "¶" ; 1 ; 1 ) ; Position ( TruncatedText ; " " ; 1 ; CountOfSpaces ) );
AddReturn = If ( CountOfReturns > 0 ; "" ; " ¶" );
ThisLine = Trim ( Left ( TruncatedText ; PositionOfLastSpace ) ) ;
UnprocessedText = Right ( Text ; Length ( Text ) - PositionOfLastSpace ) ;
Result = If ( Length ( Text ) ≤ Size ; Text; ThisLine & AddReturn & WordWrap ( UnprocessedText ; Size ) )
];
Result
)
 
Tanya Sager   Tanya Sager, Sydney, Australia
Jun 28, 2011
Here is another revised version of this WordWrap function which I find good for word wrapping email text. This version will not throw an error if there happens to be a string in your text longer than the "size" specified as input to the function, eg- if there is a long string in your text such as a URL, it will retain it rather than wrapping it.

Let ([
RequestedSizeText = Left ( Text ; Size + 1 ) ;
FullLineText = Left ( Text ; Position ( Text ; "¶" ; 1 ; 1 ) ) ;

//if one line appears to be a long string (such as a url) allow the full line, otherwise set to requested length
TruncatedText = If ( PatternCount ( RequestedSizeText ; " " ) = 0 ; FullLineText ; RequestedSizeText ) ;

CountOfSpaces = PatternCount ( TruncatedText ; " " ) ;
CountOfReturns = PatternCount ( TruncatedText ; "¶" ) ;

PositionOfLastSpace = If ( CountOfReturns > 0 ; Position ( TruncatedText ; "¶" ; 1 ; 1 ) ; Position ( TruncatedText ; " " ; 1 ; CountOfSpaces ) );
AddReturn = If ( CountOfReturns > 0 ; "" ; " ¶" );
ThisLine = Trim ( Left ( TruncatedText ; PositionOfLastSpace ) ) ;
UnprocessedText = Right ( Text ; Length ( Text ) - PositionOfLastSpace ) ;

Result = If ( Length ( Text ) ≤ Size ; Text; ThisLine & AddReturn & WordWrap ( UnprocessedText ; Size ) )
];

Result
)
 
Patrick Horn   Patrick Horn, Saarbrücken, Germany
May 8, 2012
@Tanya Sager:

Hello!
Unfortunately, your function does throw a "?" if called for example with:

Wordwrap ("1234567890123456789012345678901234567890"; 30)

Or am I missing something?
(tested with FMPA 11.0.4, Mac)

I am searching for a text wrap function which works always, it seems I have to code it on my own ... I will try ...
 
Chris Jones   Chris Jones, Oxford, UK
Jun 20, 2012
I've just uploaded a function, icio_linewrap (Text; Size; origsize) which does not throw a ? whatever you give it (at least I haven't managed to yet!).
Hope this helps all those still struggling!
 
SK8path   SK8path, Tacoma, WA
Sep 2, 2012
what is or where can I find the wordwrap ( ) or am I just missing something? Thanks much.
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Under construction. Email me your wish list for improvements.