How to calculate string font width in pixels?

前端 未结 3 458
星月不相逢
星月不相逢 2021-01-04 09:41

I want to calculate string font width in pixels in android. I am having string array and I am appending all these array elements to form one string. so I ha

3条回答
  •  执笔经年
    2021-01-04 10:09

    Thanks Jake for the working solution, yet I found this to work for me too:

    someText = "Lorem Ipsum";
    TextView myTextView = (TextView) findViewById(R.id.myTextView);
    float w = myTextView.getPaint().measureText(someText);
    

    Getting the Paint directly from the TextView that I'm going to work with makes me feel that the anwser will be more accurate.

提交回复
热议问题