Make text fit exactly as the screen size

不打扰是莪最后的温柔 提交于 2019-12-05 18:23:06

Ok here is a shot - and a rather clumsy one but in short:

*You need need to know if any give line of text will fit width-wise in your view *You need to know how many lines you have *You need to handle embedded newlines

so

will some text fit on any given line

  private boolean isTooLarge (TextView text, String newText) {    
     float textWidth = text.getPaint().measureText(newText);
     return (textWidth >= text.getMeasuredWidth ());
  } 

how many lines does your textview have:

numLinesPerPage=mTextView.getHeight()/mTextView.getLineHeight();  //not this doesn't handle special cases where you've changed the font, bolded it etc

With these two tools you could iterate through your text adding words keeping track of how many lines you have left to work with (and handle your newlines if your text contains them)

note: getHeight can't be called in constructor since it doesn't know the height yet - you could also do this in onDraw or maybe onMeasure if you have a custom control.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!