最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add a line break to text in UI5? - Stack Overflow

programmeradmin5浏览0评论

The escape character \n and Unicode character \u000a will only work in JavaScript. However, I'm trying to add a line break in XML view as below. But doesn't work:

<u:TimelineItem text="First Line\n SecondLine" />

The escape character \n and Unicode character \u000a will only work in JavaScript. However, I'm trying to add a line break in XML view as below. But doesn't work:

<u:TimelineItem text="First Line\n SecondLine" />
Share Improve this question edited Jan 18, 2021 at 2:50 Boghyon Hoffmann 18.1k14 gold badges93 silver badges202 bronze badges asked Nov 7, 2017 at 0:27 ManjunathManjunath 1351 gold badge1 silver badge8 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 10

New lines in text controls can be added with the following characters:

  • In XML views or XML fragments:

    • Line feed: &#10; or &#x0A;.
    • Remended:* In bination with carriage return: &#13;&#10; or &#x0D;&#x0A;.
  • In JS or i18n.properties files:

    • Line feed: \n or \u000a.

    • Remended:* In bination with carriage return: \r\n or \u000d\u000a.

    • Alternatively, consider using template literals instead of concatenating the above characters manually (i.e. simply replace "..." with `...`).

  • Some UI5 controls allow the HTML tag <br> (in XML: &lt;br>) out of the box:
    • sap.m.MessageStrip since UI5 v1.85: API reference, Sample
    • sap.m.FormattedText: API reference

* See Issues with different newline formats. It is remended to use the bination with Carriage Return for most of the internet protocols.


Here is a UI5 demo with sap.suite.ui.mons.TimelineItem* and sap.m.Text:

globalThis.onUI5Init = () => sap.ui.require([
  "sap/ui/core/mvc/XMLView",
  "sap/m/Text",
], async (XMLView, Text) => {
  "use strict";
  
  const view = await XMLView.create({
    definition: `<mvc:View xmlns:mvc="sap.ui.core.mvc" height="100%">
      <App xmlns="sap.m" autoFocus="false">
        <Page showHeader="false" class="sapUiResponsiveContentPadding">
          <mons:TimelineItem xmlns:mons="sap.suite.ui.mons"
            text="Multiline supported&#10;in Timeline items (XML)"
          />
          <HBox id="myBox" justifyContent="SpaceAround">
            <Text
              text="This&#10;is&#x0A;a&#13;&#10;text (created in XML view)!"
              renderWhitespace="true"
            />
          </HBox>
        </Page>
      </App>
    </mvc:View>`,
  });
  
  const textCreatedInJS = new Text({
    renderWhitespace: true,
    text: "And this\nis\u000aanother\r\ntext (created in JS)!",
  });
  view.byId("myBox").addItem(textCreatedInJS);
  view.placeAt("content");
});
<script id="sap-ui-bootstrap"
  src="https://ui5.sap./resources/sap-ui-core.js"
  data-sap-ui-libs="sap.ui.core,sap.m,sap.suite.ui.mons"
  data-sap-ui-theme="sap_fiori_3"
  data-sap-ui-oninit="onUI5Init"
  data-sap-ui-async="true"
  data-sap-ui-patversion="edge"
  data-sap-ui-excludejquerypat="true"
  data-sap-ui-xx-waitfortheme="init"
></script>
<body id="content" class="sapUiBody"></body>

* The TimelineItem had a bug in earlier versions of UI5 preventing multilines. According to their changelog for 1.44.5:

[FIX] sap.suite.ui.mons.Timeline: Rendering of multiline text improved


In case the control sap.m.Text is used, keep in mind to enable the properties renderWhitespace and wrapping in order render the new lines in the DOM.


For UI5 Control Developers

Rendering text in DOM can be achieved via the API .text(/*...*/) from the RenderManager. That API, however, doesn't necessarily apply newlines even if the text contains the above mentioned line break characters. In that case, the white-space property pre-line can be applied to the control's CSS style:

.myControlWithText {
  /* ...; */
  white-space: pre-line; /* Allows line break characters to be applied */
}

Rather than \n you can use the expression:

'&lt;br/>'

. For example:

Text="{TOPOLINO} - {=${PAPERINO} === 'NA' || ${PAPERINO} === 0 ? '---' : ${PAPERINO}}&lt;br/>{PAPERONE} "

You can use the embeddedControl aggregation to use the text control inside TimelineItem

<u:TimelineItem>
      <u:embeddedControl><Text text="First Line\n SecondLine"></Text></u:embeddedControl>
</u:TimelineItem>
发布评论

评论列表(0)

  1. 暂无评论