Ejemplo n.º 1
0
  @Override
  public int reactTagForTouch(float touchX, float touchY) {
    Spanned text = (Spanned) getText();
    int target = getId();

    int x = (int) touchX;
    int y = (int) touchY;

    Layout layout = getLayout();
    if (layout == null) {
      // If the layout is null, the view hasn't been properly laid out yet. Therefore, we can't find
      // the exact text tag that has been touched, and the correct tag to return is the default one.
      return target;
    }
    int line = layout.getLineForVertical(y);

    int lineStartX = (int) layout.getLineLeft(line);
    int lineEndX = (int) layout.getLineRight(line);

    // TODO(5966918): Consider extending touchable area for text spans by some DP constant
    if (x >= lineStartX && x <= lineEndX) {
      int index = layout.getOffsetForHorizontal(line, x);

      // We choose the most inner span (shortest) containing character at the given index
      // if no such span can be found we will send the textview's react id as a touch handler
      // In case when there are more than one spans with same length we choose the last one
      // from the spans[] array, since it correspond to the most inner react element
      ReactTagSpan[] spans = text.getSpans(index, index, ReactTagSpan.class);

      if (spans != null) {
        int targetSpanTextLength = text.length();
        for (int i = 0; i < spans.length; i++) {
          int spanStart = text.getSpanStart(spans[i]);
          int spanEnd = text.getSpanEnd(spans[i]);
          if (spanEnd > index && (spanEnd - spanStart) <= targetSpanTextLength) {
            target = spans[i].getReactTag();
            targetSpanTextLength = (spanEnd - spanStart);
          }
        }
      }
    }

    return target;
  }
Ejemplo n.º 2
0
  @Override
  public void draw(Canvas canvas) {

    Layout layout = getLayout();

    RectF rectf = mrectf;

    int left = getCompoundPaddingLeft();
    int top = getExtendedPaddingTop();

    rectf.set(
        left + layout.getLineLeft(0) - mPaddingH,
        top + layout.getLineTop(0) - mPaddingV,
        Math.min(
            left + layout.getLineRight(0), getScrollX() + getWidth() - getCompoundPaddingRight()),
        top + layout.getLineBottom(0) + mPaddingV);
    canvas.drawRoundRect(rectf, mCornerRadius, mCornerRadius, mpaint);
    super.draw(canvas);
  }