Algorithm for estimating text width based on contents

前端 未结 8 2145
傲寒
傲寒 2021-02-08 09:11

This is a long shot, but does anyone know of an algorithm for estimating and categorising text width (for a variable width font) based on its contents?

For example, I\'d

8条回答
  •  青春惊慌失措
    2021-02-08 09:48

    This worked for me:

    AffineTransform af = new AffineTransform();     
    FontRenderContext fr = new FontRenderContext(af,true,true);     
    Font f = new Font("Arial", 0, 10); // use exact font
    double width= f.getStringBounds("my string", fr).getWidth();      
    

提交回复
热议问题