我正在使用一个自定义渲染器,它允许我对标签进行调整,同时也可以在范围内添加内容。以下是呈现器的代码:
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中它向我显示了对这一行的警告:
if (Element.FormattedText.ToAttributed(Element.Font, Element.TextColor) is NSMutableAttributedString attrText)警告指出:
Label.Font在1.3.0版中已经过时
有人知道我怎么解决这个问题吗?
发布于 2017-10-28 12:44:51
第一个选项将用于禁用警告:
#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对象以作为此调用中的默认操作:
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;
}
}https://stackoverflow.com/questions/46987728
复制相似问题