Last active
June 23, 2018 10:08
-
-
Save qtyq/90f9b4894069a8b3676c to your computer and use it in GitHub Desktop.
A helper class to construct and modify a SpannableString which uses the Factory design pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.util; | |
import android.content.Context; | |
import android.text.Spannable; | |
import android.text.SpannableString; | |
import android.text.TextPaint; | |
import android.text.style.ClickableSpan; | |
import android.text.style.ForegroundColorSpan; | |
import android.text.style.StyleSpan; | |
import android.view.View; | |
import com.example.R; | |
import java.io.Serializable; | |
/** | |
* Created by qtyq on 27/04/15. | |
*/ | |
public class SpannableBuilder { | |
private SpannableString mSpanStr; | |
public static SpannableBuilder init(String s) { | |
SpannableBuilder builder = new SpannableBuilder(); | |
builder.mSpanStr = new SpannableString(s); | |
return builder; | |
} | |
public SpannableBuilder makeBold(String ss) { | |
if (mSpanStr == null) | |
mSpanStr = new SpannableString(ss); | |
int index = mSpanStr.toString().indexOf(ss); | |
if (index != -1) { | |
mSpanStr.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), index, index + ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); | |
} | |
return this; | |
} | |
public SpannableBuilder setSpan(String ss, Object o) { | |
if (mSpanStr == null) | |
mSpanStr = new SpannableString(ss); | |
int index = mSpanStr.toString().indexOf(ss); | |
if (index != -1) { | |
mSpanStr.setSpan(o, index, index + ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); | |
} | |
return this; | |
} | |
public SpannableBuilder setColor(String ss, int color) { | |
if (mSpanStr == null) | |
mSpanStr = new SpannableString(ss); | |
int index = mSpanStr.toString().indexOf(ss); | |
if (index != -1) { | |
mSpanStr.setSpan(new ForegroundColorSpan(color), index, index + ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); | |
} | |
return this; | |
} | |
public SpannableBuilder makeLink(final Context context, String span, final Serializable object) { | |
int index = mSpanStr.toString().indexOf(span); | |
if (index == -1) { | |
return this; | |
} | |
ClickableSpan clickableSpan = new ClickableSpan() { | |
@Override | |
public void onClick(View view) { | |
//do something with the object | |
} | |
@Override | |
public void updateDrawState(final TextPaint textPaint) { | |
textPaint.setColor(context.getResources().getColor(R.color.dark_grey)); | |
} | |
}; | |
setSpan(span, clickableSpan); | |
makeBold(span); | |
return this; | |
} | |
public SpannableString create() { | |
return mSpanStr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment