3

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.

1

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.

2

You can achieve it via:-

  1. Apex :- replace newline character by html line break (< br/>) and use the resultant string value to display on page

    String displayValue = System.label.label_name.replace('\n','
    ');

  2. Javascript :- Use javascript to replace newline character by html line break

< div id="testDiv" > < /div>

    <script>
          var val = '{!JSENCODE($Label.test_label)}';
          val = val.replace(/\n/g,'<br\>');
          document.getElementById('testDiv').innerHTML = val; //for testing purposes
    </script>
  • Thanks for the response. Unfortunately the newline isn't coming through to the visualforce page at all. There isn't any way to access this in the browser. I already know I can do it in apex code, I'm just hoping there is a different way that works without an extra layer of code. – Nick Cook Jun 5 '15 at 5:00
  • @NickCook: The javascript method is what I tested just now and it works fine at my end, the value for label is in following format (can't format text in comments appropriately) Line 1, Line 2, Line 3 – Anshul Jun 5 '15 at 5:38
  • For whatever reason I'm unable to replicate this on my page. It's a pretty complex page, so maybe something else is going on. Thanks anyway. – Nick Cook Jun 8 '15 at 23:39
  • which option did you tried? Apex or Javascript ?? – Anshul Jun 9 '15 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.