2.Xamarin.Forms里的文本控制
来源:互联网 发布:js省市区三级联动 编辑:程序博客网 时间:2024/09/21 11:15
先是定义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
- 2.Xamarin.Forms里的文本控制
- 5.Xamarin.Forms里按钮的实现
- Xamarin.Forms教程开发Xamarin.Forms应用程序需要的工具
- Xamarin.Forms 的一点体会
- xamarin.forms 把文本转化成语音,text to Speech
- xamarin IOS对xamarin.forms的Tabbedpage的renderer实现
- xamarin.forms Geolocator Plugin 地理位置的获取
- xamarin.forms 拍照和视频的操作
- Xamarin.Forms 获取设备的ip地址
- Xamarin.Forms的ActivityIndicator和ProgressBar比较
- Xamarin.Forms特殊的视图BoxView
- Xamarin.Forms支持的地图显示类型
- Xamarin.Forms XAML控件的公共属性
- Xamarin.Forms的滚动视图ScrollView
- Xamarin.Forms的相对布局RelativeLayout
- Xamarin.Forms使用XLabs.Forms时MainActivity的继承问题
- Xamarin.Forms探索--使用 Xamarin.Forms 来创建跨平台的用户界面
- Xamarin.Forms入门-使用 Xamarin.Forms 来创建跨平台的用户界面
- Tapestry 组件详解
- 【PAT】1081. Rational Sum (20)
- centos6.3(64位) 安装apr
- PostgreSQL xlog $PGDATA and zfs snapshot based central backup & PITR case
- Ubuntu15.04中测试安装的opencv2.4.9 时遇到usr/bin/ld: cannot find -lcufft解决方法。
- 2.Xamarin.Forms里的文本控制
- UFT对象库的使用
- hdu 1824 Let's go home (2-sat)
- C++文件操作
- 马士兵Java课堂笔记-3-Switch循环语句小细节
- recovery test script for zfs snapshot clone + postgresql stream replication + archive
- 影像去条带
- 整理IOS9适配
- CASE : zfs snapshot backup for PostgreSQL when use multi-zfs filesystem