Skip to content

Commit

Permalink
Support typeface
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 18, 2016
1 parent 5f6e3f0 commit be87be7
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Now, you have successfully created some TagViews. The following will show some m
| tag_horizontal_padding | dimension | Horizontal padding for TagView, include left and right padding(left and right padding are equal, default 20px)
| tag_vertical_padding | dimension | Vertical padding for TagView, include top and bottom padding(top and bottom padding are equal, default 17px)
| tag_text_size | dimension | TagView Text size(default 14sp)
| tag_bd_distance | dimension | The distance between baseline and descent(default 5.5px)
| tag_text_color | color | TagView text color(default #FF666666)
| tag_border_color | color | TagView border color(default #88F44336)
| tag_background_color | color | TagView background color(default #33F44336)
Expand Down Expand Up @@ -143,6 +144,11 @@ mTagContainerLayout.setTagBackgroundColor(Color.TRANSPARENT);
```java
mTagContainerLayout.setTagTextDirection(View.TEXT_DIRECTION_RTL);
```
* Use ```setTagTypeface(Typeface typeface)``` to set TagView text typeface.
```java
Typeface typeface = Typeface.createFromAsset(getAssets(), "iran_sans.ttf");
mTagContainerLayout.setTagTypeface(typeface);
```

**After set the attributes, set tags or add a tag.**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.Gravity;
Expand Down Expand Up @@ -80,6 +81,9 @@ public class TagContainerLayout extends ViewGroup {
/** TagView text color(default #FF666666)*/
private int mTagTextColor = Color.parseColor("#FF666666");

/** TagView typeface*/
private Typeface mTagTypeface = Typeface.DEFAULT;

/** Whether TagView can clickable(default unclickable)*/
private boolean isTagViewClickable;

Expand All @@ -92,6 +96,9 @@ public class TagContainerLayout extends ViewGroup {
/** TagView drag state(default STATE_IDLE)*/
private int mTagViewState = ViewDragHelper.STATE_IDLE;

/** The distance between baseline and descent(default 5.5px)*/
private float mTagBdDistance = 5.5f;

/** OnTagClickListener for TagView*/
private TagView.OnTagClickListener mOnTagClickListener;

Expand Down Expand Up @@ -138,6 +145,8 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr){
dp2px(context, mBorderWidth));
mBorderRadius = attributes.getDimension(R.styleable.AndroidTagView_container_border_radius,
dp2px(context, mBorderRadius));
mTagBdDistance = attributes.getDimension(R.styleable.AndroidTagView_tag_bd_distance,
mTagBdDistance);
mBorderColor = attributes.getColor(R.styleable.AndroidTagView_container_border_color,
mBorderColor);
mBackgroundColor = attributes.getColor(R.styleable.AndroidTagView_container_background_color,
Expand Down Expand Up @@ -373,12 +382,14 @@ private void initTagView(TagView tagView){
tagView.setTagTextColor(colors[2]);
tagView.setTagMaxLength(mTagMaxLength);
tagView.setTextDirection(mTagTextDirection);
tagView.setTypeface(mTagTypeface);
tagView.setBorderWidth(mTagBorderWidth);
tagView.setBorderRadius(mTagBorderRadius);
tagView.setTextSize(mTagTextSize);
tagView.setHorizontalPadding(mTagHorizontalPadding);
tagView.setVerticalPadding(mTagVerticalPadding);
tagView.setIsViewClickable(isTagViewClickable);
tagView.setBdDistance(mTagBdDistance);
tagView.setOnTagClickListener(mOnTagClickListener);
}

Expand Down Expand Up @@ -511,6 +522,23 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) {
public int getTagViewState(){
return mTagViewState;
}

/**
* Get TagView text baseline and descent distance.
* @return
*/
public float getTagBdDistance() {
return mTagBdDistance;
}

/**
* Set TagView text baseline and descent distance.
* @param tagBdDistance
*/
public void setTagBdDistance(float tagBdDistance) {
this.mTagBdDistance = dp2px(getContext(), tagBdDistance);
}

/**
* Set tags
* @param tags
Expand Down Expand Up @@ -909,6 +937,22 @@ public void setTagTextDirection(int textDirection) {
this.mTagTextDirection = textDirection;
}

/**
* Get TagView typeface.
* @return
*/
public Typeface getTagTypeface() {
return mTagTypeface;
}

/**
* Set TagView typeface.
* @param typeface
*/
public void setTagTypeface(Typeface typeface) {
this.mTagTypeface = typeface;
}

/**
* Get tag text direction
* @return
Expand Down
19 changes: 16 additions & 3 deletions androidtagview/src/main/java/co/lujun/androidtagview/TagView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.v4.widget.ViewDragHelper;
import android.text.TextUtils;
import android.view.MotionEvent;
Expand Down Expand Up @@ -60,6 +61,9 @@ public class TagView extends View {
/** Text direction(support:TEXT_DIRECTION_RTL & TEXT_DIRECTION_LTR, default TEXT_DIRECTION_LTR)*/
private int mTextDirection = View.TEXT_DIRECTION_LTR;

/** The distance between baseline and descent*/
private float bdDistance;

private Paint mPaint;

private RectF mRectF;
Expand All @@ -72,6 +76,8 @@ public class TagView extends View {

private float fontH, fontW;

private Typeface mTypeface;

private Runnable mLongClickHandle = new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -103,6 +109,7 @@ private void onDealText(){
}else {
mAbstractText = "";
}
mPaint.setTypeface(mTypeface);
mPaint.setTextSize(mTextSize);
final Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
fontH = fontMetrics.descent - fontMetrics.ascent;
Expand Down Expand Up @@ -144,9 +151,6 @@ protected void onDraw(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mTextColor);

// Set the distance between baseline and descent as 5.5px
float bdDistance = 5.5f;

if (mTextDirection == View.TEXT_DIRECTION_RTL){
float tmpX = getWidth() / 2 + fontW / 2;
for (char c : mAbstractText.toCharArray()) {
Expand Down Expand Up @@ -289,4 +293,13 @@ public int getTextDirection() {
public void setTextDirection(int textDirection) {
this.mTextDirection = textDirection;
}

public void setTypeface(Typeface typeface) {
this.mTypeface = typeface;
onDealText();
}

public void setBdDistance(float bdDistance) {
this.bdDistance = bdDistance;
}
}
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 @@ -21,6 +21,7 @@
<attr name="tag_horizontal_padding" format="dimension" />
<attr name="tag_vertical_padding" format="dimension" />
<attr name="tag_text_size" format="dimension" />
<attr name="tag_bd_distance" format="dimension" />
<attr name="tag_text_color" format="color" />
<attr name="tag_border_color" format="color" />
<attr name="tag_background_color" format="color" />
Expand Down
Binary file modified sample/sample-release.apk
Binary file not shown.
Binary file added sample/src/main/assets/iran_sans.ttf
Binary file not shown.
8 changes: 8 additions & 0 deletions sample/src/main/java/co/lujun/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
Expand Down Expand Up @@ -102,6 +103,13 @@ public void onClick(DialogInterface dialog, int which) {
// mTagContainerLayout1.setTagBackgroundColor(Color.TRANSPARENT);
// mTagContainerLayout1.setTagTextDirection(View.TEXT_DIRECTION_RTL);

// support typeface
// Typeface typeface = Typeface.createFromAsset(getAssets(), "iran_sans.ttf");
// mTagContainerLayout.setTagTypeface(typeface);

// adjust distance baseline and descent
// mTagContainerLayout.setTagBdDistance(4.6f);

// After you set your own attributes for TagView, then set tag(s) or add tag(s)
mTagContainerLayout1.setTags(list1);
mTagContainerLayout2.setTags(list2);
Expand Down

0 comments on commit be87be7

Please sign in to comment.