Firemonkey运行时创建TLabel时Font.Size设置不生效问题
FireMonkey运行时创建TLabel字号不生效问题说明
核心原因
- FireMonkey中所有带文本显示的控件,其
TextSettings属性下默认开启了样式继承规则,TextSettings.Styled默认值为True,此时控件文本的字体、字号、颜色等配置会优先继承父控件或全局主题样式的设置,手动修改的字体相关属性会被覆盖,无法生效。 - 设计界面中修改控件字号时,IDE会自动将
TextSettings.Styled属性修改为False,关闭样式继承,所以自定义字号可以正常显示。
修复方法
在运行时设置TLabel的字号前,先关闭样式继承,添加一行代码:
Etiqueta.TextSettings.Styled := False;
修正后的完整代码
procedure TForm2.FormCreate(Sender: TObject); var Shape: TRectangle; Etiqueta: TLabel; begin Shape := TRectangle.Create(Self); Shape.Align := alClient; Shape.Parent := Self; Etiqueta := TLabel.Create(Shape); Etiqueta.Position.X := 20; Etiqueta.Position.Y := 50; Etiqueta.Width := 200; Etiqueta.TextSettings.Styled := False; // 关闭样式继承,自定义配置才会生效 Etiqueta.TextSettings.Font.Size := 10; Etiqueta.Text := 'Small Font'; Etiqueta.Parent := Shape; Etiqueta := TLabel.Create(Shape); Etiqueta.Position.X := 20; Etiqueta.Position.Y := 100; Etiqueta.Width := 200; Etiqueta.TextSettings.Styled := False; // 关闭样式继承,自定义配置才会生效 Etiqueta.TextSettings.Font.Size := 20; Etiqueta.Text := 'Large Font'; Etiqueta.Parent := Shape; end;
内容的提问来源于stack exchange,提问作者Marc Guillot
相关产品推荐
相关产品推荐

