在Xamarin中更改格式化文本的字体 [英] Change typeface of formatted text in Xamarin

查看:269
本文介绍了在Xamarin中更改格式化文本的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带格式文本的标签,我想更改其字体样式(我的意思是字体). 我尝试使用自定义标签渲染(例如对普通文本进行渲染),但是没有得到体现. 我的疑问是,我们是否可以更改带格式的文本的字体(因为它对于标签的文本(非带格式的文本)正常工作).

I have a label with formatted text and I wanna change its font style(I mean typeface). I tried using Custom Label Rendering( like doing it for the normal text) ,but it is not getting reflected. My doubt is whether we can change typeface of formatted text (since it is working fine for text (not formatted text) of label).

这是我的.xaml文件代码

This is my .xaml file code

<Label x:Name = "invosTitle" Grid.Column = "0" Grid.Row = "0" Grid.ColumnSpan = "3"  HorizontalOptions = "CenterAndExpand">
        <Label.FormattedText>
            <FormattedString>
                <FormattedString.Spans>
                    <Span Text = "abcd"  ForegroundColor = "White" ></Span>
                    <Span Text = "   "></Span>
                    <Span Text = "efgh" ForegroundColor = "Gray"></Span>
                </FormattedString.Spans>
            </FormattedString>
        </Label.FormattedText>
</Label>

,CustomLabelRenderer代码为

and CustomLabelRenderer code is

protected override void OnElementPropertyChanged (object sender , PropertyChangedEventArgs e) 
{
    base.OnElementPropertyChanged(sender, e);
    var label = (TextView)Control; 
    Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets,  "Fonts/microsoftjhengheibold.otf");
    label.Typeface = font;
    label.SetTypeface (font, TypefaceStyle.Bold);
}

推荐答案

使用FormattedString元素时,可以使用FontFamily属性分别更改每个Span的字体.您不需要自定义标签渲染器即可实现.

When using the FormattedString element, you can change the typeface of each Span individually with the FontFamily attribute. You don't need a custom label renderer to achieve this.

<Span Text="abcd"  
      ForegroundColor="White" 
      FontFamily="Helvetica" />

Xamarin字体" 文档.

这篇关于在Xamarin中更改格式化文本的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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