Цветной текст в той же строке в TRichEdit
Как написать текст в одной строке, но с другим цветом? (Я использую richedit).
procedure TForm1.btnEClick(sender: TObject);
begin
m0.SelAttributes.Color := clBlue;
m0.SelAttributes.Style := [fsBold];
m0.lines.add('This is blue and it is bold');
m0.SelAttributes.Color := clGreen;
m0.SelAttributes.Style := [fsBold];
m0.lines.add ('This is Green and it is bold');
m0.lines.add('');
m0.lines.add('But how to write text in the same line with different color?');
// i want to have both blue and green in the same line
end;
С наилучшими пожеланиями,
пчелка
Ответы
Ответ 1
Ты на правильном пути. Просто измените SelAttributes
и используйте SelText
вместо Lines.Add
:
procedure TForm4.FormCreate(Sender: TObject);
begin
RichEdit1.Clear;
RichEdit1.SelAttributes.Color := clBlue;
RichEdit1.SelAttributes.Style := [fsBold];
RichEdit1.SelText := 'This is bold blue text.';
RichEdit1.SelAttributes.Color := clRed;
RichEdit1.SelAttributes.Style := [fsItalic];
RichEdit1.SelText := #32'This is italic red text';
end;
Это создает
![Sample output from code above]()
Ответ 2
Для последнего фрагмента текста в строке добавьте возврат каретки, чтобы завершить строку.
RichEdit1.SelAttributes.Color := clGreen;
RichEdit1.SelAttributes.Style := [];
RichEdit1.SelText := 'This is the last piece of text on the line.' + #13;