tags:

views:

18

answers:

1

I need to draw text to a canvas (of a custom view), and need to first trim it to a maximum width, adding an ellipsis at the end if necessary. I see you can do it for a TextView, but I want to do it inside a custom view's onDraw() without having to add a child view.

Is this possible? I know I could measure the string, chop off a character, measure again, etc until it is the right size....and I'm sure there are more efficient ways as well...but I'd like to avoid reinventing that wheel if I can.

A: 

Take a look at

[TextUtils.ellipsize()][1]

I think it's exactly what you want. Basically you just tell it the amount of space available and using the other state information it will create the correct text for you :)

[1]: http://developer.android.com/reference/android/text/TextUtils.html#ellipsize(java.lang.CharSequence, android.text.TextPaint, float, android.text.TextUtils.TruncateAt, boolean, android.text.TextUtils.EllipsizeCallback)

Greg
cool, that did it thanks
rob