Java Code Examples for android.text.TextUtils#ellipsize()

The following examples show how to use android.text.TextUtils#ellipsize() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private CharSequence getTruncatedText(int availWidth) {
    final TextPaint paint = getPaint();

    // Calculate the width the template takes.
    final String emptyTemplate = String.format(mTemplate, "");
    final float emptyTemplateWidth = paint.measureText(emptyTemplate);

    // Calculate the available width for the content.
    final float contentWidth = Math.max(availWidth - emptyTemplateWidth, 0.f);

    // Ellipsize the content to the available width.
    CharSequence clipped = TextUtils.ellipsize(mContent, paint, contentWidth, TruncateAt.END);

    // Build the full string, which should fit within availWidth.
    return String.format(mTemplate, clipped);
}
 
Example 2
@Nullable
private static CharSequence getEllipsizedTextWithSettingScaleX(
        @Nullable final CharSequence text, final int maxWidth, @Nonnull final TextPaint paint) {
    if (text == null) {
        return null;
    }
    final float scaleX = getTextScaleX(text, maxWidth, paint);
    if (scaleX >= MIN_TEXT_XSCALE) {
        paint.setTextScaleX(scaleX);
        return text;
    }

    // <code>text</code> must be ellipsized with minimum text scale x.
    paint.setTextScaleX(MIN_TEXT_XSCALE);
    final boolean hasBoldStyle = hasStyleSpan(text, BOLD_SPAN);
    final boolean hasUnderlineStyle = hasStyleSpan(text, UNDERLINE_SPAN);
    // TextUtils.ellipsize erases any span object existed after ellipsized point.
    // We have to restore these spans afterward.
    final CharSequence ellipsizedText = TextUtils.ellipsize(
            text, paint, maxWidth, TextUtils.TruncateAt.MIDDLE);
    if (!hasBoldStyle && !hasUnderlineStyle) {
        return ellipsizedText;
    }
    final Spannable spannableText = (ellipsizedText instanceof Spannable)
            ? (Spannable)ellipsizedText : new SpannableString(ellipsizedText);
    if (hasBoldStyle) {
        addStyleSpan(spannableText, BOLD_SPAN);
    }
    if (hasUnderlineStyle) {
        addStyleSpan(spannableText, UNDERLINE_SPAN);
    }
    return spannableText;
}
 
