首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义呈现程序中的1.3.0版本错误时,Label.Font已经过时

自定义呈现程序中的1.3.0版本错误时,Label.Font已经过时
EN

Stack Overflow用户
提问于 2017-10-28 08:27:19
回答 1查看 226关注 0票数 0

我正在使用一个自定义渲染器,它允许我对标签进行调整,同时也可以在范围内添加内容。以下是呈现器的代码:

代码语言:javascript
复制
public class JustifiedLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        //if we have a new forms element, update text
        if (e.NewElement != null)
            UpdateTextOnControl();
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        //if there is change in formatted-text, trigger update to redraw control
        if (e.PropertyName == nameof(Label.FormattedText))
        {
            UpdateTextOnControl();
        }
    }

    void UpdateTextOnControl()
    {
        if (Control == null)
            return;

        //define paragraph-style
        var style = new NSMutableParagraphStyle()
        {
            Alignment = UITextAlignment.Justified,
            FirstLineHeadIndent = 0.001f,

        };

        //define frame to ensure justify alignment is applied
        Control.Frame = new RectangleF(0, 0, (float)Element.Width, (float)Element.Height);
        Control.Lines = 0;

        if (Element.FormattedText.ToAttributed(Element.Font, Element.TextColor) is NSMutableAttributedString attrText)
        {
            var fullRange = new NSRange(0, attrText.Length);
            attrText.AddAttribute(UIStringAttributeKey.ParagraphStyle, style, fullRange);
            Control.AttributedText = attrText;
        }
    }

代码运行良好,但在IDE中它向我显示了对这一行的警告:

代码语言:javascript
复制
if (Element.FormattedText.ToAttributed(Element.Font, Element.TextColor) is NSMutableAttributedString attrText)

警告指出:

Label.Font在1.3.0版中已经过时

有人知道我怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-28 12:44:51

第一个选项将用于禁用警告:

代码语言:javascript
复制
#pragma warning disable 0618 //retaining legacy call to obsolete code
    if (Element.FormattedText.ToAttributed(font, Element.TextColor) is NSMutableAttributedString attrText)
#pragma warning restore 0618

或者,手动创建Font对象以作为此调用中的默认操作:

代码语言:javascript
复制
void UpdateTextOnControl()
{
    .....
    .....

    var fontSize = Element.FontSize;
    var fontAttributes = Element.FontAttributes;
    var fontFamily = Element.FontFamily;

    Font font;
    if (fontFamily != null)
        font = Font.OfSize(fontFamily, fontSize).WithAttributes(fontAttributes);
    else
        font = Font.SystemFontOfSize(fontSize, fontAttributes);

    if (Element.FormattedText.ToAttributed(font, Element.TextColor) is NSMutableAttributedString attrText)
    {
        var fullRange = new NSRange(0, attrText.Length);
        attrText.AddAttribute(UIStringAttributeKey.ParagraphStyle, style, fullRange);
        Control.AttributedText = attrText;
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46987728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档