[SalesForce] n easy way to show a multi-line Custom Label in a Visualforce page with the line breaks working correctly

I'm looking to display a Custom Label on a Visualforce page, however when the Custom Label has line breaks in it, they're not coming through to the Visualforce page.

If I inspect the html, the line breaks aren't there either, so using a <pre> as suggested in How to show the multi-line string in wrapper class in a VF page won't work.

From what I can see, this behaviour is different to a standard Object's text field that contains line breaks.

The only way I can think of it is to add a method in the controller that updates the string with <br /> elements, and it's the output of that method that's displayed rather than the Custom Label itself.

Has anyone come across this or have any other suggestions for a more straight forward solution?

The purpose of this is to display an address on multiple different visualforce pages that is likely to change, hence using a Custom Label rather than embedding it directly in the page.

Best Answer

This is something which cannot be achieved by purely using custom labels. Any line breaks in the custom label is lost and the text is displayed as a single line on VF page.

This is what I do.

Split the string with any random character.

If the custom label is 'This is Line One. This is Line Two', I will change it to 'This is Line One. 555 This is Line Two'.

Replace '555' with any random character that you find it easy to remember.

String s = Label.YourLabelName;
List<String> l = s.split('555');

l[0] will contain the first sentence and l[1] will contain the second sentence. Use this list on VF page.