1

I'm trying to make a Rectangle to show some information. Inside this Rectangle I have a lot of labels, and in this labels I want to show some texts in bold. For example: 'Name:' <- Bold, and after this not bold.

If this is not possible, how can I know the size in pixels of a label.text? If I have this information, I can create two label and set the position of the second to be: label2.position.X := label1.textWidth;

Thanks!

7
  • The easiest solution is to use separate labels. Commented Nov 29, 2016 at 18:29
  • I'm creating this in run time, so, if I use 2 labels, I need to know where the text of the first ends. How can I know this? Commented Nov 29, 2016 at 18:31
  • 3
    label1.Canvas.TextWidth(label1.Caption) Commented Nov 29, 2016 at 18:35
  • 2
    Or, by default Labels have auto-size enabled. Commented Nov 29, 2016 at 18:41
  • 2
    You could set Label2.Position.X := Label1.Position.X + Label1.Width after assigning Label1.Caption. A better option would be to place the two Labels on a TRelativePanel and then you can set Label2.RightOf = Label1 so it stays to the right of Label1 regardless of any changes to Label1's width. See Using the Relative Panel. Commented Nov 29, 2016 at 21:23

1 Answer 1

4

Using the standard label control your only option is to use two such controls, one with Font.Style including fsBold, the other not.

Place your first, bold label then as long as you leave/set the AutoSize property true, the Width property will tell you the width:

// Where: // // - boldLabel is a created, initialised and positioned // label with bold text // // - normalLabel is a created and initialised label which // has not yet been positioned (horizontally) // // - spacingPixels is the distance you wish to maintain // between the two normalLabel.Position.X := boldLabel.Position.X + boldLabel.Width + spacingPixels; 

There are a number of 3rd party label controls, many of them free + open source (for VCL [see below]), which support varying degrees of markup in a label. There may be similar implementations for FMX.

For VCL projects you might want to check out the JediVCL library which includes a label supporting not just bold but other, albeit limited HTML markup. If this is of interest, the control you are looking for in that library is TJvHTLabel.

NB. For future ref: You don't specify whether your project is FMX or VCL but it appears from the use of the Position property that it is likely to be FMX. For issues involving controls, the framework in use can be a significant factor and should be mentioned to avoid eliciting answers that may not be relevant.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.