Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 是否有一种方法可以在标签内部使用跨距,并使其合理?_Xamarin_Xamarin.forms - Fatal编程技术网

Xamarin 是否有一种方法可以在标签内部使用跨距,并使其合理?

Xamarin 是否有一种方法可以在标签内部使用跨距,并使其合理?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在使用此代码向标签内的文本添加一些颜色: <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" />

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

<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
{
    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;
        }
    }
}
public类justizedLabelRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
//如果我们有一个新的表单元素,更新文本
if(例如NewElement!=null)
UpdateTextOnControl();
}
受保护的覆盖无效OneElementPropertyChanged(对象发送方,System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
//如果格式化文本中有更改,则触发对重画控件的更新
if(e.PropertyName==nameof(Label.FormattedText))
{
UpdateTextOnControl();
}
}
作废UpdateTextOnControl()
{
if(Control==null)
返回;
//定义段落样式
var style=新的NSMutableParagraphStyle()
{
对齐=UITextAlignment.justized,
FirstLineHeadIndent=0.001f,
};
//定义框架以确保应用对齐对齐
Control.Frame=新矩形f(0,0,(float)Element.Width,(float)Element.Height);
控制线=0;
如果(Element.FormattedText.ToAttributed(Element.Font,Element.TextColor)为NSMutableAttributedString AttributeText)
{
var fullRange=new NSRange(0,attrText.Length);
attrText.AddAttribute(UIStringAttributeKey.ParagraphStyle,style,fullRange);
Control.AttributedText=属性文本;
}
}
}

在标签中使用
HorizontalTextAlignment=“Center”
会起作用吗?我希望它是正确的,这意味着每行的第一个字符和最后一个字符都是垂直排列的。对于误解,我深表歉意。看一看。
<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;
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;
        }
    }
}