【问题标题】:Is there a way that I can use Spans inside of a label and also have it justified?有没有办法可以在标签内使用 Spans 并使其合理?
【发布时间】:2017-10-27 19:22:33
【问题描述】:

我正在使用此代码为标签内的文本添加一些颜色:

<Label.FormattedText>
  <FormattedString>
     <Span Text="I would like the word " />
     <Span Text="HERE" ForegroundColor="Red" FontAttributes="Bold" />
     <Span Text="to be in a bold font" />
   </FormattedString>
</Label.FormattedText>

之前我一直使用这个渲染代码来证明标签的合理性:

public class JustifiedLabelRenderer : LabelRenderer
{
    public JustifiedLabelRenderer() {}

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        //if we have a new forms element, we want to update text with font style (as specified in forms-pcl) on native control
        if (e.NewElement != null)
            UpdateTextOnControl();
    }

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

        //if there is change in text or font-style, trigger update to redraw control
        if (e.PropertyName == nameof(Label.Text)
           || e.PropertyName == nameof(Label.FontFamily)
           || e.PropertyName == nameof(Label.FontSize)
           || e.PropertyName == nameof(Label.TextColor)
           || e.PropertyName == nameof(Label.FontAttributes))
        {
            UpdateTextOnControl();
        }
    }

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

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

        //define attributes that use both paragraph-style, and font-style 
        var uiAttr = new UIStringAttributes()
        {
            ParagraphStyle = style,
            BaselineOffset = 0,

            Font = Control.Font
        };

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

        //set new text with ui-style-attributes to native control (UILabel)
        var stringToJustify = Control.Text ?? string.Empty;
        var attributedString = new Foundation.NSAttributedString(stringToJustify, uiAttr.Dictionary);
        Control.AttributedText = attributedString;
        Control.Lines = 0;
    }

当我尝试使用此代码设置颜色对齐标签时,跨度字体和颜色的更改不再显示。

有没有一种方法可以让我拥有一个既合理又具有内部颜色跨度的标签?

这是我正在使用的标签的 XAML 定义:

<local:JustifiedLabel x:Name="c0Label" />

这是填充它的代码:

var s = new FormattedString();
s.Spans.Add(new Span { Text = "test words test words test words test words test words test words", ForegroundColor = Color.Red});
s.Spans.Add(new Span { Text = "ABCDEFG", ForegroundColor = Color.Black });
s.Spans.Add(new Span { Text = " test words test words test words test words test words test words test words test words test words test words test words test words ", ForegroundColor = Color.FromHex("555555") });
c0Label.FormattedText = s;

【问题讨论】:

  • 在标签中使用HorizontalTextAlignment="Center" 可以吗?
  • 我希望它是合理的,这意味着每行的首尾字符都垂直对齐。
  • 对造成的误解深表歉意。看看this

标签: xamarin xamarin.forms


【解决方案1】:

您必须更新渲染器以考虑FormattedText。例如:尝试将渲染器逻辑更改为以下:

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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2011-08-27
    • 2021-10-14
    • 2013-09-04
    相关资源
    最近更新 更多