Is it possible to have newline character in Ionic2 toast?

10,797

Solution 1

Actually it IS possible. You can do the following:

.toast-message {
  white-space: pre;
}

and \n for a line-break.

Note: Take a look at home.ts and style.css.

See working plunkr

Solution 2

Although @iWork solution works for many situation but if you have close button in your toast it will be pushed out of screen.

So you can use these style sheet instead:

.toast-message {
  white-space: pre-line;
}

p.s you need to use \n for line break in your string

Share:
10,797
Ankit Maheshwari
Author by

Ankit Maheshwari

I rather enjoy coding because it gives me an experience of complete absorption, almost like meditation — If Truth Be Told. Working with a team of highly devoted, experienced and skilled programmers to develop new products and services. We bring innovative products for business and ideas to life using modern technology. Mostly self-taught, we use the following technologies on a day-to-day basis: Ionic Framework, React Native, HTML/CSS/SASS, Javascript, AngularJS, Node.js, Polymer Library, Material Design, Web Components, Progressive Web Apps, AMP, Python, Django, Java, Google Firebase, Google Cloud Platform.

Updated on July 27, 2022

Comments

  • Ankit Maheshwari
    Ankit Maheshwari almost 2 years

    I tried both \n and < br/ >, but unfortunately not working!

    Is this Possible ?

    //Displaying toast to welcome user!
    let user = this.currentUser();
    //console.log(user);
    let toast = Toast.create({
      message: 'Hi ' + user.email + '! <br/> Welcome to My App',
      duration: 5000,
      position: 'bottom'
    });
    
    toast.onDismiss(() => {
      console.log('Dismissed toast');
    });
    
    this.nav.present(toast);

  • Ankit Maheshwari
    Ankit Maheshwari over 7 years
    Somehow it is not working in actual application, I written same code over plunkr (working!) but not working in my type-script file. OR this may be the issue in Ionic2
  • A.W.
    A.W. over 7 years
    Ok this is working. I first used <br/> but it works using '\n` as in the answer.
  • El Dude
    El Dude over 7 years
    I didn't have to do the css, \n worked out of the box. <br/> was a deadend