Italic TextView with wrap_contents seems to clip the text at right edge

前端 未结 13 1263
孤独总比滥情好
孤独总比滥情好 2020-12-24 10:22


        
相关标签:
13条回答
  • 2020-12-24 11:07

    Found another solution (tested on 4.1.2 and 4.3 while using wrap_content). If you extend TextView or EditText class you can override onMeasure method this way:

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        final int measuredWidth = getMeasuredWidth();
        final float tenPercentOfMeasuredWidth = measuredWidth * 0.1f;
        final int newWidth = measuredWidth + (int) tenPercentOfMeasuredWidth;
    
        setMeasuredDimension(newWidth, getMeasuredHeight());
    }
    
    0 讨论(0)
  • 2020-12-24 11:08

    Looking at the source I found out that setting a shadow extends the clip rectangle.

    A trick is to set an invisible shadow just beyond the character.

    For example:

    android:shadowRadius="2"
    android:shadowDx="2"
    android:shadowColor="#00000000"
    

    I think this solution is better as it will not extend the width of the TextView which may happen when adding an extra character (which is more apparent with a background).

    0 讨论(0)
  • 2020-12-24 11:14

    android:layout_width="wrap_content" , gives you a rectangle for wrapped content rendering. All will work well for normal text (non-italic).

    Once you have italic text enabled, the wrapped text will try to fit into the rectangle and hence the rightmost character will be cut off unless its un-cut-able (such as ., ), 1, etc.)

    Solution as suggested is to have a space at the end of the text (or anything better ??)

    PS: This applies to android:gravity="right" too because the text will be pushed to the rightmost. If we have italic text, we face the same problem.

    0 讨论(0)
  • 2020-12-24 11:14

    You can add a HAIR SPACE to your string

    <string name="hair_space">&#x200A;</string>
    
    String hairSpace = getContext().getString(R.string.hair_space);
    textView.setText(hairSpace + originalString + hairSpace)
    
    0 讨论(0)
  • 2020-12-24 11:14

    Add a 3dp padding to the right on your TextView. I tried with 1dp and 2dp, but 3dp seemed to do the trick fully.

    android:paddingRight="3dp"

    0 讨论(0)
  • 2020-12-24 11:19

    This is my solution: Format textview and measure. After that set width of textview with 1 pixel add to the width measured.

    TextView textView = new TextView(this);
    textView.setText("Text blah blah");
    textView.setTypeface(typeface, Typeface.BOLD_ITALIC)
    textView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    textView.setLayoutParams(new LayoutParams(textView.getMeasuredWidth() + 1, 
                                                 LayoutParams.WRAP_CONTENT));
    

    Working for me. Hope these help.

    0 讨论(0)
提交回复
热议问题