Example 3
private boolean createLayout(int width) {
    if (text != null) {
        try {
            if (leftDrawable != null) {
                width -= leftDrawable.getIntrinsicWidth();
                width -= drawablePadding;
            }
            if (rightDrawable != null) {
                width -= rightDrawable.getIntrinsicWidth();
                width -= drawablePadding;
            }
            CharSequence string = TextUtils.ellipsize(text, textPaint, width, TextUtils.TruncateAt.END);
            /*if (layout != null && TextUtils.equals(layout.getText(), string)) {
                calcOffset(width);
                return false;
            }*/
            layout = new StaticLayout(string, 0, string.length(), textPaint, width + AndroidUtilities.dp(8), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            calcOffset(width);
        } catch (Exception ignore) {

        }
    } else {
        layout = null;
        textWidth = 0;
        textHeight = 0;
    }
    invalidate();
    return true;
}
 
Example 4
private CharSequence ellipsizeText(final CharSequence text,final TextPaint paint,final float maxWidth)
{
paint.setTextSize(mChipFontSize);
if(maxWidth<=0&&Log.isLoggable(TAG,Log.DEBUG))
  Log.d(TAG,"Max width is negative: "+maxWidth);
final CharSequence ellipsize=TextUtils.ellipsize(text,paint,maxWidth,TextUtils.TruncateAt.END);
return ellipsize;
}
 
Example 5
private boolean createLayout(int width) {
    if (text != null) {
        try {
            if (leftDrawable != null) {
                width -= leftDrawable.getIntrinsicWidth();
                width -= drawablePadding;
            }
            if (rightDrawable != null) {
                int dw = (int) (rightDrawable.getIntrinsicWidth() * rightDrawableScale);
                width -= dw;
                width -= drawablePadding;
            }
            CharSequence string;
            if (scrollNonFitText) {
                string = text;
            } else {
                string = TextUtils.ellipsize(text, textPaint, width, TextUtils.TruncateAt.END);
            }
            /*if (layout != null && TextUtils.equals(layout.getText(), string)) {
                calcOffset(width);
                return false;
            }*/
            layout = new StaticLayout(string, 0, string.length(), textPaint, scrollNonFitText ? AndroidUtilities.dp(2000) : width + AndroidUtilities.dp(8), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            calcOffset(width);
        } catch (Exception ignore) {

        }
    } else {
        layout = null;
        textWidth = 0;
        textHeight = 0;
    }
    invalidate();
    return true;
}
 
Example 6
private boolean createLayout(int width) {
    if (text != null) {
        try {
            if (leftDrawable != null) {
                width -= leftDrawable.getIntrinsicWidth();
                width -= drawablePadding;
            }
            if (rightDrawable != null) {
                int dw = (int) (rightDrawable.getIntrinsicWidth() * rightDrawableScale);
                width -= dw;
                width -= drawablePadding;
            }
            CharSequence string;
            if (scrollNonFitText) {
                string = text;
            } else {
                string = TextUtils.ellipsize(text, textPaint, width, TextUtils.TruncateAt.END);
            }
            /*if (layout != null && TextUtils.equals(layout.getText(), string)) {
                calcOffset(width);
                return false;
            }*/
            layout = new StaticLayout(string, 0, string.length(), textPaint, scrollNonFitText ? AndroidUtilities.dp(2000) : width + AndroidUtilities.dp(8), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            calcOffset(width);
        } catch (Exception ignore) {

        }
    } else {
        layout = null;
        textWidth = 0;
        textHeight = 0;
    }
    invalidate();
    return true;
}
 
Example 7
Source Project: talk-android   File: RecipientEditTextView.java    License: MIT License 5 votes vote down vote up
private CharSequence ellipsizeText(CharSequence text, TextPaint paint, float maxWidth) {
    paint.setTextSize(mChipFontSize);
    if (maxWidth <= 0 && Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "Max width is negative: " + maxWidth);
    }
    return TextUtils.ellipsize(text, paint, maxWidth,
            TextUtils.TruncateAt.END);
}
 
Example 8
private CharSequence ellipsizeToWidth(CharSequence text) {
  return TextUtils.ellipsize(text,
                             getPaint(),
                             getWidth() - getPaddingLeft() - getPaddingRight(),
                             TruncateAt.END);
}
 
Example 9
/**
 * Deprecated use loadSafeLabel(PackageManager, float, int) instead
 *
 * @hide
 */
@SystemApi
public @NonNull CharSequence loadSafeLabel(@NonNull PackageManager pm) {
    // loadLabel() always returns non-null
    String label = loadUnsafeLabel(pm).toString();
    // strip HTML tags to avoid <br> and other tags overwriting original message
    String labelStr = Html.fromHtml(label).toString();

    // If the label contains new line characters it may push the UI
    // down to hide a part of it. Labels shouldn't have new line
    // characters, so just truncate at the first time one is seen.
    final int labelLength = Math.min(labelStr.length(), MAX_SAFE_LABEL_LENGTH);
    final StringBuffer sb = new StringBuffer(labelLength);
    int offset = 0;
    while (offset < labelLength) {
        final int codePoint = labelStr.codePointAt(offset);
        final int type = Character.getType(codePoint);
        if (type == Character.LINE_SEPARATOR
                || type == Character.CONTROL
                || type == Character.PARAGRAPH_SEPARATOR) {
            labelStr = labelStr.substring(0, offset);
            break;
        }
        // replace all non-break space to " " in order to be trimmed
        final int charCount = Character.charCount(codePoint);
        if (type == Character.SPACE_SEPARATOR) {
            sb.append(' ');
        } else {
            sb.append(labelStr.charAt(offset));
            if (charCount == 2) {
                sb.append(labelStr.charAt(offset + 1));
            }
        }
        offset += charCount;
    }

    labelStr = sb.toString().trim();
    if (labelStr.isEmpty()) {
        return packageName;
    }
    TextPaint paint = new TextPaint();
    paint.setTextSize(42);

    return TextUtils.ellipsize(labelStr, paint, MAX_LABEL_SIZE_PX,
            TextUtils.TruncateAt.END);
}
 
Example 10
public GroupCreateSpan(Context context, TLRPC.User user, ContactsController.Contact contact) {
    super(context);

    currentContact = contact;
    deleteDrawable = getResources().getDrawable(R.drawable.delete);
    textPaint.setTextSize(AndroidUtilities.dp(14));

    avatarDrawable = new AvatarDrawable();
    avatarDrawable.setTextSize(AndroidUtilities.dp(12));
    if (user != null) {
        avatarDrawable.setInfo(user);
        uid = user.id;
    } else {
        avatarDrawable.setInfo(0, contact.first_name, contact.last_name, false);
        uid = contact.contact_id;
        key = contact.key;
    }

    imageReceiver = new ImageReceiver();
    imageReceiver.setRoundRadius(AndroidUtilities.dp(16));
    imageReceiver.setParentView(this);
    imageReceiver.setImageCoords(0, 0, AndroidUtilities.dp(32), AndroidUtilities.dp(32));

    int maxNameWidth;
    if (AndroidUtilities.isTablet()) {
        maxNameWidth = AndroidUtilities.dp(530 - 32 - 18 - 57 * 2) / 2;
    } else {
        maxNameWidth = (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(32 + 18 + 57 * 2)) / 2;
    }
    String firstName;
    if (user != null) {
        firstName = UserObject.getFirstName(user);
    } else {
        if (!TextUtils.isEmpty(contact.first_name)) {
            firstName = contact.first_name;
        } else {
            firstName = contact.last_name;
        }
    }
    CharSequence name = TextUtils.ellipsize(firstName.replace('\n', ' '), textPaint, maxNameWidth, TextUtils.TruncateAt.END);
    nameLayout = new StaticLayout(name, textPaint, 1000, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    if (nameLayout.getLineCount() > 0) {
        textWidth = (int) Math.ceil(nameLayout.getLineWidth(0));
        textX = -nameLayout.getLineLeft(0);
    }

    TLRPC.FileLocation photo = null;
    if (user != null && user.photo != null) {
        photo = user.photo.photo_small;
    }
    imageReceiver.setImage(photo, null, "50_50", avatarDrawable, null, null, 0, null, 1);
    updateColors();
}
 
Example 11
Source Project: TokenAutoComplete   File: SpanUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Nullable
public static Spanned ellipsizeWithSpans(@Nullable CharSequence prefix, @Nullable CountSpan countSpan,
                                       int tokenCount, @NonNull TextPaint paint,
                                       @NonNull CharSequence originalText, float maxWidth) {

    float countWidth = 0;
    if (countSpan != null) {
        //Assume the largest possible number of items for measurement
        countSpan.setCount(tokenCount);
        countWidth = countSpan.getCountTextWidthForPaint(paint);
    }

    EllipsizeCallback ellipsizeCallback = new EllipsizeCallback();
    CharSequence tempEllipsized = TextUtils.ellipsize(originalText, paint, maxWidth - countWidth,
            TextUtils.TruncateAt.END, false, ellipsizeCallback);
    SpannableStringBuilder ellipsized = new SpannableStringBuilder(tempEllipsized);
    if (tempEllipsized instanceof Spanned) {
        TextUtils.copySpansFrom((Spanned)tempEllipsized, 0, tempEllipsized.length(), Object.class, ellipsized, 0);
    }

    if (prefix != null && prefix.length() > ellipsizeCallback.start) {
        //We ellipsized part of the prefix, so put it back
        ellipsized.replace(0, ellipsizeCallback.start, prefix);
        ellipsizeCallback.end = ellipsizeCallback.end + prefix.length() - ellipsizeCallback.start;
        ellipsizeCallback.start = prefix.length();
    }

    if (ellipsizeCallback.start != ellipsizeCallback.end) {

        if (countSpan != null) {
            int visibleCount = ellipsized.getSpans(0, ellipsized.length(), TokenCompleteTextView.TokenImageSpan.class).length;
            countSpan.setCount(tokenCount - visibleCount);
            ellipsized.replace(ellipsizeCallback.start, ellipsized.length(), countSpan.getCountText());
            ellipsized.setSpan(countSpan, ellipsizeCallback.start, ellipsized.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ellipsized;
    }
    //No ellipses necessary
    return null;
}
 
Example 12
public GroupCreateSpan(Context context, TLRPC.User user, ContactsController.Contact contact) {
    super(context);

    currentContact = contact;
    deleteDrawable = getResources().getDrawable(R.drawable.delete);
    textPaint.setTextSize(AndroidUtilities.dp(14));

    avatarDrawable = new AvatarDrawable();
    avatarDrawable.setTextSize(AndroidUtilities.dp(12));
    if (user != null) {
        avatarDrawable.setInfo(user);
        uid = user.id;
    } else {
        avatarDrawable.setInfo(0, contact.first_name, contact.last_name, false);
        uid = contact.contact_id;
        key = contact.key;
    }

    imageReceiver = new ImageReceiver();
    imageReceiver.setRoundRadius(AndroidUtilities.dp(16));
    imageReceiver.setParentView(this);
    imageReceiver.setImageCoords(0, 0, AndroidUtilities.dp(32), AndroidUtilities.dp(32));

    int maxNameWidth;
    if (AndroidUtilities.isTablet()) {
        maxNameWidth = AndroidUtilities.dp(530 - 32 - 18 - 57 * 2) / 2;
    } else {
        maxNameWidth = (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(32 + 18 + 57 * 2)) / 2;
    }
    String firstName;
    if (user != null) {
        firstName = UserObject.getFirstName(user);
    } else {
        if (!TextUtils.isEmpty(contact.first_name)) {
            firstName = contact.first_name;
        } else {
            firstName = contact.last_name;
        }
    }
    CharSequence name = TextUtils.ellipsize(firstName.replace('\n', ' '), textPaint, maxNameWidth, TextUtils.TruncateAt.END);
    nameLayout = new StaticLayout(name, textPaint, 1000, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    if (nameLayout.getLineCount() > 0) {
        textWidth = (int) Math.ceil(nameLayout.getLineWidth(0));
        textX = -nameLayout.getLineLeft(0);
    }

    TLRPC.FileLocation photo = null;
    if (user != null && user.photo != null) {
        photo = user.photo.photo_small;
    }
    imageReceiver.setImage(photo, null, "50_50", avatarDrawable, null, null, 0, null, 1);
    updateColors();
}
 
Example 13
private void calculateUsingTextSize(final float textSize) {
    if (mText == null) return;

    final float availableWidth = lerp(mExpandedBounds.width(), mCollapsedBounds.width(),
            mExpandedFraction, mTextSizeInterpolator);
    final float newTextSize;
    boolean updateDrawText = false;

    if (isClose(textSize, mCollapsedTextSize)) {
        newTextSize = mCollapsedTextSize;
        mScale = 1f;
        if (mCurrentTypeface != mCollapsedTypeface) {
            mCurrentTypeface = mCollapsedTypeface;
            updateDrawText = true;
        }
    } else {
        newTextSize = mExpandedTextSize;
        if (mCurrentTypeface != mExpandedTypeface) {
            mCurrentTypeface = mExpandedTypeface;
            updateDrawText = true;
        }

        if (isClose(textSize, mExpandedTextSize)) {
            // If we're close to the expanded text size, snap to it and use a scale of 1
            mScale = 1f;
        } else {
            // Else, we'll scale down from the expanded text size
            mScale = textSize / mExpandedTextSize;
        }
    }

    if (availableWidth > 0) {
        updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
        mCurrentTextSize = newTextSize;
        mBoundsChanged = false;
    }

    if (mTextToDraw == null || updateDrawText) {
        mTextPaint.setTypeface(mCurrentTypeface);
    }

    // Now we update the text ellipsis...
    mTextPaint.setTextSize(textSize);
    final CharSequence title = TextUtils.ellipsize(mText, mTextPaint,
            availableWidth, TextUtils.TruncateAt.END);
    if (!TextUtils.equals(title, mTextToDraw)) {
        mTextToDraw = title;
        mIsRtl = calculateIsRtl(mTextToDraw);
    }
    mTextPaint.setTextSize(mCurrentTextSize);
}
 
Example 14
Source Project: SmartChart   File: NumberPickerView.java    License: Apache License 2.0 4 votes vote down vote up
private void drawContent(Canvas canvas){
    int index;
    int textColor;
    float textSize;
    float fraction = 0f;// fraction of the item in state between normal and selected, in[0, 1]
    float textSizeCenterYOffset;

    for(int i = 0; i < mShowCount + 1; i++){
        float y = mCurrDrawFirstItemY + mItemHeight * i;
        index = getIndexByRawIndex(mCurrDrawFirstItemIndex + i, getOneRecycleSize(), mWrapSelectorWheel && mWrapSelectorWheelCheck);
        if(i == mShowCount / 2){//this will be picked
            fraction = (float)(mItemHeight + mCurrDrawFirstItemY) / mItemHeight;
            textColor = getEvaluateColor(fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else if(i == mShowCount / 2 + 1){
            textColor = getEvaluateColor(1 - fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(1 - fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(1 - fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else{
            textColor = mTextColorNormal;
            textSize = mTextSizeNormal;
            textSizeCenterYOffset = mTextSizeNormalCenterYOffset;
        }
        mPaintText.setColor(textColor);
        mPaintText.setTextSize(textSize);

        if(0 <= index && index < getOneRecycleSize()){
            CharSequence str = mDisplayedValues[index + mMinShowIndex];
            if (mTextEllipsize != null) {
                str = TextUtils.ellipsize(str, mPaintText, getWidth() - 2 * mItemPaddingHorizontal, getEllipsizeType());
            }
            canvas.drawText(str.toString(), mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        } else if(!TextUtils.isEmpty(mEmptyItemHint)){
            canvas.drawText(mEmptyItemHint, mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        }
    }
}
 
Example 15
Source Project: cathode   File: CollapsingTextHelper.java    License: Apache License 2.0 4 votes vote down vote up
private void calculateUsingTextSize(final float textSize) {
  if (mText == null) return;

  final float availableWidth;
  final float newTextSize;
  boolean updateDrawText = false;

  if (isClose(textSize, mCollapsedTextSize)) {
    availableWidth = mCollapsedBounds.width();
    newTextSize = mCollapsedTextSize;
    mScale = 1f;
    if (mCurrentTypeface != mCollapsedTypeface) {
      mCurrentTypeface = mCollapsedTypeface;
      updateDrawText = true;
    }
  } else {
    availableWidth = mExpandedBounds.width();
    newTextSize = mExpandedTextSize;
    if (mCurrentTypeface != mExpandedTypeface) {
      mCurrentTypeface = mExpandedTypeface;
      updateDrawText = true;
    }

    if (isClose(textSize, mExpandedTextSize)) {
      // If we're close to the expanded text size, snap to it and use a scale of 1
      mScale = 1f;
    } else {
      // Else, we'll scale down from the expanded text size
      mScale = textSize / mExpandedTextSize;
    }
  }

  if (availableWidth > 0) {
    updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
    mCurrentTextSize = newTextSize;
    mBoundsChanged = false;
  }

  if (mTextToDraw == null || updateDrawText) {
    mTextPaint.setTextSize(mCurrentTextSize);
    mTextPaint.setTypeface(mCurrentTypeface);

    // If we don't currently have text to draw, or the text size has changed, ellipsize...
    final CharSequence title =
        TextUtils.ellipsize(mText, mTextPaint, availableWidth, TextUtils.TruncateAt.END);
    if (!TextUtils.equals(title, mTextToDraw)) {
      mTextToDraw = title;
      mIsRtl = calculateIsRtl(mTextToDraw);
    }
  }
}
 
Example 16
Source Project: Yahala-Messenger   File: ChatBaseCell.java    License: MIT License 4 votes vote down vote up
public void setMessageObject(MessageObject messageObject) {
    currentMessageObject = messageObject;
    isPressed = false;
    isCheckPressed = true;
    isAvatarVisible = false;
    wasLayout = false;

 /*  if (currentMessageObject.messageOwner.getId() < 0 && currentMessageObject.messageOwner.getSend_state() != MessagesController.MESSAGE_SEND_STATE_SEND_ERROR && currentMessageObject.messageOwner.getSend_state() != MessagesController.MESSAGE_SEND_STATE_SENT) {
       if (MessagesController.getInstance().sendingMessages.get(currentMessageObject.messageOwner.getId()) == null) {
            currentMessageObject.messageOwner.setSend_state(MessagesController.MESSAGE_SEND_STATE_SEND_ERROR);
        }
    }*/
    //FileLog.e("messageObject.messageOwner.getJid()",messageObject.messageOwner.getJid()+"");
    try {


        currentUser = ContactsController.getInstance().friendsDict.get(messageObject.messageOwner.getJid());

        if (isChat && currentMessageObject.isOut()) {
            isAvatarVisible = true;
            if (currentUser != null) {
                if (currentUser.photo != null) {
                    currentPhoto = currentUser.photo.photo_small;
                } else {
                    currentPhoto = null;
                }
                avatarImage.setImage(currentPhoto, "50_50", getResources().getDrawable(Utilities.getUserAvatarForId(currentUser.id)));
            } else {
                avatarImage.setImage((TLRPC.FileLocation) null, "50_50", null);
            }
        }

        if (!media) {
            if (currentMessageObject.isOut()) {
                currentTimePaint = timePaintOut;
            } else {
                currentTimePaint = timePaintIn;
            }
        } else {
            currentTimePaint = timeMediaPaint;
        }

        currentTimeString = LocaleController.formatterDay.format(currentMessageObject.messageOwner.getDate());
        timeWidth = (int) Math.ceil(currentTimePaint.measureText(currentTimeString));

        namesOffset = 0;

        if (drawName && isChat && currentUser != null && currentMessageObject.messageOwner.getOut() != 1) {
            currentNameString = Utilities.formatName(currentUser.first_name, currentUser.last_name);
            nameWidth = getMaxNameWidth();

            CharSequence nameStringFinal = TextUtils.ellipsize(currentNameString.replace("\n", " "), namePaint, nameWidth - OSUtilities.dp(12), TextUtils.TruncateAt.END);
            nameLayout = new StaticLayout(nameStringFinal, namePaint, nameWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            if (nameLayout.getLineCount() > 0) {
                nameWidth = (int) Math.ceil(nameLayout.getLineWidth(0));
                namesOffset += OSUtilities.dp(18);
                nameOffsetX = nameLayout.getLineLeft(0);
            } else {
                nameWidth = 0;
            }
        } else {
            currentNameString = null;
            nameLayout = null;
            nameWidth = 0;
        }

        //  if (drawForwardedName && messageObject.messageOwner.tl_message instanceof TLRPC.TL_messageForwarded) {
       /* currentForwardUser = MessagesController.getInstance().users.get(messageObject.messageOwner.fwd_from_id);
        if (currentForwardUser != null) {
            currentForwardNameString = Utilities.formatName(currentForwardUser.first_name, currentForwardUser.last_name);

            forwardedNameWidth = getMaxNameWidth();

            CharSequence str = TextUtils.ellipsize(currentForwardNameString.replace("\n", " "), forwardNamePaint, forwardedNameWidth - AndroidUtilities.dp(40), TextUtils.TruncateAt.END);
            str = Html.fromHtml(String.format("%s<br>%s <b>%s</b>", LocaleController.getString("ForwardedMessage", R.string.ForwardedMessage), LocaleController.getString("From", R.string.From), str));
            forwardedNameLayout = new StaticLayout(str, forwardNamePaint, forwardedNameWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            if (forwardedNameLayout.getLineCount() > 1) {
                forwardedNameWidth = Math.max((int) Math.ceil(forwardedNameLayout.getLineWidth(0)), (int) Math.ceil(forwardedNameLayout.getLineWidth(1)));
                namesOffset += AndroidUtilities.dp(36);
                forwardNameOffsetX = Math.min(forwardedNameLayout.getLineLeft(0), forwardedNameLayout.getLineLeft(1));
            } else {
                forwardedNameWidth = 0;
            }
        } else {
            currentForwardNameString = null;
            forwardedNameLayout = null;
            forwardedNameWidth = 0;
        }*/
        //   } else {
        currentForwardNameString = null;
        forwardedNameLayout = null;
        forwardedNameWidth = 0;
        //   }

        requestLayout();
    } catch (Exception e) {
    }
}
 
Example 17
@SuppressWarnings("ReferenceEquality") // Matches the Typeface comparison in TextView
private void calculateUsingSubtitleTextSize(final float size) {
    if (subtitle == null) {
        return;
    }

    final float collapsedWidth = collapsedBounds.width();
    final float expandedWidth = expandedBounds.width();

    final float availableWidth;
    final float newTextSize;
    boolean updateDrawText = false;

    if (isClose(size, collapsedSubtitleTextSize)) {
        newTextSize = collapsedSubtitleTextSize;
        subtitleScale = 1f;
        if (currentSubtitleTypeface != collapsedSubtitleTypeface) {
            currentSubtitleTypeface = collapsedSubtitleTypeface;
            updateDrawText = true;
        }
        availableWidth = collapsedWidth;
    } else {
        newTextSize = expandedSubtitleTextSize;
        if (currentSubtitleTypeface != expandedSubtitleTypeface) {
            currentSubtitleTypeface = expandedSubtitleTypeface;
            updateDrawText = true;
        }
        if (isClose(size, expandedSubtitleTextSize)) {
            // If we're close to the expanded title size, snap to it and use a scale of 1
            subtitleScale = 1f;
        } else {
            // Else, we'll scale down from the expanded title size
            subtitleScale = size / expandedSubtitleTextSize;
        }

        final float textSizeRatio = collapsedSubtitleTextSize / expandedSubtitleTextSize;
        // This is the size of the expanded bounds when it is scaled to match the
        // collapsed title size
        final float scaledDownWidth = expandedWidth * textSizeRatio;

        if (scaledDownWidth > collapsedWidth) {
            // If the scaled down size is larger than the actual collapsed width, we need to
            // cap the available width so that when the expanded title scales down, it matches
            // the collapsed width
            availableWidth = Math.min(collapsedWidth / textSizeRatio, expandedWidth);
        } else {
            // Otherwise we'll just use the expanded width
            availableWidth = expandedWidth;
        }
    }

    if (availableWidth > 0) {
        updateDrawText = (currentSubtitleTextSize != newTextSize) || boundsChanged || updateDrawText;
        currentSubtitleTextSize = newTextSize;
        boundsChanged = false;
    }

    if (subtitleToDraw == null || updateDrawText) {
        subtitleTextPaint.setTextSize(currentSubtitleTextSize);
        subtitleTextPaint.setTypeface(currentSubtitleTypeface);
        // Use linear title scaling if we're scaling the canvas
        subtitleTextPaint.setLinearText(subtitleScale != 1f);

        // If we don't currently have title to draw, or the title size has changed, ellipsize...
        final CharSequence text =
            TextUtils.ellipsize(this.subtitle, subtitleTextPaint, availableWidth, TextUtils.TruncateAt.END);
        if (!TextUtils.equals(text, subtitleToDraw)) {
            subtitleToDraw = text;
            isRtl = calculateIsRtl(subtitleToDraw);
        }
    }
}
 
Example 18
private void calculateUsingTextSize(final float textSize) {
    if (mText == null) return;

    final float availableWidth;
    final float newTextSize;
    boolean updateDrawText = false;

    if (isClose(textSize, mCollapsedTextSize)) {
        availableWidth = mCollapsedBounds.width();
        newTextSize = mCollapsedTextSize;
        mScale = 1f;
        if (mCurrentTypeface != mCollapsedTypeface) {
            mCurrentTypeface = mCollapsedTypeface;
            updateDrawText = true;
        }
    } else {
        availableWidth = mExpandedBounds.width();
        newTextSize = mExpandedTextSize;
        if (mCurrentTypeface != mExpandedTypeface) {
            mCurrentTypeface = mExpandedTypeface;
            updateDrawText = true;
        }

        if (isClose(textSize, mExpandedTextSize)) {
            // If we're close to the expanded text size, snap to it and use a scale of 1
            mScale = 1f;
        } else {
            // Else, we'll scale down from the expanded text size
            mScale = textSize / mExpandedTextSize;
        }
    }

    if (availableWidth > 0) {
        updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
        mCurrentTextSize = newTextSize;
        mBoundsChanged = false;
    }

    if (mTextToDraw == null || updateDrawText) {
        mTextPaint.setTextSize(mCurrentTextSize);
        mTextPaint.setTypeface(mCurrentTypeface);
        // Use linear text scaling if we're scaling the canvas
        mTextPaint.setLinearText(mScale != 1f);

        // If we don't currently have text to draw, or the text size has changed, ellipsize...
        final CharSequence title = TextUtils.ellipsize(mText, mTextPaint,
                availableWidth, TextUtils.TruncateAt.END);
        if (!TextUtils.equals(title, mTextToDraw)) {
            mTextToDraw = title;
            mIsRtl = calculateIsRtl(mTextToDraw);
        }
    }
}
 
Example 19
Source Project: ticdesign   File: CollapsingTextHelper.java    License: Apache License 2.0 4 votes vote down vote up
private void calculateUsingTextSize(final float textSize) {
    if (mText == null) return;

    final float availableWidth;
    final float newTextSize;
    boolean updateDrawText = false;

    if (isClose(textSize, mCollapsedTextSize)) {
        availableWidth = mCollapsedBounds.width();
        newTextSize = mCollapsedTextSize;
        mScale = 1f;
        if (mCurrentTypeface != mCollapsedTypeface) {
            mCurrentTypeface = mCollapsedTypeface;
            updateDrawText = true;
        }
    } else {
        availableWidth = mExpandedBounds.width();
        newTextSize = mExpandedTextSize;
        if (mCurrentTypeface != mExpandedTypeface) {
            mCurrentTypeface = mExpandedTypeface;
            updateDrawText = true;
        }

        if (isClose(textSize, mExpandedTextSize)) {
            // If we're close to the expanded text size, snap to it and use a scale of 1
            mScale = 1f;
        } else {
            // Else, we'll scale down from the expanded text size
            mScale = textSize / mExpandedTextSize;
        }
    }

    if (availableWidth > 0) {
        updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
        mCurrentTextSize = newTextSize;
        mBoundsChanged = false;
    }

    if (mTextToDraw == null || updateDrawText) {
        mTextPaint.setTextSize(mCurrentTextSize);
        mTextPaint.setTypeface(mCurrentTypeface);

        // If we don't currently have text to draw, or the text size has changed, ellipsize...
        final CharSequence title = TextUtils.ellipsize(mText, mTextPaint,
                availableWidth, TextUtils.TruncateAt.END);
        if (!TextUtils.equals(title, mTextToDraw)) {
            mTextToDraw = title;
            mIsRtl = calculateIsRtl(mTextToDraw);
        }
    }
}
 
Example 20
private CharSequence ellipsizeToWidth(CharSequence text) {
  return TextUtils.ellipsize(text,
                             getPaint(),
                             getWidth() - getPaddingLeft() - getPaddingRight(),
                             TruncateAt.END);
}