Reputation: 5196
I try to display some text on a PDF document
. This text might be long or short but should be on a single line. This is why I opt for the TextViewCompat.setAutoSizeTextTypeWithDefaults
utility method but it doesn't seem to work when drawing on a canvas
. The text is cropped instead of being reduced.
Here is my code:
PageInfo startingPageInfo = new Builder(STANDARD_PDF_PAGE_WIDTH, STANDARD_PDF_PAGE_HEIGHT, 1).create();
final Page startingPage = document.startPage(startingPageInfo);
Canvas canvas = startingPage.getCanvas();
canvas.save();
final TextView documentTitle = new TextView(this);
TextViewCompat.setAutoSizeTextTypeWithDefaults(documentTitle, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
documentTitle.setLines(1);
documentTitle.setText(String.format("%s - %s", category.getName(), period));
documentTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 56);
documentTitle.setTypeface(null, BOLD);
documentTitle.setTextColor(Color.BLACK);
int measuredWidth = MeasureSpec.makeMeasureSpec(canvas.getWidth() - 100, MeasureSpec.EXACTLY);
int measuredHeight = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);
documentTitle.measure(measuredWidth, measuredHeight);
documentTitle.layout(0, 0, measuredWidth, measuredHeight);
canvas.translate(titleDx, titleDy);
documentTitle.draw(canvas);
I'm not comfortable with custom views inside Android so I'm maybe doing something wrong here, any help would be appreciated.
Upvotes: 6
Views: 364
Reputation: 62831
Rewrite2+
The trouble you are having is caused by the following line:
documentTitle.layout(0, 0, measuredWidth, measuredHeight);
This line should be:
documentTitle.layout(0, 0, width, height);
Your code is setting the width and height to some astronomical values, so the text always "fits."
This is a short demo of (mostly) your code working. I used a custom LinearLayout
but that won't matter. I think just the change above should work by itself, but I may have made some other little tweaks that you may find in the code below.
MyLinearLayout.java
public class MyLinearLayout extends LinearLayout {
private AppCompatTextView documentTitle;
public MyLinearLayout(@NonNull Context context) {
super(context);
init();
}
public MyLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public MyLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
documentTitle = new AppCompatTextView(this.getContext());
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
String displayString = String.format("%s - %s", "Some very, very, ", "very, very long text");
int width = getWidth() - getPaddingEnd();
int height = 100;
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width, height);
documentTitle.setLayoutParams(lp);
TextViewCompat.setAutoSizeTextTypeWithDefaults(documentTitle, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
documentTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 56);
documentTitle.setTypeface(null, BOLD);
documentTitle.setTextColor(Color.BLACK);
documentTitle.setMaxLines(1);
int measuredWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
int measuredHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
documentTitle.measure(measuredWidth, measuredHeight);
// Arguments here are width and height, not measureWidth and measeredHeight.
documentTitle.layout(0, 0, width, height);
canvas.save();
for (int i = 0; i < 5; i++) {
documentTitle.setText(displayString);
documentTitle.draw(canvas);
displayString += " - longer - longer";
canvas.translate(0, 150);
}
canvas.restore();
}
}
Upvotes: 4