Skip to content

Commit

Permalink
Add: Whether to support 'letters show with RTL(eg: Android -> diordnA…
Browse files Browse the repository at this point in the history
…)' style(default false)
  • Loading branch information
whilu committed Mar 5, 2017
1 parent bec2cc0 commit 4dd343d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Now, you have successfully created some TagViews. The following will show some m
| tag_cross_color | color | The cross icon color(default Color.BLACK)
| tag_cross_line_width | dimension | The cross line width(default 1dp)
| tag_cross_area_padding | dimension | The padding of the cross area(default 10dp)
| tag_support_letters_rlt | boolean | Whether to support 'letters show with RTL(eg: Android -> diordnA)' style(default false)

**You can set these attributes in layout file, or use setters(each attribute has get and set method) to set them.**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public class TagContainerLayout extends ViewGroup {
/** OnTagClickListener for TagView*/
private TagView.OnTagClickListener mOnTagClickListener;

/** Whether to support 'letters show with RTL(eg: Android -> diordnA)' style(default false)*/
private boolean mTagSupportLettersRTL = false;

private Paint mPaint;

private RectF mRectF;
Expand Down Expand Up @@ -217,6 +220,8 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr){
mCrossColor = attributes.getColor(R.styleable.AndroidTagView_tag_cross_color, mCrossColor);
mCrossLineWidth = attributes.getDimension(R.styleable.AndroidTagView_tag_cross_line_width,
dp2px(context, mCrossLineWidth));
mTagSupportLettersRTL = attributes.getBoolean(R.styleable.AndroidTagView_tag_support_letters_rlt,
mTagSupportLettersRTL);
attributes.recycle();

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Expand Down Expand Up @@ -450,6 +455,7 @@ private void initTagView(TagView tagView){
tagView.setCrossAreaPadding(mCrossAreaPadding);
tagView.setCrossColor(mCrossColor);
tagView.setCrossLineWidth(mCrossLineWidth);
tagView.setTagSupportLettersRTL(mTagSupportLettersRTL);
}

private void invalidateTags(){
Expand Down Expand Up @@ -1189,4 +1195,20 @@ public float getCrossLineWidth() {
public void setCrossLineWidth(float mCrossLineWidth) {
this.mCrossLineWidth = mCrossLineWidth;
}

/**
* Get the 'letters show with RTL(like: Android -> diordnA)' style if it's enabled
* @return
*/
public boolean isTagSupportLettersRTL() {
return mTagSupportLettersRTL;
}

/**
* Set whether the 'support letters show with RTL(like: Android -> diordnA)' style is enabled
* @param mTagSupportLettersRTL
*/
public void setTagSupportLettersRTL(boolean mTagSupportLettersRTL) {
this.mTagSupportLettersRTL = mTagSupportLettersRTL;
}
}
27 changes: 24 additions & 3 deletions androidtagview/src/main/java/co/lujun/androidtagview/TagView.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class TagView extends View {
/** The distance between baseline and descent*/
private float bdDistance;

/** Whether to support 'letters show with RTL(eg: Android -> diordnA)' style(default false)*/
private boolean mTagSupportLettersRTL = false;

private Paint mPaint, mRipplePaint;

private RectF mRectF;
Expand Down Expand Up @@ -197,9 +200,19 @@ protected void onDraw(Canvas canvas) {
mPaint.setColor(mTextColor);

if (mTextDirection == View.TEXT_DIRECTION_RTL) {
canvas.drawText(mAbstractText,
(isEnableCross() ? getWidth() + fontW : getWidth()) / 2 - fontW / 2,
getHeight() / 2 + fontH / 2 - bdDistance, mPaint);
if (mTagSupportLettersRTL){
float tmpX = (isEnableCross() ? getWidth() + getHeight() : getWidth()) / 2
+ fontW / 2;
for (char c : mAbstractText.toCharArray()) {
String sc = String.valueOf(c);
tmpX -= mPaint.measureText(sc);
canvas.drawText(sc, tmpX, getHeight() / 2 + fontH / 2 - bdDistance, mPaint);
}
}else {
canvas.drawText(mAbstractText,
(isEnableCross() ? getWidth() + fontW : getWidth()) / 2 - fontW / 2,
getHeight() / 2 + fontH / 2 - bdDistance, mPaint);
}
} else {
canvas.drawText(mAbstractText,
(isEnableCross() ? getWidth() - getHeight() : getWidth()) / 2 - fontW / 2,
Expand Down Expand Up @@ -489,4 +502,12 @@ public int getCrossColor() {
public void setCrossColor(int mCrossColor) {
this.mCrossColor = mCrossColor;
}

public boolean isTagSupportLettersRTL() {
return mTagSupportLettersRTL;
}

public void setTagSupportLettersRTL(boolean mTagSupportLettersRTL) {
this.mTagSupportLettersRTL = mTagSupportLettersRTL;
}
}
1 change: 1 addition & 0 deletions androidtagview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@
<attr name="tag_cross_color" format="color" />
<attr name="tag_cross_line_width" format="dimension" />
<attr name="tag_cross_area_padding" format="dimension" />
<attr name="tag_support_letters_rlt" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit 4dd343d

Please sign in to comment.