2017-04-24 102 views
4

我想使特定子字符串的字体为BOLD。StyleSpan不适用于TextView中的SpannableString

这里是我的代码:

String fullName = "some name"; 
String searchFreeText = "some"; 
SpannableString wordToSpan = new SpannableString(fullName); 

Typeface helvticaBoldTypeface = Typeface.createFromAsset(context.getAssets(), FONT_HELVETICA_BOLD); 
int startPosition = fullName.toLowerCase().indexOf(searchFreeText.toLowerCase()); 
StyleSpan bold = new StyleSpan(helvticaBoldTypeface.getStyle()); 
        wordToSpan.setSpan(bold, startPosition, startPosition + searchFreeText.length(), 0); 
firstLineTextView.setText(wordToSpan, BufferType.SPANNABLE); 

文本仍然不够大胆。 如果我的代码更改为:

firstLineTextView.setTypeface(helvticaBoldTypeface, Typeface.BOLD); 

,并删除

StyleSpan bold = new StyleSpan(helvticaBoldTypeface.getStyle()); 
        wordToSpan.setSpan(bold, startPosition, startPosition + searchFreeText.length(), 0); 

那么所有的文字是勇敢的,这是很正常的,但不是我想要的。 我需要使粗体只有特定的文本。我期望看到这个:一些的名字。

什么问题? 感谢

+0

检查我的回答 –

回答

1

我注意到一两件事: 您正在使用FONT_HELVETICA_BOLD(这已经是大胆的所以没有什么特别要发生在你的文字cuz它已经是大胆的只是将其更改为某个常规字体)

You can find some fonts here

为了使特定的文本大胆和有色,你可以试试下面的代码

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpannedWithBold); 

StyleSpan styleSpan = new StyleSpan(android.graphics.Typeface.BOLD); 

spannableStringBuilder.setSpan(styleSpan, 
     positionOfSearchedText, 
     positionOfSearchedText + searchTextToBeBold.length(), 
     Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

//Use this if you wanna make specific text bold 
SpannableString uncolouredSpan = new SpannableString(spannableStringBuilder); 
textViewToBeBoldSpanned.setText(uncolouredSpan); 

//Use this if you wanna make specific text bold plus coloured 
SpannableString coloredSpan = giveMeColoredSpan(
     new SpannableString(spannableStringBuilder), 
     ContextCompat.getColor(getContext(), R.color.your_specific_colour)); 

textViewToBeBoldSpannedAndColoured.setText(coloredSpan); 


public static SpannableString giveMeColoredSpan(SpannableString spannableString, int color) { 
    spannableString.setSpan(
      new ForegroundColorSpan(color), 0, spannableString.length(), 0); 
    return spannableString; 
} 

如果还是不知为何,你被卡住你的问题了,我已经创建了一个回购协议Github上https://github.com/harsh159357/customtextview

活动代码从我的回购

CustomTextViewActivity .java

import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.Spannable; 
import android.text.SpannableString; 
import android.text.SpannableStringBuilder; 
import android.text.style.ForegroundColorSpan; 
import android.text.style.StyleSpan; 
import android.view.View; 
import android.widget.Button; 
public class CustomTextViewActivity extends AppCompatActivity implements View.OnClickListener { 
    public static final String MAKE_THIS_SPECIFIC_PART_COLORED = "MakeThisSpecificPartColored"; 
    public static final String MAKE_THIS_SPECIFIC_PART_BOLD = "MakeThisSpecificPartBold"; 
    public static final String MAKE_THIS_SPECIFIC_PART_BOLD_AND_COLORED = "MakeThisSpecificPartBoldAndColored"; 
    CustomTextView customTextView1, 
      customTextView2, 
      customTextView3; 
    Button changeToRoboto, 
      changeToCalculus, 
      makeSpecificTextColored, 
      makeSpecificTextBold, 
      makeSpecificTextBoldAndColored; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_text_view); 
     customTextView1 = (CustomTextView) findViewById(R.id.customTextView1); 
     customTextView2 = (CustomTextView) findViewById(R.id.customTextView2); 
     customTextView3 = (CustomTextView) findViewById(R.id.customTextView3); 
     changeToRoboto = (Button) findViewById(R.id.change_to_roboto); 
     changeToCalculus = (Button) findViewById(R.id.change_to_calculus); 
     makeSpecificTextBold = (Button) findViewById(R.id.make_specific_text_bold); 
     makeSpecificTextColored = (Button) findViewById(R.id.make_text_colored); 
     makeSpecificTextBoldAndColored = (Button) findViewById(R.id.make_text_bold_and_colored); 
     changeToRoboto.setOnClickListener(this); 
     changeToCalculus.setOnClickListener(this); 
     makeSpecificTextColored.setOnClickListener(this); 
     makeSpecificTextBold.setOnClickListener(this); 
     makeSpecificTextBoldAndColored.setOnClickListener(this); 
    } 
    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.change_to_roboto: 
       customTextView1.setFont(getString(R.string.font_roboto_regular)); 
       customTextView2.setFont(getString(R.string.font_roboto_regular)); 
       customTextView3.setFont(getString(R.string.font_roboto_regular)); 
       break; 
      case R.id.change_to_calculus: 
       customTextView1.setFont(getString(R.string.font_calculus_sans)); 
       customTextView2.setFont(getString(R.string.font_calculus_sans)); 
       customTextView3.setFont(getString(R.string.font_calculus_sans)); 
       break; 
      case R.id.make_text_colored: 
       makeTextColoured(); 
       break; 
      case R.id.make_specific_text_bold: 
       makeSpecificTextBold(); 
       break; 
      case R.id.make_text_bold_and_colored: 
       makeTextBoldAndColored(); 
       break; 
     } 
    } 
    public static SpannableString giveMeColoredSpan(SpannableString spannableString, int color) { 
     spannableString.setSpan(
       new ForegroundColorSpan(color), 0, spannableString.length(), 0); 
     return spannableString; 
    } 
    private void makeTextColoured() { 
     String textToBeSpanned = customTextView1.getText().toString(); 
     SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpanned); 
     int positionOfSearchedText = textToBeSpanned.indexOf(MAKE_THIS_SPECIFIC_PART_COLORED); 
     SpannableString coloredSpan = giveMeColoredSpan(
       new SpannableString(textToBeSpanned), 
       ContextCompat.getColor(getApplicationContext(), android.R.color.holo_red_dark)); 
     customTextView1.setText(coloredSpan); 
    } 
    private void makeSpecificTextBold() { 
     String textToBeSpanned = customTextView2.getText().toString(); 
     SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpanned); 
     int positionOfSearchedText = textToBeSpanned.indexOf(MAKE_THIS_SPECIFIC_PART_BOLD); 
     StyleSpan styleSpan = new StyleSpan(android.graphics.Typeface.BOLD); 
     spannableStringBuilder.setSpan(styleSpan, 
       positionOfSearchedText, 
       positionOfSearchedText + MAKE_THIS_SPECIFIC_PART_BOLD.length(), 
       Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     customTextView2.setText(spannableStringBuilder); 
    } 
    private void makeTextBoldAndColored() { 
     String textToBeSpanned = customTextView3.getText().toString(); 
     SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textToBeSpanned); 
     int positionOfSearchedText = textToBeSpanned.indexOf(MAKE_THIS_SPECIFIC_PART_BOLD_AND_COLORED); 
     StyleSpan styleSpan = new StyleSpan(android.graphics.Typeface.BOLD); 
     spannableStringBuilder.setSpan(styleSpan, 
       positionOfSearchedText, 
       positionOfSearchedText + MAKE_THIS_SPECIFIC_PART_BOLD_AND_COLORED.length(), 
       Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     SpannableString coloredBoldSpan = giveMeColoredSpan(
       new SpannableString(spannableStringBuilder), 
       ContextCompat.getColor(getApplicationContext(), android.R.color.holo_red_dark)); 
     customTextView3.setText(coloredBoldSpan); 
    } 
} 

