Skip to content

Add support for text style and font family from attr for TagView #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Now, you have successfully created some TagViews. The following will show some m
| 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)
| tag_background | reference | TagView background resource(default none background)
| tag_text_style | enum | TagView text style. (Like TextView text style)
| tag_text_font_family | string | TagView font family (ej. san-serif-medium)

**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 @@ -29,12 +29,15 @@
import android.view.View;
import android.view.ViewGroup;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import androidx.annotation.DrawableRes;
import androidx.annotation.IntDef;
import androidx.customview.widget.ViewDragHelper;

import static co.lujun.androidtagview.Utils.dp2px;
Expand Down Expand Up @@ -156,6 +159,22 @@ public class TagContainerLayout extends ViewGroup {
*/
private int mTagTextColor = Color.parseColor("#FF666666");

/**
* TagView text font family
*/
private String mTagTextFontFamily = null;


/** @hide */
@IntDef(value = {Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC})
@Retention(RetentionPolicy.SOURCE)
public @interface Style {}

/**
* TagView text style
*/
private @Style int mTagTextStyle = Typeface.NORMAL;

/**
* TagView typeface
*/
Expand Down Expand Up @@ -329,6 +348,8 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mTagBackgroundColor = attributes.getColor(R.styleable.AndroidTagView_tag_background_color,
mTagBackgroundColor);
mTagTextColor = attributes.getColor(R.styleable.AndroidTagView_tag_text_color, mTagTextColor);
mTagTextFontFamily = attributes.getString(R.styleable.AndroidTagView_tag_text_font_family);
mTagTextStyle = attributes.getInt(R.styleable.AndroidTagView_tag_text_style, mTagTextStyle);
mTagTextDirection = attributes.getInt(R.styleable.AndroidTagView_tag_text_direction, mTagTextDirection);
isTagViewClickable = attributes.getBoolean(R.styleable.AndroidTagView_tag_clickable, false);
isTagViewSelectable = attributes.getBoolean(R.styleable.AndroidTagView_tag_selectable, false);
Expand Down Expand Up @@ -561,6 +582,7 @@ private void onAddTag(String text, int position) {
addView(tagView, position);
}


private void initTagView(TagView tagView, int position) {
int[] colors;
if (mColorArrayList != null && mColorArrayList.size() > 0) {
Expand All @@ -581,6 +603,15 @@ private void initTagView(TagView tagView, int position) {
tagView.setTagMaxLength(mTagMaxLength);
tagView.setTextDirection(mTagTextDirection);
tagView.setTypeface(mTagTypeface);

if (mTagTextFontFamily == null && mTagTextStyle != Typeface.NORMAL) {
tagView.setTypeface(Typeface.defaultFromStyle(mTagTextStyle));
} else if (mTagTextFontFamily != null) {
tagView.setTypeface(Typeface.create(mTagTextFontFamily, mTagTextStyle));
} else {
tagView.setTypeface(mTagTypeface);
}

tagView.setBorderWidth(mTagBorderWidth);
tagView.setBorderRadius(mTagBorderRadius);
tagView.setTextSize(mTagTextSize);
Expand Down Expand Up @@ -1382,6 +1413,44 @@ public void setTagTextDirection(int textDirection) {
this.mTagTextDirection = textDirection;
}


/**
* Get TagView FontFamily
*
* @return
*/
public String getTagTextFontFamily() {
return mTagTextFontFamily;
}

/**
* Set TagView FontFamily.
*
* @param fontFamily
*/
public void setTagTextFontFamily(String fontFamily) {
this.mTagTextFontFamily = fontFamily;
}

/**
* Get TagView text style.
*
* @return
*/
public @Style int getTagTextStyle() {
return mTagTextStyle;
}

/**
* Set TagView text style.
*
* @param textStyle
*/
public void setTagTextStyle(@Style int textStyle) {
this.mTagTextStyle = textStyle;
}


/**
* Get TagView typeface.
*
Expand Down
8 changes: 8 additions & 0 deletions androidtagview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
<attr name="tag_text_size" format="dimension" />
<attr name="tag_bd_distance" format="dimension" />
<attr name="tag_text_color" format="color" />
<attr name="tag_text_font_family" format="string" />
<attr name="tag_text_style" format="enum">
<enum name="normal" value="0" />
<enum name="bold" value="1" />
<enum name="italic" value="2" />
<enum name="bold_italic" value="3" />
</attr>
<attr name="tag_border_color" format="color" />
<attr name="tag_background_color" format="color" />
<attr name="tag_max_length" format="integer" />
Expand Down Expand Up @@ -51,5 +58,6 @@
<attr name="tag_cross_area_padding" format="dimension" />
<attr name="tag_support_letters_rlt" format="boolean" />
<attr name="tag_background" format="reference" />

</declare-styleable>
</resources>
2 changes: 2 additions & 0 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
app:tag_clickable="true"
app:tag_enable_cross="true"
app:tag_theme="pure_teal"
app:tag_text_style="italic"
app:vertical_interval="10dp" />

<LinearLayout
Expand Down Expand Up @@ -74,6 +75,7 @@
app:horizontal_interval="10dp"
app:tag_clickable="false"
app:tag_theme="random"
app:tag_text_font_family="sans-serif-black"
app:vertical_interval="10dp" />

<co.lujun.androidtagview.TagContainerLayout
Expand Down