Firemonkey: How to align properly label on the right?

天大地大妈咪最大 提交于 2019-12-10 11:39:24

问题


If I do

RectangleCont.beginUpdate;

myText1 := TText.Create(RectangleCont);
myText1.parent := RectangleCont;
myText1.text := 'a long text'; 
myText1.Position.x := RectangleCont.width - myText1.width; // 150px for exemple
myText1.align := TalignLayout.right;

myText2 := TText.Create(RectangleCont);
myText2.parent := RectangleCont;
myText2.text := 'OK';
myText2.Position.x := RectangleCont.width - myText1.width - myText2.width; // 50px for exemple
myText2.align := TalignLayout.right;

myText3 := TText.Create(RectangleCont);
myText3.parent := RectangleCont;
myText3.text := 'OK';
myText3.Position.x := RectangleCont.width - myText1.width - myText2.width - myText3.width; // 0px for exemple
myText3.align := TalignLayout.right;

RectangleCont.EndUpdate;

I will have <myText1><myText3><myText2> instead of <myText3><myText2><myText1>

Note: if I put shorter text in myText1.text then it works properly.

Is there any workaround?


回答1:


The Delphi Align property is good for most situations, but when you have more rare needs, it is not difficult to write your own resize event. Simply skip setting the Align and Position properties in your code, and instead use this code (it is untested, but I think you get the idea, in case I forgot something):

...
RectangleCont.OnResize := DoResizeRectangle;
...


procedure TMainForm.DoResizeRectangle(aSender: TObject);
var lParent: TControl;
begin
  lParent := TControl(aSender);
  myText1.Height := lParent.Height;
  myText2.Height := lParent.Height;
  myText3.Height := lParent.Height;
  myText1.Position.Point := PointF(lParent.Width-myText1.Width, 0);
  myText2.Position.Point := PointF(myText1.Position.X-myText2.Width, 0);
  myText3.Position.Point := PointF(myText2.Position.X-myText3.Width, 0);
end;



回答2:


myText1.align := TalignLayout.left;
myText2.align := TalignLayout.right;
myText1.align := TalignLayout.right;


来源:https://stackoverflow.com/questions/44084850/firemonkey-how-to-align-properly-label-on-the-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!