Page 1 of 1
inserting text at the top with different style
Posted: Wed Mar 18, 2015 1:52 pm
by Lucian
Hi,
I have some stuff in a TRichViewEdit. At the top, there is a piece of text, centered and some whatever font and color.
I would like to insert few lines of text above, at the top, and I would like that my new inserted text to have some specific style. No matter what I tried, the inserted piece is sticked to the text at the top and the style is screwed up one way or the other.
The help file for InsertText says "Inserted text has current text and current paragraph style." I understand that. But how do I insert a piece of text at the top and give it a different style *without* breaking the existing body?
Thanks
Posted: Wed Mar 18, 2015 2:02 pm
by Sergey Tkachenko
Code: Select all
// moving to the beginning of the document
rve.SetSelectionBounds(0, rve.GetOffsBeforeItem(0), 0, rve.GetOffsBeforeItem(0));
// changing the current text style
rve.CurTextStyleNo := ...
// inserting text
rve.InsertText(...);
not working
Posted: Thu Mar 19, 2015 8:08 am
by Lucian
Nope, this is still not working for me. The inserted text (which I would like it to be left aligned) picks up the alignment from the first paragraph of the body (which is centered).
My inserted text looks like this:
_______'#$D#$A#$D#$A'
[email protected] schrieb am 18.03.2015 14:53:08: '#$D#$A#$D#$A
Thanks,
more on this
Posted: Thu Mar 19, 2015 8:12 am
by Lucian
Also, if I set the para style, like this:
rve.CurParaStyleNo := ...
Than this breaks the body of the original text. The paragraph which was centered (at the top) it becomes left aligned.
Posted: Thu Mar 19, 2015 10:22 am
by Sergey Tkachenko
Code: Select all
// moving to the beginning of the document
rve.SetSelectionBounds(0, rve.GetOffsBeforeItem(0), 0, rve.GetOffsBeforeItem(0));
// changing the current text style
rve.CurTextStyleNo := ...
// inserting an empty paragraph
rve.InsertText(#$D#$A);
// applying the required paragraph style to this empty paragraph
rve.ApplyParaStyle(...);
// inserting the rest of text
rve.InsertText(#$D#$A'[email protected] schrieb am 18.03.2015 14:53:08: '#$D#$A#$D#$A
);
not working
Posted: Thu Mar 19, 2015 11:14 am
by Lucian
I am still not having luck. I do 3 insertions at the top of the message (it's an email message getting answered) and they don't look ok.
Plus, your recommendation to insert an empty paragraph is not good for me as this is inserting additional white space not desired.
I think the flow should be as simple as this:
1. position the cursor where we want it;
2. prepare "that location" for inserting text:
- set font attributes
- set paragraph attributes
3. Insert the text.
Another possibility could be:
1. position the cursor where we want it;
2. insert the text (say that the text is 30 characters long)
3. select 30 characters and apply whatever formats to the selection
This also does not work for me because the selection I made does not really equals the length of the text I am inserting. I was using methods from RVLinear, but I don't know what I am doing wrong, the selection is bad, so this is not working also.
Posted: Thu Mar 19, 2015 11:30 am
by Sergey Tkachenko
Yes, my code has a bug: after InsertText(#$D#$A), the caret is in the second paragraph, so we need to return it back.
If you remove #$D#$A from the beginning of the second InsertText, the inserted text will be inserted in very beginning.
I tested in the ActionTest demo:
Code: Select all
function GetGreenTextStyle(RVStyle: TRVStyle): Integer;
var TextStyle: TFontInfo;
begin
TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(RVStyle.TextStyles[0]);
TextStyle.Color := clGreen;
Result := RVStyle.FindTextStyle(TextStyle);
TextStyle.Free;
end;
function GetRightAlParaStyle(RVStyle: TRVStyle): Integer;
var ParaStyle: TParaInfo;
begin
ParaStyle := TParaInfo.Create(nil);
ParaStyle.Assign(RVStyle.ParaStyles[0]);
ParaStyle.Alignment := rvaRight;
Result := RVStyle.FindParaStyle(ParaStyle);
ParaStyle.Free;
end;
procedure TForm3.ToolButton73Click(Sender: TObject);
begin
RichViewEdit1.SetSelectionBounds(0, RichViewEdit1.GetOffsBeforeItem(0),
0, RichViewEdit1.GetOffsBeforeItem(0));
RichViewEdit1.CurTextStyleNo := GetGreenTextStyle(RVStyle1);
RichViewEdit1.InsertText(#$D#$A);
RichViewEdit1.SetSelectionBounds(0, RichViewEdit1.GetOffsBeforeItem(0),
0, RichViewEdit1.GetOffsBeforeItem(0));
RichViewEdit1.ApplyParaStyle(GetRightAlParaStyle(RVStyle1));
RichViewEdit1.InsertText('[email protected] schrieb am 18.03.2015 14:53:08: '#$D#$A#$D#$A);
end;
This code adds a text line and two empty paragraphs after it.
If you remove the final #$D#$A#$D#$A, there will be no empty lines.
test case (still not working for me)
Posted: Thu Mar 19, 2015 12:13 pm
by Lucian
I just sent you a very simple test case.
regards
Lucian
testcase 2
Posted: Thu Mar 19, 2015 12:18 pm
by Lucian
I resent the project, please use test2.zip
Lucian
Posted: Sun Mar 22, 2015 11:23 am
by Sergey Tkachenko
I received your code.
You need to move the caret to the beginning before applying the paragraph style, not after.
All right!
Posted: Sun Mar 22, 2015 4:44 pm
by Lucian
It worked in the test and I understand now. I'll work this on the application and see if my problem is gone.
Thank you,
Lucian