Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.graphics.Paint;
- import android.widget.TextView;
- import java.util.Arrays;
- /**
- * Helpers for dealing with custom typefaces and measuring text to display
- */
- public class TypefaceUtils {
- /**
- * Find the maximum number of digits in the given numbers
- *
- * @param numbers
- * @return max digits
- */
- public static int getMaxDigits(int... numbers) {
- int max = 1;
- for (int number : numbers) {
- max = Math.max(max, (int) Math.log10(number) + 1);
- }
- return max;
- }
- /**
- * Get width of number of digits
- *
- * @param view
- * @param numberOfDigits
- * @return number width
- */
- public static int getWidth(TextView view, int numberOfDigits) {
- Paint paint = new Paint();
- paint.setTypeface(view.getTypeface());
- paint.setTextSize(view.getTextSize());
- char[] text = new char[numberOfDigits];
- Arrays.fill(text, '0');
- return Math.round(paint.measureText(text, 0, text.length));
- }
- }
Add Comment
Please, Sign In to add comment