Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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
C# 对齐标签xamarin表单中的文本_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 对齐标签xamarin表单中的文本

C# 对齐标签xamarin表单中的文本,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我试图在标签Xamarin表单中对齐文本,但无法如何在不使用webview的情况下对齐与图片相同的文本(不只是样式对齐) 我已经附上了我的代码,但它只剩下了 string strText="MY LONG TEXT IS GOING HERE"; var lblMSG = new Label {TextColor = Color.Black }; lblMSG.Text=strText; lblMSG.LineBreakMod

我试图在标签Xamarin表单中对齐文本,但无法
如何在不使用webview的情况下对齐与图片相同的文本(不只是样式对齐)


我已经附上了我的代码,但它只剩下了

        string strText="MY LONG TEXT IS GOING HERE";

        var lblMSG = new Label {TextColor = Color.Black };

        lblMSG.Text=strText;
        lblMSG.LineBreakMode = LineBreakMode.WordWrap;
        lblMSG.HorizontalOptions = LayoutOptions.Fill;
        lblMSG.HorizontalTextAlignment = TextAlignment.Start;
        lblMSG.VerticalTextAlignment = TextAlignment.Center;
        StackLayout  stk=   new StackLayout { Children = { lblMSG}, BackgroundColor = Color.White ,HorizontalOptions =LayoutOptions.FillAndExpand }

而不是使用标签。我使用了HtmlWebViewSource

一个例子如下:

XAML:


XAML的代码隐藏(在您的案例中,viewModel.Model.Content=strText):

var browser=new WebView();
browser.HorizontalOptions=LayoutOptions.FillAndExpand;
browser.VerticalOptions=LayoutOptions.FillAndExpand;
var source=新的HtmlWebViewSource();
var text=“”+
"" +
String.Format(“{0}

”,viewModel.Model.Content)+ "" + ""; Html=文本; browser.Source=Source; webViewLayout.Children.Add(浏览器);
如果问题是关于将多行文本拉伸到全宽(文本必须接触块的左右两侧),则可以使用平台渲染器

你的意思是设计文本的样式吗?如果是,则需要手动设置。不设置样式仅用于证明-对于样式设置,我使用FormattedString()“lblMSG.HorizontalTextAlignment=TextAlignment.Start;”使文本从左侧开始。尝试将其更改为TextAlignment.Center.Simple,但有效。非常感谢。在您给定的解决方案中,我可以更改字体吗?我需要“蒙特塞拉特”字体我的元素没有连接到互联网请给我任何解决方案或任何想法?你需要添加字体文件到您的项目,然后它可以被引用。
 <StackLayout x:Name="webViewLayout"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand">
    <!-- view codebehind for WebView for overview text -->
 </StackLayout>
var browser = new WebView();
browser.HorizontalOptions = LayoutOptions.FillAndExpand;
browser.VerticalOptions = LayoutOptions.FillAndExpand;

var source = new HtmlWebViewSource();
var text = "<html>" +
        "<body  style=\"text-align: justify;\">" +
        String.Format("<p>{0}</p>", viewModel.Model.Content) +
        "</body>" +
        "</html>";
source.Html = text ;
browser.Source = source;
webViewLayout.Children.Add(browser);