删除SpannableString中的span

时间:2014-03-13 05:31:18

标签: android

如果我在文本框中选择特定文本(编辑文本),我想制作粗体,斜体,下划线,更改字体,单独更改所选文本的字体颜色。

例如:

  

文字希望格式化分段

如果我选择" matt"在单词格式化并尝试进行解包,结果将是这样的

  

文字希望 matted segmented

代码:

public class RichTextEditor extends EditText
{
    Context context;
    boolean isBold = false;
    boolean isItalic = false;
    boolean isUnderline = false;
    int fontSize = 6;
    int fontColor = Color.BLACK;
    TextView firstT, middleT, lastT;
    SpannableStringBuilder res = new SpannableStringBuilder();

    StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
    StyleSpan italicSpan = new StyleSpan(Typeface.ITALIC);
    StyleSpan BoldItalicSpan
        = new StyleSpan(Typeface.BOLD_ITALIC);

    public void setAllT (TextView firstT, TextView middleT, TextView lastT)
    {
        this.firstT = firstT;
        this.middleT = middleT;
        this.lastT = lastT;
    }

    public void makeBold ()
    {
        SpannableString text = new SpannableString(getText());
        int start = getSelectionStart();
        int end = getSelectionEnd();

        if (start == end)
        {
            isBold = true;
        }
        else
        {
            SpannableString before = (SpannableString) text.subSequence(0,
                    start);
            System.out.println("First : " + before);
            SpannableString middle = (SpannableString)     text.subSequence(start,
                    end);
            System.out.println("Middle : " + middle);
            SpannableString after = (SpannableString) text.subSequence(end,
                    text.length());
            System.out.println("Last : " + after);

            StyleSpan ref = isBetween(text, Typeface.BOLD);

            if (ref != null)
            {
                middle.removeSpan(ref);
                System.out.println("Removing Bold...");
                setText(before);
                append(middle);
                append(new SpannableString(" "));
                append(after);
            }
            else
            {
                middle.setSpan(getBoldStyleSpan(), 0, middle.length(), 0);

                setText(before);
                append(middle);
                append(after);
            }
            CharSequence now = TextUtils.concat(before, > middle, after);
            SpannableString ne = new SpannableString(now);
        }
    }

    public void makeItalic ()
    {

        SpannableString text = new SpannableString(getText());
        res = new SpannableStringBuilder(getText());

        int start = getSelectionStart();
        int end = getSelectionEnd();
        if (start == end)
        {
            isBold = true;
        }
        else
        {
            SpannableString before = (SpannableString) text.subSequence(0,
                    start);
            SpannableString middle = (SpannableString) text.subSequence(start, end);
            SpannableString after = (SpannableString) text.subSequence(end,
                text.length());
            StyleSpan ref = isBetween(text, Typeface.ITALIC);

            if (ref != null)
            {
                middle.removeSpan(getBoldStyleSpan());
                System.out.println("Removing...");
            }
            else
            {
                middle.setSpan(getItalicStyleSpan(), 0, middle.length(), >     0);
            }

            setText(before);
            append(middle);
            append(after);
        }
    }

    public void changeFontSize () {}  // TODO Auto-generated method stub    

    public void changeFont () {}   // TODO Auto-generated method stub   

    public void changeFontColor () {} // TODO Auto-generated method stub    


    private StyleSpan isBetween (SpannableString text, StyleSpan toCheck){
        //return span if it is between the param tocheck span,else null     
    }

    private StyleSpan getBoldStyleSpan () {
        return new StyleSpan(Typeface.BOLD);
    }

    private StyleSpan getItalicStyleSpan () {
        return new StyleSpan(Typeface.ITALIC);
    }

}

public RichTextEditor (Context context, AttributeSet attr)
{
    super(context, attr);
    this.context = context;>mStyles = new ArrayList<RichTextEditor.PropertyHolder>();
}

0 个答案:

没有答案