AddTextNL with Style Template

General TRichView support forum. Please post your questions here
Post Reply
RedOne
Posts: 1
Joined: Fri May 31, 2013 7:26 am

AddTextNL with Style Template

Post by RedOne »

Hello

I like the new feature of the style templates in RichView 14.
But I have some understanding problems how the style templates and the textstyles are connected.
As an example, how can I add a text and set a proper style template?
Have I to use the StyleTemplateId-property?

One of many attempts:

Code: Select all

function FindTextStyleByName( AName: string ): TFontInfo;
var
  i: Integer;
begin
  for i:= 0 to rvEdit.Style.TextStyles.Count - 1 do begin
    if SameText( rvEdit.Style.TextStyles.Items[ i ].StyleName, AName ) then begin
      Result:= rvEdit.Style.TextStyles.Items[ i ];
      exit;
    end;
  end;
end;

procedure AddText;
begin
  rvEdit.Clear;
  rvEdit.AddTextNL( 'Text without style', -1, -1, -1 );
  FindTextStyleByName( 'heading 1' ).StyleTemplateId:= rvEdit.Style.StyleTemplates.FindItemByName( 'heading 1' ).Id;
  rvEdit.AddTextNL( 'Text with style', FindTextStyleByName( 'heading 1' ).Index, -1, -1 );
// or with .ID?
  rvEdit.AddTextNL( 'Text with style', FindTextStyleByName( 'heading 1' ).ID, -1, -1 );
end;

// Change a text style. This works fine
procedure TForm31.ButtonChangeStyleClick(Sender: TObject);
var
  index: Integer;
  frm: TfrmRVStyles;
  tmpstyle: TRVStyle;
begin
  tmpstyle := TRVStyle.Create(Self);
  tmpstyle.StyleTemplates.AssignStyleTemplates( rvEdit.Style.StyleTemplates, true);
  with tmpstyle.StyleTemplates.FindItemByName( 'heading 1' ) do
  begin
   TextStyle.Color := clRed;
   ValidTextProperties := ValidTextProperties + [rvfiColor];
  end;

  rvEdit.ChangeStyleTemplates(tmpstyle.StyleTemplates);
  rvEdit.RVData.PaintBuffered;

  tmpstyle.free;
end;

Thanks
RedOne
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

1) Your AddText procedure really links a text style with a style template.
(Index must be used, TFontInfo does not have Id property)

However, this procedure does not assign properties of StyleTemplate to the text style. If you want to do it, instead of

Code: Select all

FindTextStyleByName( 'heading 1' ).StyleTemplateId:= rvEdit.Style.StyleTemplates.FindItemByName( 'heading 1' ).Id;
call

Code: Select all

FindTextStyleByName( 'heading 1' ).ModifiedProperties := [];
rvEdit.Style.StyleTemplates.FindItemByName( 'heading 1' ).ApplyToTextStyle(FindTextStyleByName( 'heading 1' ), nil);
2) Normally, the style templates "Normal", "heading 1", "heading 2", etc. are applied not to text, but to paragraphs (and affect all text inside paragraphs). An example of creating a new document with paragraph linked to "Normal" style template is here: http://www.trichview.com/help/idh_trvst ... style.html

3) The demo showing how to work with StyleTemplates without RichViewActions can be found in TRichView demos, in Demos\*\Editors\StyleTemplates\
Post Reply