I have a question about one android text rendering detail:
Whether current character rendered width depends from previos character in android text renderer (for example in textview)?
For example if char 'x' have width equals x_w and 'y' - y_w, will string "xy" always have with equals
x_w + y_w
?
Some more details:
The point is that I should measure strings as quiсk as possible. I want to measure once each letter (character) from text using Paint.getTextWidths(...) and then jsut use work with characters width.
Depends on the font. It's not something I would assume, if you want the width of a string, measure the full string.
If you absolutely need that, use a monospace font.
Related
I have a seemingly simple task in Android, which turned out to be quite complex.
I have multiple strings, and I want each string to be centered based on a character in a specific position in that string. For example "azcde" should be centered on a character in index 1 ("z" in this case), "trxdsf" should be centered on a character in index 2 ("x" in this case), ...
Below is an example.
The problem is that characters vary in size (e.g. "i" is much thinner than "m")...
Can this be done in TextView or custom View?
Is there some way to use StaticLayout without doing automatic word wrapping? If the text includes newlines I want them to break up the paragraphs, but I don't want it wrap at a given width. It's not giving sane results, especially with Center/Right aligned text.
Is there a way to simply turn off the auto-wrapping on StaticLayout?
More details. I use getDesiredWidth to calculate a width and then use this as the wrap width for a new StaticLayout. This prevents auto-wrapping, but it also seems to turn off explicit wrapping as. GetLineCount returns 1 for text that actually has multiple-lines.
For example textview with size 33.0 dp in xml. It means double number or it has other features?
I assume you're referring to either layout_width or layout_height; in this case these numbers are always floating point. The ".0" on the end has no particular significance.
See http://developer.android.com/reference/android/R.attr.html#layout_width for more information.
I am trying to make a sign (the ones that drivers use at the airport to find someone "Mr Smith") and I wanted the sign to have the largest Font size possible, (its for a tablet), I could write a function to change the size depending on the length of the Text but is there a way of doing natively/better?
Thanks
The most flexible way is to create a custom view that draws the text in onDraw(). When drawing the text (with one of Canvas.drawText()) you will have to provide a Paint, and that would let you know the precise length of text, not just length of string (see Paint.measureText()).
This way you would have plenty of ways to calculate and redistribute the space (and it would totally rely on you how).
One way I can think of is calculating the length for a huge font size and then using the h/w ratio to see if I want to fill the screen in width or height.
In my app I am displaying a table where the individual cells are editable text fields (i.e. EditText objects). I am currently struggling calculating appropriate widths for these.
If I set the width to be as many "Ems" as the displayed text has characters, my fields are much to wide by about a factor of 2 and the table looks ugly and wastes lots of precious screen space. Setting the width in "Ems" is essentially like assuming the worst case, i.e. the width of a string under the assumption that it contains only the broadest characters in a font, usually 'M' (hence the name of the method), 'm', 'W', or '_'). But on average strings contain narrower characters and so most of these fields are half empty and much too wide for their actual content. I am thus desperately seeking a way to calculate a better fitting width, not that worst case width.
The "normal" way to do this in Java (in AWT or Swing at least) is to asks a widget's current Font (actually a Font's FontMetrics), to calculate and return the width that will be necessary to display a given string in pixels. But how does one obtain a TextView's Font? I haven't found any method to obtain a (Text)View's font and/or calculate a more appropriate width given the actual content of a cell's text string. How does one do that in Android?
Michael
I just found the "missing link": <TextView>.getPaint() is the answer!
The "Paint" then has methods like <Paint>.measureText(...) to calculate a string's width and <Paint>.ascent() and <Paint>.descent() to calculate a string's height.
Michael