Как пример есть вот такой текст в компоненте RichEdit:
<B>hello</B>
<I>hello.</I>
<U>подчеркнутый</U>
Пытаюсь отобразить текст как в браузере, пока только с простыми тегами форматирования текста.
Для этого набросал вот такой код, который должен отформатировать текст RichEdit'a для каждого тега:
procedure TForm1.HTMLText;
var
i: Integer;
s: string;
IsTagBegin: boolean;
IsTagEnd: boolean;
tag: string;
textStart, textEnd: integer;
tagProcessed: boolean;
count: integer;
begin
s := RichEdit1.Lines.Text;
isTagBegin := False;
IsTagEnd := False;
tag := '';
textStart := 0;
textEnd := 0;
tagProcessed := False;
count := 0;
for i := 1 to Length(s) do
begin
if (s[i] = '<') and (s[i+1] <> '/') then
begin
isTagBegin := True;
isTagEnd := False;
tagProcessed := True;
continue;
end
else
if (s[i] = '<') and (s[i+1] = '/') then
begin
textEnd := count;
isTagBegin := False;
isTagEnd := True;
continue;
end;
if isTagBegin and (s[i] <> '>') then
tag := tag + s[i]
else
if s[i] = '>' then
begin
if isTagBegin then
begin
textStart := i;
count := 0;
end;
isTagBegin := False;
isTagEnd := False;
continue;
end;
if not isTagBegin and not IsTagEnd then
count := count + 1;
if not isTagBegin and isTagEnd and tagProcessed then
begin
cxRichEdit1.SetSelection(textStart, textEnd);
if '<' + tag + '>' = '<B>' then
cxRichEdit1.SelAttributes.Style := [fsBold]
else
if '<' + tag + '>' = '<I>' then
cxRichEdit1.SelAttributes.Style := [fsItalic]
else
if '<' + tag + '>' = '<U>' then
cxRichEdit1.SelAttributes.Style := [fsUnderline];
textStart := 0;
textEnd := 0;
tag := '';
tagProcessed := False;
count := 0;
end;
end;
end;
Получаю результат не тот что надо. Оно почему-то в каждой новой строчке делает сдвиг по символам в каждой строке + 1 символ кроме первой. Вот результат: