Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Dynamic Font Size


This question is answered.


Permlink Replies: 1 - Last Post: Sep 23, 2017 7:08 AM Last Post By: Markus Humm
Brian Wheatley

Posts: 60
Registered: 2/5/01
Dynamic Font Size  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 22, 2017 11:05 AM
I am working on a mobile application and would like to be able to adjust the font size of a label dynamically based on the width of the label. The goal is to reduce the font size until the label fits properly on whatever size display the user has.

Does anyone have an example of doing this?
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Dynamic Font Size
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 23, 2017 7:08 AM   in response to: Brian Wheatley in response to: Brian Wheatley
Am 22.09.2017 um 20:05 schrieb Brian Wheatley:
I am working on a mobile application and would like to be able to adjust the font size of a label dynamically based on the width of the label. The goal is to reduce the font size until the label fits properly on whatever size display the user has.

Does anyone have an example of doing this?

Hello,

you need a TTextLayout for this. The following function gets nthe width
of a text in logical pixel, the rest of your algorithm should be easy
enough for you I guess.

One hint: creating a TTextLayout is comparatively time consuming.

function TYourClass.MeasureTextWidth(const AFont: TFont; const AText:
string): Single;
var
  LLayout: TTextLayout;
begin
  LLayout := TTextLayoutManager.DefaultTextLayout.Create;
  try
    LLayout.BeginUpdate;
    try
      LLayout.WordWrap := False;
      LLayout.Font.Assign(AFont);
      LLayout.Text := AText;
    finally
      LLayout.EndUpdate;
    end;
    Result := LLayout.TextWidth;
  finally
    LLayout.Free;
  end;
end;
 


Greetings

Markus
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02