Einzelnen Beitrag anzeigen

Benutzerbild von Mavarik
Mavarik

Registriert seit: 9. Feb 2006
Ort: Stolberg (Rhld)
4.130 Beiträge
 
Delphi 10.3 Rio
 
#1

Layout.AddAttributes Fehler oder Feature?

  Alt 27. Nov 2013, 18:37
Hallo Zusammen!

Wir mal was neues...

gegeben sei folgende Funktion:

Delphi-Quellcode:
  Procedure DP(Var AFont : TFont;Var ALayout:TTextLayout);
  begin
    ALayout.ClearAttributes;
    AFont.Size := 12;
    ALayout.AddAttribute(TTextRange.Create(10,20),TTextAttribute.Create(AFont,TAlphaColorRec.Black));
    AFont.Size := 22;
    ALayout.AddAttribute(TTextRange.Create(40,60),TTextAttribute.Create(AFont,TAlphaColorRec.Black));
  end;
Funktioniert das? Nein... Warum nicht? Weil der Font-Teil keine Klasse ist, sondern in einen Record kopiert wird.
Somit werden beide Bereiche mit Font.Size = 22 gerändert.

Noch besser finde ich jedoch:

Delphi-Quellcode:
  Procedure DP(Var AFont : TFont;Var ALayout:TTextLayout);
  Var LP : TFont;
  begin
    ALayout.ClearAttributes;
    AFont.Size := 12;
    LP := TFont.Create;
    LP.Assign(AFont);
    ALayout.AddAttribute(TTextRange.Create(10,20),TTextAttribute.Create(LP,TAlphaColorRec.Black));
    AFont.Size := 22;
    LP := TFont.Create;
    LP.Assign(AFont);
    ALayout.AddAttribute(TTextRange.Create(40,60),TTextAttribute.Create(LP,TAlphaColorRec.Black));
  end;
Da der Layout.EndUpdate außerhalb des Procedure Scope ist hat ARC die TFonts gekillt und es gibt eine nette Exception!

Ich finde das ist ein Designfehler!

Mavarik

Geändert von Mavarik (28. Nov 2013 um 06:24 Uhr)
  Mit Zitat antworten Zitat