The novice consult.
How to achieve when encountering the string text color of the paragraph with the first letter "A:" is A color; When you encounter the string text color of the paragraph with the first letter of "B:", it changes to another color.
See rendering
How is the code implemented, thanks for your help
Text formatting color problem
Text formatting color problem
- Attachments
-
- 颜色.jpg (45.74 KiB) Viewed 13053 times
-
- Site Admin
- Posts: 17524
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Re: Text formatting color problem
Code: Select all
uses RVTypes;
type
TTextColorPair = record
Text: TRVUnicodeString;
Color: TColor;
end;
TTextColorPairs = array of TTextColorPair;
// Returns color for Text from the rules listed in TextColorPairs
function GetColorForText(const Text: TRVUnicodeString;
const TextColorPairs: TTextColorPairs): TColor;
var
i: Integer;
begin
Result := clNone;
for i := Low(TextColorPairs) to High(TextColorPairs) do
if Copy(Text, 1, Length(TextColorPairs[i].Text)) = TextColorPairs[i].Text then
begin
Result := TextColorPairs[i].Color;
exit;
end;
end;
// Returns index of in rvs.TextStyles of the style having all properties of
// rvs.TextStyles[StyleNo], but the specified color.
// If it does not exist, it is created
function GetStyleNoWithColor(rvs: TRVStyle; StyleNo: Integer; Color: TColor): Integer;
var
TextStyle: TFontInfo;
begin
if rvs.TextStyles[StyleNo].Color = Color then
begin
Result := StyleNo;
exit;
end;
TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rvs.TextStyles[StyleNo]);
TextStyle.Color := Color;
Result := rvs.FindTextStyle(TextStyle);
TextStyle.Free;
end;
// Changes colors of text in rv basing on rules specified TextColorPairs
procedure ColorLines(rv: TCustomRichView; const TextColorPairs: TTextColorPairs);
var
i: Integer;
Color: TColor;
begin
Color := clNone;
for i := 0 to rv.ItemCount - 1 do
begin
if rv.IsParaStart(i) then
if rv.GetItemStyle(i) >= 0 then
Color := GetColorForText(rv.GetItemTextW(i), TextColorPairs)
else
Color := clNone;
if (Color <> clNone) and (rv.GetItemStyle(i) >= 0) then
rv.GetItem(i).StyleNo := GetStyleNoWithColor(rv.Style, rv.GetItemStyle(i), Color);
end;
end;
Code: Select all
var
ColorRules: TTextColorPairs;
begin
SetLength(ColorRules, 2);
ColorRules[0].Text := 'A';
ColorRules[0].Color := clRed;
ColorRules[1].Text := 'B';
ColorRules[1].Color := clBlue;
ColorLines(RichViewEdit1, ColorRules);
RichViewEdit1.Invalidate;
end;
In this case, ColorLines must be:
Code: Select all
procedure ColorLines(rv: TCustomRichView; const TextColorPairs: TTextColorPairs);
var
i: Integer;
Color: TColor;
begin
Color := clNone;
for i := 0 to rv.ItemCount - 1 do
begin
if rv.IsParaStart(i) then
if rv.GetItemStyle(i) >= 0 then
Color := GetColorForText(rv.GetItemTextW(i), TextColorPairs)
else
Color := clNone;
if (Color <> clNone) and (rv.GetItemStyle(i) >= 0) and
(rv.Style.TextStyles[rv.GetItemStyle(i)].Color = clWindowText) then
rv.GetItem(i).StyleNo := GetStyleNoWithColor(rv.Style, rv.GetItemStyle(i), Color);
end;
end;
Re: Text formatting color problem
Sergey Tkachenko Thank you very much