Code: Select all
function GetNewStyle(rvs: TRVStyle; BaseStyleNo: Integer; FontName: string; Color: TColor;
Style: TFontStyles; Size: Integer): Integer;
begin
Result := rvs.TextStyles.FindStyleWithColor(BaseStyleNo, Color, clNone);
Result := rvs.TextStyles.FindStyleWithFontStyle(BaseStyleNo, Style, []);
Result := rvs.TextStyles.FindStyleWithFontSize(BaseStyleNo, Size);
Result := rvs.TextStyles.FindStyleWithFontName(BaseStyleNo,FontName);
if (Result < 0) then
begin
rvs.TextStyles.Add;
Result := rvs.TextStyles.Count-1;
rvs.TextStyles[Result].Assign(rvs.TextStyles[BaseStyleNo]);
rvs.TextStyles[Result].Standard := False;
rvs.TextStyles[Result].Color := Color;
rvs.TextStyles[Result].BackColor := clNone;
rvs.TextStyles[Result].Style := Style;
rvs.TextStyles[Result].Size := Size;
rvs.TextStyles[Result].FontName := FontName;
end;
end;
function GetNewPara(rvs: TRVStyle; BaseParaNo: Integer; Align: TRVAlignment): Integer;
begin
Result := rvs.ParaStyles.FindStyleWithAlignment(BaseParaNo, Align);
if (Result < 0) then
begin
rvs.ParaStyles.Add;
Result := rvs.ParaStyles.Count-1;
rvs.ParaStyles[Result].Assign(rvs.ParaStyles[BaseParaNo]);
rvs.ParaStyles[Result].Standard := False;
rvs.ParaStyles[Result].Alignment := Align;
end;
end;
procedure AddRichText(RichEd: TppRichText; Texto: String; FontName: string;
Style: TFontStyles; Size: Integer; Align: TAlignRich; Parent : TWinControl);
var
mem : TMemoryStream;
rve : TRichViewEdit;
rvs : TRVStyle;
lAlign: TRVAlignment;
begin
rve := TRichViewEdit.Create(Parent);
rvs := TRVStyle.Create(Parent);
mem := TMemoryStream.Create;
try
rve.Parent := Parent;
rve.Style := rvs;
case Align of
alrLeft : lAlign := rvaLeft;
alrCenter : lAlign := rvaCenter;
alrRight : lAlign := rvaRight;
alrJustify : lAlign := rvaJustify;
end;
rve.Clear;
rve.CurTextStyleNo := GetNewStyle(rvs,0,FontName,clBlack,Style,Size);
rve.CurParaStyleNo := GetNewPara(rvs,0,lAlign);
rve.InsertText(Texto);
rve.SelectAll;
rve.FormatTail;
mem.Clear;
rve.SaveRTFToStream(mem,False);
mem.Position := 0;
RichEd.Text := '';
RichEd.LoadFromRTFStream(mem);
finally
mem.Free;
rve.Free;
rvs.Free;
end;
end;
Code: Select all
AddRichText(MyppRichView,
'MY TEXT MY TEXT MY TEXT MY TEXT',
'Times New Roman',[],12,alrJustify, TheParentObject);
thanks.