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!
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.
Related
I'm implementing a TListView in a Firemonkey app, where the height of each list item must be as small as possible, just large enough to fit the text. However, there is an apparent margin on the top of the text, leaving an empty space at the beginning of each list item.
The item appearance is set to ListItemRightDetail, but I cannot find any properties anywhere to allow me to control this - neither in the FMX design or the Styles for the List View.
How do I eliminate the margin at the top of list item text so that I can minimize the list item heights?
There are not necessarily any "margin" properties, as this margin is pretty much built-in. However, this can be adjusted by modifying the PlaceOffset.Y property, and making them a negative value. This will "lift up" the contents within each list item. I find -3 to be a sufficient amount to reduce this margin...
ListView1.ItemAppearanceObjects.ItemObjects.Text.PlaceOffset.Y := -3;
ListView1.ItemAppearanceObjects.ItemObjects.Detail.PlaceOffset.Y := -3;
I am using buttons to display product names in a matrix using TGridLayout.
The problem is that commonly Items contains 3 or 4 words and in my language (Portuguese) some words tend to be long.
I would like that somehow I could calculate the size of the font, decreasing it, in order to make all the text show up automatically (of course there is also a decrease limit, anything below 9 or 8 point for the font turn to be difficult to read).
The wordwrap property is turned on to have many lines, and use the most possible space for the text.
I don't know if you're programming for an Android/iOS app, but you can't change the fontsize of a button. I've had the same problem, my solution was to make an abbreviation of the words. And then i put labels above it to explain the abbreviations.
Of course you can adjust the font size of a button:
TButton.TextSettings.Font.Size
Moving from TBX to SpTBX. I used in the past TTbxDockablePanel which has such props:
DockedWidth
DockedHeight
FloatingWidth
FloatingHeight
I can't find such properties in SpTBX panels, so how should I change my code to use such props? ie. i need to get/set SpTBX panel's docked width/height and floating width/height.
DockedWidth and DockedHeight
A TSpTBXDockablePanel now has a property DefaultDockedSize. A docked panel is docked to either a vertical or horizontal dock, so it only needs one size, which specifies the width (in a vertical dock) or height (in a horizontal dock.) The length of the other axis is a result of either the dock size or the other panels in the same dock. If there are other panels, the size it becomes is the width of the other panels in the dock. If there aren't, it will use either the previous size (if DefaultDockedSize is 0) or try to use DefaultDockedSize.
See TSpTBXCustomDockablePanel.SetParent for the code. You'll be interested in the two branches beginning if ToDock then... and if ToFloating then.... There are some comments there that I've tried to interpret to write the above.
From memory, when I converted from TBX to SpTBX years ago, this was the hardest area to get behaving exactly the same. However, you can get it to behave 'okay', ie with behaviour that makes sense, easily.
FloatingWidth and FloatingHeight
Replaced by FloatingClientWidth and FloatingClientHeight. Note that these are the client sizes not window sizes, but in general they should be direct replacements.
In addition
There is a comment at the top of the SpTBXDkPanels.pas file,
// - To handle the size constraints use GetMinMaxSize when the DP is floating,
// and ConstrainedResize when is Docked (explicitly check if it's docked).
You might find that useful too.
There are a reasonable number of comments scattered through the code. The SpTBX library doesn't have very good documentation, but it does come with high-quality source... Use the source, Luke :)
It's a TMemo, not that that should make any difference.
Googling suggests that I can use Canvas->TextWidth() but those are Delphi examples and BCB doesn't seem to offer this property.
I really want something analogous to memo->Font->Height for width.
I realize that not all fonts are fixed width, so a good estimate will do.
All that I need is to take the width of a TMemo in pixels and make a reasonable guess at how many characters of the current font it will hold.
Of course, if I really want to be lazy, I can just google for the average height/width ratio, since height is known. Remember, an approximation is good enough for me if it is tricky to get exact.
http://www.plainlanguagenetwork.org/type/utbo211.htm says, " A width to height ratio of 3:5 (0.6) is recommended for most applications"
Actually your google search is not entirely off. You do need access to a canvas object, or at least a handle to a DC object. In general when searching for help concerning VCL classes it often pays to search for delphi examples since these are more common.
Anyway to calculate the size of a string you could have a look at the TextExtent function, it is a function for the TCanvas class. Simply pass the character which width you want to test, and the return value will be a TSize construct. However there is also a TextWidth function, as well as a TextHeight function. You can use these as well. Actually these call the TextExtent internally.
You have to note one thing though, the functions use the current font of the TCanvas object, more specifically the font bound to the DC the canvas uses. So assign the font you wish to test with first, and then pass the character.
I have some old code that calculates the width of a string like this:
// This canvas could be the form canvas: canvas = Form1->Canvas or the
// memo canvas which will probably be what you want.
canvas->Font->Assign(fontToTest);
int textwidth = TextWidth(textToTest);
If you want more control of what to do, you can also do this using the Windows API, this is essentially what the VCL does for you, in that case the following example would look like this:
// This canvas could be the form canvas: canvas = Form1->Canvas
canvas->Font->Assign(fontToTest);
// The initial size, this is really important if we use wordwrapping. This is
// the text area of the memo control.
TRect rect = ClientRect;
// This is the font format we wish to calculate using, in this example our text
// will be left aligned, at the top of the rectangle.
fontformat = DT_LEFT | DT_TOP;
// Here we calculate the size of the text, both width and height are calculated
// and stored in the rect variable. Also note that we add the DT_CALCRECT to the
// fontformat variable, this makes DrawTextEx calculate the size of the text,
// without drawing it.
::DrawTextEx(canvas->handle,
textToTest.c_str(),
textToTest.Length(),
&rect,
fontformat | DT_CALCRECT,
NULL);
// The width is:
int width = rect.Width();
The fontformat is a parameter that specifies different options for how to align and layout the text, if you plan on drawing text it will be a good idea to check out the different possibilities it offers: DrawTextEx Function [1]
EDIT: Reading through your question again, it struck me that the function you might be searching for is: GetTextExtentExPoint Windows API documentation states the following about this function:
The GetTextExtentExPoint function
retrieves the number of characters in
a specified string that will fit
within a specified space and fills an
array with the text extent for each of
those characters. (A text extent is
the distance between the beginning of
the space and a character that will
fit in the space.) This information is
useful for word-wrapping calculations.
You can find more information about the GetTextExtentExPoint function here: GetTextExtentExPoint Function [2]
[1] http://msdn.microsoft.com/en-us/library/dd162499%28VS.85%29.aspx
[2] http://msdn.microsoft.com/en-us/library/dd144935%28VS.85%29.aspx
What you could do, if you have access to Win32 API functions, is create a RichEdit Window the same size as your TMemo window, place the text in the RichEdit window, send the EM_FORMATRANGE message to the window and from the result determine how many characters it will hold. Of course this method will work with multiple lines etc...
Please have a look at this screenshot
alt text http://www.maclife.com/files/u18/Yep3-big.jpg
I think these are the main features of such a 'tag panel':
1) Each tag on the panel is a standalone control and can be clicked
2) Auto line wrapping when there is not enough space to show the next tag in the current line.
3) Rounded corner rectangle border for each tag is a nice-to-have feature.
I want to implement the similar function in Delphi, Is there an existing control to do this? If not, what's the best way to implement such a control?
Thank you.
When you are on a recent Delphi version use a TFlowPanel and some appropriate controls for the tags. A simple TButton or a TLinkLabel should do for that.
Each clickable tag doesn't necessarily have to be its own control. It just has to be a region that you can detect being clicked.
Suppose you represent each area as a Windows region. You can figure out how wide each one should be based on its text with the TCanvas.TextExtent function. Then create a region with a function like CreateRectRgn. For rounded corners, try CreateRoundRectRgn instead. You can test for mouse events in each region with the PtInRegion function. You can paint borders around them with FrameRgn. The last obstacle is to draw them on the screen so they'll all fit. You're creating the regions and you know their widths, so assign tags to a row until you run out of space, and then start the next line.
There are two possible solutions to custom alignment in Delphi 7. You can make your own flowpanel by deriving from TCustomPanel and override the AlignControls( )-method, or you can set alignment to alCustom and handle the OnAlignPosition-event.
I guess I would have gone for the TCustomPanel-derivative option. TFlowPanel in form Delphi 2007 uses that option- I have to admit, though, that I have never tried either my self...