Text being cut off from front for a particular fon

2019-04-29 03:10发布

问题:

The first letter of each word on new line is cut off for us denealian cursive font.

see the picture this one is with padding.If I am not using any padding ,it will be like in pic 2

This is my code

<com.font.anything.writinghelper
    android:id="@+id/textView" 
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#000000"
    android:padding="20dip"
    android:textSize="60sp"
    android:text="japanese google donkey elephant ostrich"
    />

Here writing helper is a class extending textview just to underline the text

     protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    Rect r = mRect;
    int padding =55;
    Paint paint = mPaint;
    int count = getLineCount();
    for (int i = 0; i < count; i++) {
    int baseline = getLineBounds(i, r);
    canvas.drawLine(r.left - padding, baseline, r.right + padding,
            baseline, paint);
    }
    super.onDraw(canvas);
}

Can anyone help.?

Edit

Requested screenshot

Is There a way to put some extra space towards left side of the TextView ?

回答1:

Finally I made a work around for this..It's a font issue and I am not sure it can be fixed by any other way.So what I have done is ,first get the TextView Layout and get the end and start of each line and hence obtain string in each line.Now append space in front of each line .Here is the entire code.May help someone else.

    ArrayList<String> lines = new ArrayList<String>();
    ArrayList<String> newLines = new ArrayList<String>();
    String line="";
    String text = getText().toString();
    Layout layout = getLayout();
    int start=0;
    int end;

    for (int i=0; i<count; i++) {
        end = layout.getLineEnd(i);

        lines.add(text.substring(start,end));
        line = lines.get(i);
        start = end;
        String nwText = "";
        nwText = " "+ line+" ";
        newLines.add(nwText);
    }
    Paint mPaint = getPaint();
    int i = 0;
   // String[] textLines = Results.split("\\n+");
    //float textsize = getTextSize();

    for (String textLine : newLines)
    {
       // mPaint.setTextSize(textsize);
        int baseline = getLineBounds(i, r);
        canvas.drawText(textLine, 0, baseline, mPaint);
       i++;
    }


回答2:

I come out with a similar solution. This recursively add spaces to the beginning and end of line, and if while adding space the word go to the line below this solution fix itself errors. (The firsts 3 variables have class scope).

    row = 0;
    offset = 0;
    oldPos = -1;
    final String nonBreakingSpace = "\u00A0\u00A0";
    textV.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int pos;
            StringBuilder sb = new StringBuilder(textV.getText());

            pos = textV.getLayout().getLineStart(row);
            if (oldPos!=-1 && pos<oldPos)
            {
                sb.delete(oldPos, oldPos + nonBreakingSpace.length());
            }

            if (offset==0)
            {
                row++;
                offset = -1;
            }
            else
            {
                offset = 0;
            }
            final int lineCount = textV.getLayout().getLineCount();
            pos = textV.getLayout().getLineStart(row);
            oldPos = pos+offset;
            if (row>=lineCount)
            {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    removeLayoutListenerPre16(textV.getViewTreeObserver(), this);
                } else {
                    removeLayoutListenerPost16(textV.getViewTreeObserver(), this);
                }
                sb.insert(0, nonBreakingSpace);
                sb.insert(sb.length(), nonBreakingSpace);
            }
            else
            {
                sb.insert(pos+offset, nonBreakingSpace);                    
            }
            textV.setText(sb);
        }
    }); 

and:

@SuppressWarnings("deprecation")
private void removeLayoutListenerPre16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
    observer.removeGlobalOnLayoutListener(listener);
}

@TargetApi(16)
private void removeLayoutListenerPost16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
    observer.removeOnGlobalLayoutListener(listener);
}