Android根据所设置的textSize获取字体高度和设置行高
来源:互联网 发布:文化部网络歌曲黑名单 编辑:程序博客网 时间:2024/06/06 07:27
From:http://qiaoweishu.iteye.com/blog/780526
网络中好多资料介绍获取字体高度的方法如下:
- public int getFontHeight(float fontSize)
- {
- Paint paint = new Paint();
- paint.setTextSize(fontSize);
- FontMetrics fm = paint.getFontMetrics();
- return (int) Math.ceil(fm.descent - fm.top) + 2;
- }
个人更倾向于以下方式获取字体实际高度:
- Math.ceil(fm.descent - fm.ascent)
通过实际的截图对文字高度的确定,后者更准确一些。
有了字体高度信息,就可以添加行与行之间的空隙,调整行高。
个人实现方式如下:
- paint.setTextSize(fFontWidth);
- FontMetrics fm = paint.getFontMetrics();
- fFontHeight = (float)Math.ceil(fm.descent - fm.ascent);
- if(fFontHeight > fLineHeight)
- {
- fMulValue = fLineHeight / fFontHeight;
- fAddValue = -1;
- }
- else
- {
- fMulValue = 1;
- fAddValue = fLineHeight - fFontHeight;
- }
- textViewLeft.setTextSize(fFontWidth);
- textViewLeft.setLineSpacing(fAddValue, fMulValue);
实践验证这种方式对多种分辨率的屏幕的适应性较强。
以上是个人在实际的功能实现过程中的总结,水平较浅,若看官有不同的看法或者见解,望不惜赐教!
0 0
- Android根据所设置的textSize获取字体高度和设置行高
- Android获取字体高度和设置行高
- Android获取字体高度和设置行高
- Android获取字体高度和设置行高
- [Android]TextView设置字体大小时应该知道的事-同样的textSize不同的效果
- textSize 设置
- Android 获取屏幕宽高 动态设置组件高度
- ]TextView设置字体大小时应该知道的事-同样的textSize不同的效果
- android根据字体大小设置控件高度
- 【android】根据屏幕分辨率设置底栏高度
- android 字符串 转换成Bitmap 设置文本的字体: 宋体,隶书啥的,获取字体的 行高, 文字生成图片
- Android 根据屏幕大小设置字体
- Android 根据屏幕大小设置字体
- Android 根据屏幕大小设置字体
- Android 根据屏幕大小设置字体
- Android 根据屏幕大小设置字体
- 设置&获取ActionBar的高度
- android 字体的设置
- VC(VISUAL_C++)虚拟键VK值列表
- simple-spring-memcached简介
- memcached搭建缓存系统
- 软件版本管理原则
- LRU Cache
- Android根据所设置的textSize获取字体高度和设置行高
- Python 获取新浪微博的最新公共微博
- 简单开发过程介绍(C语言)
- "error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
- Apache Ant 简介
- httphelp
- picture
- centos 编译安装 heartbeat
- hdu2099