任何方式直接设置跨度spanable文字? [英] Any way to set span directly to spanable text?

查看:346
本文介绍了任何方式直接设置跨度spanable文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个错误,但我需要知道... :)
IAM develeoping一个Android应用程序,在我想显示在一个单一的TextView键入的脸,发现的这方面的一个非常有用的,自定义字体扩展字体跨度跨度,
按我的理解,我们正在创造一个spannable用两个词如下:

This may be a blunder,But I need to know... :) Iam develeoping an android application,In in I want to display to type face in a single textview and found this One very useful,A custom typeface span that extends typeface span, As per my understanding , we are creating a spannable as follows using two words

String firstWord = "first ";
String secondWord = "second";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);

然后用我们这样的自定义设置跨越两式的脸说的话

then sets two type face to that words using our custom span like this

// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0,              
firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE),   rstWord.length(), secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

现在我的问题,我有一个大量的文本所以,如果我能够把在直接横跨在我的文字的将是一个更容易对于我们来说, 是可能把跨度直接以文字就像我们把< b>大胆< / b> 因为我需要的是用我的自定义的字体间距像< typeface1>样品< / typeface1> < typeface2>文件< / typeface2方式>

Now my question, I have a large amount of text so If i am able to put the spans directly in my text its will be a more easy for us ,is that possible to put spans directly to the text like we put <b>bold </b> as my need is to use my custom typeface span like <typeface1> sample </typeface1> <typeface2> Doc </typeface2>.

我需要的是直接将更改应用于文本所具有的SET SPAN做文本

这是可能的,如果可能的话,我需要写的跨度在我的文字,我怎样才能做到这一点..还是我compleatly错了?

Is that possible, if possible what i need to write as span in my text,How can I achieve this.. or am i compleatly wrong ?

感谢您对所有

推荐答案

很晚的答案,但你要的是pretty容易。

Very late answer but what you asked for is pretty easy.

例如,让我们说我们要改变引号之间的一切都变成斜体。

For instance, lets say we want to change everything between quotation marks into italic font.

    String s =  "\"Hello my name is Edward\" Hola, my nombre es Edward";        
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(text);
    Spannable spannable = new SpannableString(s);
    while (m.find()) {        
      int textLocation = text.indexOf(m.group(1)); 

      // Set the custom typeface to span over a section of the spannable object       
       spannable.setSpan( new CustomTypefaceSpan("sans-serif",my_italic_font),
       textLocation-1, textLocation + m.group(1).length(),spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                   

      // Set the text of a textView with the spannable object
      TextView textbox = (TextView) v.findViewById(R.id.cardtextbox);

    }
    textbox.setText( spannable );

的结果取值

你好,我的名字是爱德华的霍拉,我NOMBRE ES爱德华

"Hello my name is Edward" Hola, my nombre es Edward

现在,而不是引号,你会使用标签的正则表达式像&LT; B&GT;大胆&LT; / B&GT; ;

Now instead of quotation marks you would use a regex pattern for tags like <b>bold </b>;

和你也可以事后用一个简单的 .replace删除标记();

And you could also remove the tags afterwards with a simple .replace();

这篇关于任何方式直接设置跨度spanable文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