2.Xamarin.Forms里的文本控制

来源:互联网 发布:js省市区三级联动 编辑:程序博客网 时间:2024/06/09 21:31

先是定义Label的位置和颜色,这里是重新声明了一个类继承的ContentPage来作为一个页面:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Xamarin.Forms;namespace App15{    class aaa:ContentPage    {        public aaa()        {            Content = new Label            {                Text = "aaaaaaaaaaaaaaaaa",                HorizontalOptions = LayoutOptions.Center,                VerticalOptions = LayoutOptions.Center,                BackgroundColor = Color.Blue,                TextColor = Color.Gray            };        }    }    public class App : Application    {        public App()        {            // The root page of your application            MainPage = new aaa();        }        protected override void OnStart()        {            // Handle when your app starts        }        protected override void OnSleep()        {            // Handle when your app sleeps        }        protected override void OnResume()        {            // Handle when your app resumes        }    }}

这里写图片描述

可以看到,文字和背景是能够改变颜色的,在Xamarin里,对颜色的支持也是很全面的
除了提前预置的那些颜色外,开发者还可以自定义颜色,使用如下方法:

new Color(double grayShade) new Color(double r, double g, double b) new Color(double r, double g, double b, double a) Color.FromRgb(double r, double g, double b) Color.FromRgb(int r, int g, int b) Color.FromRgba(double r, double g, double b, double a) Color.FromRgba(int r, int g, int b, int a) Color.FromHsla(double h, double s, double l, double a) 

同样Color类里也提供了几种方法以供使用:

AddLuminosity(double delta) MultiplyAlpha(double alpha) WithHue(double newHue) WithLuminosity(double newLuminosity) WithSaturation(double newSaturation) 

然后是字体和属性的设置

这个是加大字体,并且加粗和斜体

 public aaa()        {            Content = new Label            {                Text = "aaaaaaaaaaaaaaaaa",                HorizontalOptions = LayoutOptions.Center,                VerticalOptions = LayoutOptions.Center,                BackgroundColor = Color.Blue,                TextColor = Color.Gray,                FontSize=Device.GetNamedSize(NamedSize.Large,typeof(Label)),                FontAttributes=FontAttributes.Bold|FontAttributes.Italic            };        }

这里写图片描述

然后是在一段文字中使用多种格式的:

class aaa:ContentPage    {        public aaa()        {            FormattedString formattedString = new FormattedString();            formattedString.Spans.Add(new Span { Text = "I" });            formattedString.Spans.Add(new Span            {                Text = " love",                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),                FontAttributes = FontAttributes.Bold            });            formattedString.Spans.Add(new Span            {                Text = "Xamarin.forms!"            });            Content = new Label            {                FormattedText = formattedString,                HorizontalOptions = LayoutOptions.Center,                VerticalOptions = LayoutOptions.Center,                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))            };        }    }

这里写图片描述

当然,也可以省略些代码,这样写

class aaa:ContentPage    {        public aaa()        {            Content = new Label            {                FormattedText = new FormattedString                {                    Spans =                    {                        new Span                        {                            Text="I "                        },                        new Span                        {                            Text = " love",                            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),                            FontAttributes = FontAttributes.Bold                        },                        new Span                        {                            Text=" Xamarin.forms"                        }                    }                }                HorizontalOptions = LayoutOptions.Center,                VerticalOptions = LayoutOptions.Center,                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))            };        }    }

效果和上面的那段一样。

最后再附上一段所有大小的代码和图片

class aaa:ContentPage    {        public aaa()        {            FormattedString formattedString = new FormattedString();            NamedSize[] name =            {                NamedSize.Default,NamedSize.Micro,NamedSize.Small,NamedSize.Medium,NamedSize.Large            };            foreach(var a in name)            {                double fontsize = Device.GetNamedSize(a, typeof(Label));                formattedString.Spans.Add(new Span                {                    Text = string.Format("Named Size={0}({1:F2})", a, fontsize),                    FontSize=fontsize                });                if (a != name.Last())                {                    formattedString.Spans.Add(new Span                    {                        Text = "\n\n"                    });                }            }            Content = new Label            {                FormattedText = formattedString,                HorizontalOptions = LayoutOptions.Center,                VerticalOptions = LayoutOptions.Center            };        }    }

这里写图片描述

0 0
原创粉丝点击