Android 自定义字体 Spannable Typeface Span

发布于 2024-10-29 03:31:09 字数 797 浏览 2 评论 0原文

我在整个应用程序中使用 Helvetica 字体。目前我正在与资产分开创建字体。所以我说过,

HelveticaNeue = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeue.ttf");
HelveticaNeueBold = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeueBold.ttf");
HelveticaNeueBoldItalic = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeueBoldItalic.ttf");

当我为一个 TextView 使用一种字体时,这三种效果都很好。但是,我需要使用 spannable

Spannable WordtoSpan = new SpannableString("This text should be normal, but this needs to be bold, and normal");    
WordtoSpan.setSpan(new TypefaceSpan("bold"), 5, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

并且我希望字符串的一部分是 HelveticaNeueBold,一些只是 HelveticaNeue。我认为需要向Android表明上面的3种字体实际上是相关的,以便它可以很好地切换它们。也在寻找其他方法来做到这一点。

I am using Helvetica fonts throughout my application. Currently I am creating the fonts separately from assets. So I have say these three

HelveticaNeue = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeue.ttf");
HelveticaNeueBold = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeueBold.ttf");
HelveticaNeueBoldItalic = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeueBoldItalic.ttf");

Everything works great when I use one typeface for one TextView. However, I need to use a spannable

Spannable WordtoSpan = new SpannableString("This text should be normal, but this needs to be bold, and normal");    
WordtoSpan.setSpan(new TypefaceSpan("bold"), 5, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

And I want to have part of the string be HeleveticaNeueBold and some be just HelveticaNeue. I think what is needed is to indicate to Android that the 3 fonts above are actually related so that it can switch them nicely. Looking for any other way of doing this as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2024-11-05 03:31:09

您将需要编写一个自定义 TypefaceSpan。好消息是它们并不困难。有关示例,请参阅此处

You are going to want to write a custom TypefaceSpan. The good news is that they aren't difficult. See here for an example.

↙厌世 2024-11-05 03:31:09

您可以执行以下操作:

SpannableString spannableString = new SpannableString("your text");
Typeface typeface = Typeface.create(ResourcesCompat.getFont(this, R.font.your_font);
spannableString.setSpan(
    new TypefaceSpan(typeface, Typeface.NORMAL)), 
    0, 4, 
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

该构造函数将替换以前的文本样式,从而实现您想要的效果。然而,问题是它是在 API 28 中引入的。

You can do the following:

SpannableString spannableString = new SpannableString("your text");
Typeface typeface = Typeface.create(ResourcesCompat.getFont(this, R.font.your_font);
spannableString.setSpan(
    new TypefaceSpan(typeface, Typeface.NORMAL)), 
    0, 4, 
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

This constructor will replace the previous style of the text, achieving what you want. However, the catch is that it was introduced in API 28.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文