简体   繁体   中英

Angular Material mat-form-field Placeholder text on multiple lines

I use a regular mat-form-field with a textarea inside. My issue is that the placeholder text for this Textarea is rather long. In mobile, where space is more limited, this placeholder text is truncated by Angular Material with an ellipsis.

I would like to have the placeholder text adjust to space restrictions by shifting down onto the next line. So, for example, instead of:

This is a really long text and it cannot fit on a single li...

I would like:

This is a really long text and it cannot fit on a single
line

I have already tried various approaches to changing the CSS of the mat-input-placeholder and mat-input-placeholder-wrapper , with no success. I know that part of the solution involves changing the text-overflow property (below), but I can't get the other parts.

::ng-deep .mat-input-placeholder {
  text-overflow: clip;
}

Can anyone help?

Thanks!

::ng-deep is deprecated.

Instead, you should use ViewEncapsulation and control the CSS inside the component.

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css'],
  encapsulation: ViewEncapsulation.None,
})

For placeholder (remove placeholder from element):

<mat-placeholder style="white-space: normal;">{{question.label}}</mat-placeholder>

Then add your CSS styles without ::ng-deep in the example.component.css

.mat-form-field-label {
  white-space: normal;
}

textarea.mat-input-element {
  padding: 0px 0;
  margin: 5px 0;
}

.mat-input-placeholder {
  white-space: normal;
}

wrap text:

<textarea></textarea>

<textarea
  matInput
  #fileName="ngModel"
  name="fileName"
  [(ngModel)]="action!.params.file!.originalName"
  type="text"
  autocomplete="off"
  autocapitalize="off"
  readonly
  required
  [matTooltip]="action!.params.file!.originalName"
></textarea>

overflow-clip text:

<input/>

<input
  matInput
  #fileName="ngModel"
  name="fileName"
  [(ngModel)]="action!.params.file!.originalName"
  type="text"
  autocomplete="off"
  autocapitalize="off"
  readonly
  required
  [matTooltip]="action!.params.file!.originalName"
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM