【问题标题】:Height of text in TextView where there are two fonts?[android]TextView中有两种字体的文本高度?[android]
【发布时间】:2013-10-25 10:01:47
【问题描述】:

我找不到解决方案。我的 textview 中的文本有两种字体:行间距不同。

问题是:如果 Textview 包含具有两种行距的文本,我如何获取每个文本的高度?

我使用了很多示例,但我无法达到良好的效果,Paint 类的方法无法解决我的问题。

请帮忙

【问题讨论】:

    标签: android fonts textview


    【解决方案1】:

    希望这对你有帮助:

         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("$" )));
    

    【讨论】:

    • 每种字体的行高不同。我需要获取每个文本的高度
    【解决方案2】:

    没有必要找出每个文本的高度。

    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();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 2011-04-05
      • 2014-10-21
      相关资源
      最近更新 更多