Get the TextSettings.Font.Style property using GetObjectProp using Delphi Tokyo 10.2

I use the Delphi function GetObjectProp to get the properties of the components of the form, I get all the properties of several components, but I can not get the TextSettings.Font.Style (Bold, Italic, ...) property like TLabel for example. I need to know if the component text is in italics or italics. The procedure I'm trying to get these properties is shown below:

procedure Tfrm1.aoClicarComponente(Sender: TObject); var TextSettings: TTextSettings; Fonte: TFont; Estilo: TFontStyle; Componente_cc: TControl; begin Componente_cc := TControl(Label1); if IsPublishedProp(Componente_cc, 'TextSettings') then begin TextSettings := GetObjectProp(Componente_cc, 'TextSettings') as TTextSettings; if Assigned(TextSettings) then Fonte := GetObjectProp(TextSettings, 'Font') as TFont; if Assigned(Fonte) then Estilo := GetObjectProp(Fonte, 'Style') as TFontStyle; // <-- error in this line if Assigned(Estilo) then Edit1.text := GetPropValue(Estilo, 'fsBold', true); end end; 

The error displayed in the line where I marked above.

[dcc64 error] uPrincipal.pas (1350): E2015 The operator is not applicable to this type of operand

What am I doing wrong?

+5
source share
1 answer

GetObjectProp(Fonte, 'Style') will not work, since Style not an object-oriented property, it is a Set property. And GetPropValue(Estilo, 'fsBold', true) simply wrong (not that you got far enough to call it anyway), because fsBold not a property, it is a member of the TFontStyle enumeration. To get the value of the Style property, you should use GetOrdProp(Fonte, 'Style') , GetSetProp(Fonte, 'Style') or GetPropValue(Fonte, 'Style') (as an integer , string or variant respectively) instead.

After you get the TextSettings object, you don’t need to use RTTI at all to access the Font.Style property, just enter the property directly.

Try this instead:

 procedure Tfrm1.aoClicarComponente(Sender: TObject); var Componente_cc: TControl; TextSettings: TTextSettings; begin Componente_cc := ...; if IsPublishedProp(Componente_cc, 'TextSettings') then begin TextSettings := GetObjectProp(Componente_cc, 'TextSettings') as TTextSettings; Edit1.Text := BoolToStr(TFontStyle.fsBold in TextSettings.Font.Style, true); end; end; 

The best (and preferred) solution is to not use RTTI at all. FMX classes that have the TextSettings property also implement the ITextSettings interface for this situation, for example:

 procedure Tfrm1.aoClicarComponente(Sender: TObject); var Componente_cc: TControl; Settings: ITextSettings; begin Componente_cc := ...; if Supports(Componente_cc, ITextSettings, Settings) then begin Edit1.Text := BoolToStr(TFontStyle.fsBold in Settings.TextSettings.Font.Style, true); end; end; 

See the Embarcadero documentation for more details:

Setting text options in FireMonkey

+6
source

Source: https://habr.com/ru/post/1271378/


All Articles