detect clipping in android TextView

前端 未结 3 1602
天命终不由人
天命终不由人 2021-02-09 04:12

I have a TextView in my android application that has a set width on it. It\'s currently got a gravity of \"center_horitonzal\" and a set textSize (9sp). I pull values to put o

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-09 05:00

    I found a way to measure the width of text using the TextView's Paint object, and lower it until it fit in the size I needed. Here's some sample code:

        float size = label.getPaint().measureText(item.getTitle());
        while (size > 62) {
            float newSize = label.getTextSize() - 0.5f;
            label.setTextSize(newSize);
            size = label.getPaint().measureText(item.getTitle());
        }
    

提交回复
热议问题