support_ms
support_ms

Reputation: 1933

Height of text in TextView where there are two fonts?[android]

I can not find a solution.Text in my textview has two fonts: line spacing`is different.

Question is: if Textview contains text which has two type of linespacing , how can I get the height of each text?

I used many samples , but I could not reach good result, methods of Paint class could not solve my problem.

Any help please

Upvotes: 1

Views: 167

Answers (2)

support_ms
support_ms

Reputation: 1933

There is no need to find out each text's height.

public static int getHeight(Context context, CharSequence text, int textSize, int deviceWidth, Typeface typeface,int padding) {
            TextView textView = new TextView(context);
            textView.setPadding(padding,0,padding,padding);
            textView.setTypeface(typeface);
            textView.setText(text, TextView.BufferType.SPANNABLE);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
            int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
            int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            textView.measure(widthMeasureSpec, heightMeasureSpec);
            return textView.getMeasuredHeight();
        }

Upvotes: 1

Shivang Trivedi
Shivang Trivedi

Reputation: 2182

hope this will help u:

     private SpannableStringBuilder setSpanColor(final String str, final String text) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(str);

    if (str.contains(text)) {
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setTextSize(textSize);

            }
        }, str.indexOf(text), str.indexOf(text) + text.length(), 0);
    }
    return ssb;

}

   <string name="confirm_msg">PLEASE  %1$s second String</string>
    txt.setText(setSpansize(String.format(getString(R.string.confirm_msg), "$" ),
            String.valueOf("$" )));

Upvotes: 1

Related Questions