How can I know the line number of editfield of blackberry - blackberry

I would like to know the line number in EditField while I am typing some text in this component.

Get the width of the edit field. (int returned) (e.g. 320 px)
Get the font width of your current text. Font class have getAdvance() method that accepts the string parameter (int returned) (e.g. 650 px)
if string width > edit field width you can divide string width and edit width and on the other step get the modulo of it. (here you will get 2 as division and 10 will be yr modulo. That means you have filled two lines + in third line you are having characters that can be fit into 10 px.
I hope it will solve your problem, but be careful if you use any long words, the module had never been tested with long words but I fear about result's accuracy with long words.

Related

Calculate how many chararcters can fit on one line of a UILabel

I'm trying to stop text from wrapping when it doesnt fit in a line of a multi-line (numberOfLines = 0) label but to do that I need to know how many characters can fit in a line of the label so I can cut off the rest and just use "\n" to add the next string to the next line. How would I go about finding how many characters can fit on each line with my font and size and size of the label (Courier 17.0 monospaced by the way). Also the size of the label can be dynamic since like screen size/size classes can change, which is why this is needed.
Ex of what I want to happen and why I need to know how many characters fit
on a line:
var lineOne = "Too long"
var lineTwo = "Also too long"
myLabel.text = lineOne + "\n" + lineTwo
// Then I would calculate how many characters can fit on a line and alter the two strings accordingly
Output of the label (pretend the label character limit happens to be like 3):
Too
Als
I Just realized another thing I need to be able to calculate is how many lines can fit in a label (height) if the numberOfLines = 0.
I don't think you need to do them programmatically..
To do that, Select your label and in the Attributes inspector, change the
Line Break to Character Wrap.
Then change Lines to the maximum amount of lines you want to allow.
And you can add constraints for height and width accordingly. Then use <labelOutlet>.sizeToFit();

Word Wrap text in Argox OS214 - PPLA

I use PPLA language to print labels directly to an Argox OS-214.
How can I word wrap long texts?
Do I have to make the calculations or is there any function that does that for me?
If I have to calculate, do you hav any tips on how to do it?
In the PPLA language manual there are no functions to word wrap long text lines, so you have to calculate yourself where to insert the line breaks before sending the commands to the printer.
How to do it depends by the programming language that you are using; for example in the PHP language there is a wordwrap function that can do it for you (the C source code of the wordwrap function is here, at line 803).
A simple description of a word wrapping algorithm can be found on the wikipedia:
SpaceLeft := LineWidth
for each Word in Text
if (Width(Word) + SpaceWidth) > SpaceLeft
insert line break before Word in Text
SpaceLeft := LineWidth - Width(Word)
else
SpaceLeft := SpaceLeft - (Width(Word) + SpaceWidth)
First of all, you have to split the text in words, but I suppose that this is not a problem for you, so I won't elaborate further.
LineWidth should be calculated as the label width minus the left and right margins width.
Assuming that for your text you're using a single fixed-width (non proportional) font, you can convert the LineWidth to a number of characters by simply dividing it by the character width (or by simply printing a long line of text and then counting the number of characters that gets printed); this way each word length corresponds to the number of its characters.
Otherwise, if you're using a proportional font, calculating each word width is more difficult, unless you know each character width.
An idea could be using an image with the same pixel size of the label, just to simulate the printing and for calculating where to insert the line breaks, but this requires to have a function to calculate the pixel width of each word (for example in PHP there is the imagettfbbox function), and also to use exactly the same font on the printer and the PC.
Be aware that you could also have to handle some words that are longer than the label width; in this case you must simply split the word at LineWidth and continue on the next line.

How to set the width of a textbox, depending on a specific format string?

I have a TextBox that uses a specific format string to display its content. The width of the box is set to take exactly the amount of space that is needed to display the text. For this purpose I use the width that the formatted text would have:
box.width = new FormattedText(
String.Format(Format, value),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Arial"),
8,
Brushes.Black).Width;
When doing this the textbox almost has the right width. But I still need to add an offset of 12, otherwise the area of the text would be cropped by some pixels:
I got the number 12 by try-and-error. So two questions arise:
Why do I need this offset, were does the lost width go to (whitespace and border?)?
Is there a better approach to get the right offset than to just guess it?

Calculate characters that fit a fixed rect TextView

I have seen here people needing to calculate the size of the NSString given a size but I need to do the opposite.
Given a specified rect (or fixed UITextView, or multiline UILabel, no scrolling) I need to know:
if it managed to show all the chars of my NSString
if not, what is the last char shown
So that I can display the remaining text in another UITextView (of course if I could use a single UITextView I would not have this problem).
At first it seems a simple thing to do, but actually I am not finding a way, intuitively I think I could use either UITextView's:
textView.contentSize.height;
or NSString's:
sizeWithFont:constrainedToSize:lineBreakMode:
or a combination of the two, but I need to be precise and those methods do not help me in telling what is the last character that managed to fit the visible area of the UITextView.
Not sure if this is actually possible, but is a requirement of my client who thinks programming iOS is like printing a newspaper and expects to be able to format text around an image....
You could maybe get the maximum height of one line of text from a one character long string.
If you use that with sizeWithFont:constrainedToSize:lineBreakMode: then you should be able to know if your text runs onto more than one line (if the cgsize height is greater than the height of one line of text).
In order to find out the last character (or word) you would have to loop around the length of the string adding characters (or words) as you go and checking for when the cgsize height increases to add a new line, this will give you the character point when to split into multiple strings ( for multiple fields/labels/textviews ) or when to insert line breaks into the text ( if using a single multi-line textview or label ).
I hope you find an easier way...

How to get font size in delphi

I am looking for a function which should take parameters as Font name, sample character, width, height of the sample character and should return Font Size.
It must look like this:
GetFontSize(<Font Name>, <Sample Character>, <Sample Character Width>,
<Sample Character Height>)
which must return the font size,
Is this possible in delphi?
You may want to take a look at this page that discusses font size and points.
It uses the following to convert between the points and pixel size:
Arial 12pt at 96 dpi:
font size in points 12
font size in pixels = ------------------- × 96 dpi = ---- × 96 = 16 pixels
72 points per inch 72
You could use that for your conversion. You just need to be aware of possible differences in your screen dpi as Blorgbeard stated. Points are usually used to describe the size on paper. You need to decide how you want to map that to the screen. For example many programs will allow you to set a zoom level. That does not change the printed size on paper but does affect the pixel height on the screen.
Depending on what you are trying to accomplish you may need to get the actual sizes of the font. If you are trying to find out exactly how the font is put together take a look at Obtaining Font Metrics The code on that page uses the .Net libraries but it has a good explanation. If you want to get the Text Metrics in Delphi you would use the GetTextMetrics function.
As Rob stated in his comment you can draw text at a specific height by setting the Font.Size property to the negative height in pixels. Windows will then draw the text out at that height. In that case you don't need a specific letter or the width as you have in your prototype function. For a given font size each letter will have a different height and width. I.E. capital letters will be taller than lower case letters and letters like "W" will be wider than letters like "i".
I'm only aware of methods to do the opposite - that is, to get pixel sizes from point sizes.
The only way I can think of is to set up a TCanvas, and repeatedly call GetTextExtent on it, changing the font size each time, until you get a size that's acceptably close to what you want.
Depending on how accurate you need this, you could just convert your desired height in pixels to points - a point is 1/72 of an inch, and your screen is probably 96 pixels per inch (but check GetDeviceCaps to get the real figure). See this question.
That would get you a font size pretty close to the pixel size you want.

Resources