Paint.measureText is not accurate with Arabic text. Here is what I tried:
I have a TextView tv1.
tv1.getWidth(); gives 480, which is okay. tv1.getPaint().measureText("المؤلف : عبدالرحمن ابن أبي بكر أبو الفضل السيوطي"); gives 502, which is bigger than the screen width.
Bad result, since the text is taking about 80% of the line width! The result should be around 400.
Thank you.
i faced this problem today . everything is working as expected and you should know that when your text contains Arabic vowels like ّ ِ ُ َ chars , they are all being calculated in the width as seperate chars and android Paint.measureText is not aware that it shouldnt take those chars into account , because they wont add extra width and they will be drawn on top and bottom of other chars.
the solution is : you should first remove those chars from text and then measure your text.
Related
I have a textview where I want to show text in vertical (just like rotated 90º). With android:rotation="-90" you can get this.
If the text would have no rotation, and you would like to fit a sentence, you would first look at the width of the textview, since text will try to be in 1 line. Since I rotate the text, here I expect to see the height of the object. However, Android is still considering the width. I need to fix the size of the width, but this causes me a problem for the text: Although the textview has a big vertical dimension and text would fit, Android thinks it needs to use the width to calculate the available space. At the end this causes me overflow of the text, appearing for example, just one word as multiple lines of 1 char.
How can I make android to understand that the space for the lenght to fit the text is now the vertical, instead of the horizontal?
I want to calculate the ems value according to the string or text length count If text length increase it should occupy it's width size according to the characters count without cut-off text in a single line, thanks
Tried to keep string length as ems but it gives very long width size with empty spaces, Thanks in advance
Using monospaced fonts you could count the em-width of a string. Since letters like i and m do in other cases not have the same length, the pixel/em/dp count cannot be predicted.
If someone would write a sophisticated logic for the widths of letters, count them, then multiply count with per-letter-length, this solution would still be too inaccurate without even more refinements: Ligatures and kerning make the actual width of a word even more complex. For example, A and V can overlap a little, while a capital A following another A or the letter V following another V are same width at at base or top, respectively. Therefore their centers have to be moved away from each other so as to not overlap.
I would like to draw vertical lines between Numbers/Letters in my TextView. So it should look similar to
A|B|C|D
without the use of the | character and use drawLine() instead.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example. However the lines dont line up as they should.
Not sure whats not working, help appreciated.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example.
That's your problem. For starters, you haven't specified that you're using a mono-spaced font. If you're not, then the letters won't be evenly distributed. Even if you are using a mono-spaced font, likely the padding at the beginning (and possibly end) of the TextView are going to offset things. I can't remember exactly how TextView measures things, but I suspect looking at actual left padding value would be a good start to find the left padding. If you want to use this with a variable width font, you'll want to use something like Paint.measureText to measure the width of the characters.
Once you have all that, you can add the width of the character(s) to the left padding to find the position to place each line.
I am working on Android textview,case is i have a specific height of textview,i want to adjust the text in the given textview height by adjusting the font size.If a text is of 10 charachers it will covers the whole height and if text is off 400 characters it will decreases the font to adjust in height of textview. I have tried auto text resize class but it does not getting the desire result.
You are looking for FontFit TextView. Check out here.
You just have to use it instaed of normal TextView like this..
TextView tv = new FontFitTextView(context);
Hope this helps
You can also write your own logic for that like
1- : Count no or character in side text watcher
2- : if no of character more then 10 then reduce the size of test using textsize method.
3- : the same logic if character more then 10 then reduce the size of test using textsize method.
or you can also put it on loop then i think you can achieve it also.
I want to create a text reader, which will cut the text and put it in the TextView (represents my pages) and one after the other. These are contained in a ViewFlipper (represents my entire text), which allows me to turn the pages.
By cons I can not cut my text intelligently. For now I limit each TextView to a number of characters, but it is very unstable. Indeed some letters take up more space than others .. We can not use this method.
How can we know what quantity of text will enter into a TextView?
I tried to find the maximum size of my View manually .
I invoked in:
float test = view.getPaint().measureText(myString);
It works well. When I put 480 letters (0 and 1), I get to 14,400 and my text is entirely within the screen. With:
1 -> 12.0
0 -> 12.0
é -> 11.0
\n -> 13.0
\r -> 7.0
total to fill the page -> 14400
The problem is that the line breaks and carriage return are causing problems.
If I have only letters or numbers in my string, so good: if we respect the maximum size of 14,400, everything is displayed on the screen. No Problem!
By cons, if a line breaks or a carriage return, it does not work. Even if we we respect the limit of 14400, the text above and is not fully displayed.
This is because the "MeasureText" method;" only returns 13 for a line break. But it's weird, a line break should account for at least 480 to make it consistent.
How do publishers eBook (Moon Reader, + Aldiko) for the text to be formatted so good? Each page has the same height, the text ends in the same place ..
You can fix the number of lines in your TextView by adding android:maxLines="some_integer". The other way is this:
if (myTextView.getMeasuredWidth() < myTextView.getPaint().measureText(myText)) {
myTextView.setHorizontalFadingEdgeEnabled(true);
myTextView.setHorizontallyScrolling(true);
}
But this will add cloudy effect to the last character if the text is too long.
Before get measured width or height call below method :
anyview.measure(0,0);
without calling this method you all wage getting 0 value for height and width