如何通过spannable方法使用自定义字体设置所选文本的字体样式 [英] How to set font style of selected text using custom typeface through spannable method

查看:215
本文介绍了如何通过spannable方法使用自定义字体设置所选文本的字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从的EditText 使用自定义的字体可设置样式为选定的文本。我得到下面的错误在编译时。 构造StyleSpan(字体)未定义

I want to set style to selected text from EditText using custom typeface. I am getting below error at compile time. The constructor StyleSpan(Typeface) is undefined.

下面code我申请。

int start=editbox.getSelectionStart();
int end=editbox.getSelectionEnd();
Spannable span=(Spannable)editbox.getText();
StyleSpan f = new StyleSpan( 
                            Typeface.createFromAsset(getAssets(),
                             "fonts/genbkbasr.ttf"));
span.setSpan(f, start,end, 0);

感谢。

推荐答案

我写了一个类来解决这个限制。它出现在有限的测试工作,但我还没有写,我需要它的应用程序。请注意,假设自定义的字体是作为一种资产,它使一个静态调用来检索应用程序上下文(它需要访问的资源)。更好的方法是通过在上下文的构造。

I wrote a class to work around this limitation. It appeared to work in limited testing, but I haven't yet written the application that I needed it for. Note that it assumes that the custom font is available as an asset, and it makes a static call to retrieve the application's context (which it needs to access the resource). A better approach would be to pass in the Context to the constructor..

import android.content.Context;

public class TypefaceResourceSpan extends MetricAffectingSpan implements ParcelableSpan {

private String resourceName_;
private Typeface tf_;

public TypefaceResourceSpan(String resourceName) {
    super();
    resourceName_=resourceName;
    tf_=createTypeface(resourceName_);
}

public TypefaceResourceSpan(Parcel src) {
    resourceName_ = src.readString();
    tf_=createTypeface(resourceName_);
}

private Typeface createTypeface(String resourceName) {
    Typeface result=null;
    Context c=TikunKorimMain.getAppContext();
    if (c==null) {
        Log.e("TypefaceResourceSpan", "Application context is null!");
    }
    AssetManager am=c.getAssets();
    if (am==null) {
        Log.e("TypefaceResourceSpan", "AssetManager is null!");
    }
    result=Typeface.createFromAsset(am, resourceName);
    return result;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(resourceName_);
}

@Override
public void updateMeasureState(TextPaint p) {
    Typeface old=p.getTypeface();
    if ( old != null && !old.isBold() && tf_.isBold() ) {
        p.setFakeBoldText(true);
    }
    if ( old != null && !old.isItalic() && tf_.isItalic() ) {
        p.setTextSkewX(-0.25f);
    }
    p.setTypeface(tf_);
}

@Override
public void updateDrawState(TextPaint tp) {
    Typeface old=tp.getTypeface();
    if ( old != null && !old.isBold() && tf_.isBold() ) {
        tp.setFakeBoldText(true);
    }
    if ( old != null && !old.isItalic() && tf_.isItalic() ) {
        tp.setTextSkewX(-0.25f);
    }
    tp.setTypeface(tf_);
}

public int getSpanTypeId() {
    // TODO does this work???!?
    return 123456;
}

public int describeContents() {
    return 0;
}
}

这篇关于如何通过spannable方法使用自定义字体设置所选文本的字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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