【问题标题】:Colorful text in the same line in TRichEditTRichEdit 中同一行中的彩色文本
【发布时间】:2013-11-25 05:18:55
【问题描述】:

如何在同一行写不同颜色的文本? (我使用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;

最好的祝愿, 蜜蜂

【问题讨论】:

    标签: delphi fonts richedit


    【解决方案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;
    

    这会产生

    【讨论】:

      【解决方案2】:

      如果您使用主题...上面的答案将不起作用..
      你看不到任何颜色... 直到您从样式中删除 seFont..

      RichEdit1.styleElements:=richedit1.styleElements-[seFont];
      

      例如。

      ....
      amsg:='Hola';
      
      RichEdit1.SelStart := Length(RichEdit1.Lines.Text);
      RichEdit1.SelAttributes.Color := acolor;
      RichEdit1.Lines.Add(amsg + sLineBreak);
      RichEdit1.SelLength := Length(amsg + sLineBreak);
      

      【讨论】:

        【解决方案3】:

        对于该行中的最后一段文本,包括回车以结束该行。

        RichEdit1.SelAttributes.Color := clGreen;
        RichEdit1.SelAttributes.Style := [];
        RichEdit1.SelText := 'This is the last piece of text on the line.' + #13;
        

        【讨论】:

        • 我认为这不能回答 OP 的问题。 (Ken White 可能确实回答了预期的问题。)
        • 是的。肯的​​回答是正确的。我应该将我的答案添加为评论,因为它更像是(希望)有用的提示而不是答案。 (我没有足够的声望点来添加 cmets。)
        • 好的,很公平! :)
        猜你喜欢
        • 2011-12-29
        • 1970-01-01
        • 2021-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-26
        • 1970-01-01
        • 2021-12-20
        相关资源
        最近更新 更多