actvity_text_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:orientation="vertical" 
     tools:context="com.harsh.customtextview.CustomTextViewActivity"> 

     <com.harsh.customtextview.CustomTextView 
      android:id="@+id/customTextView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:gravity="center" 
      android:text="@string/roboto_regular" 
      android:textColor="@color/colorPrimary" 
      android:textSize="18sp" 
      app:font="@string/font_roboto_regular" /> 

     <com.harsh.customtextview.CustomTextView 
      android:id="@+id/customTextView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:gravity="center" 
      android:text="@string/themed_text" 
      android:textColor="@color/colorPrimary" 
      android:textSize="18sp" 
      app:theme="@style/roboto_theme" /> 

     <com.harsh.customtextview.CustomTextView 
      android:id="@+id/customTextView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:gravity="center" 
      android:text="@string/calculus_sans" 
      android:textColor="@color/colorPrimary" 
      android:textSize="18sp" 
      app:font="@string/font_calculus_sans" /> 


     <Button 
      android:id="@+id/change_to_roboto" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="Change to Roboto" /> 

     <Button 
      android:id="@+id/change_to_calculus" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="Change to Calculus" /> 

     <Button 
      android:id="@+id/make_text_colored" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="Make text coloured" /> 

     <Button 
      android:id="@+id/make_specific_text_bold" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="Make specific text bold" /> 

     <Button 
      android:id="@+id/make_text_bold_and_colored" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="Make text bold and colored" /> 

    </LinearLayout> 
</ScrollView> 
+0

感谢。但它没有奏效。文本保持正常 – Natalie

+1

@Natalie检查我编辑的答案 –

+0

我试着下面的代码和平和结果是规则不粗体字符在我的TextView整个字符串。我错过了什么吗? – Natalie

0

所以我解决了这个问题。 2部分是解决它: 设置TextView的空字符串应用样式的变化(firstLineTextView.setText("");)之前和使用firstLineTextView.append(wordToSpan);代替firstLineTextView.setText(wordToSpan)

下面是代码:

String fullName = "some name"; 
String searchFreeText = "some"; 
int startPosition = fullName.toLowerCase().indexOf(searchFreeText.toLowerCase()); 
SpannableString wordToSpan = new SpannableString(fullName); 
firstLineTextView.setText(""); 
        wordToSpan.setSpan(new StyleSpan(Typeface.BOLD), startPosition, startPosition + searchFreeText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        firstLineTextView.append(wordToSpan); 
相关问题