获取 TextView 中文本的大小 [英] Get the size of a text in TextView

查看:24
本文介绍了获取 TextView 中文本的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 textView 放置在指定中心的 x 和 y 坐标时遇到问题.首先,我尝试在 textView 中设置文本,并以视图的宽度和高度移动视图从这个链接.

I have a problem placing a textView at specified center's x and y coordinates. Firstly, I tried to set the text in the textView, and to move the view with the width and the height of the view from this link.

但它不起作用,我想尝试其他方法.我想知道是否有一种方法可以获取指定文本在我的 textView 中的大小?我的意思是,我知道 text 和 textSize,如何获得 textView 的宽度和高度?

But it doesn't work, and I'd like to try something else. I'd like to know if there is a method to get the size which a specified text will take in my textView? I mean, I know the text and the textSize, how can I get the width and the height my textView will take?

类似于 (NSString)sizeWithFont; 的方法,适合那些了解 iPhone 开发的人.

Something like the method (NSString)sizeWithFont; for those who know iPhone dev.

推荐答案

如果你的textview叫做tv

If your textview is called tv

tv.setText("bla");
tv.measure(0, 0);       //must call measure!
tv.getMeasuredHeight(); //get height
tv.getMeasuredWidth();  //get width

更多信息(更新):如何获取视图的宽度/高度

More on this (updated): How to get width/height of a View

这篇关于获取 TextView 中文本的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