格式化的文本
正如你所看到的,Label有一个Text属性,你可以设置为一个字符串。 但是Label也有一个替代的FormattedText属性,构造了一个格式不统一的段落。
FormattedText属性的类型是FormattedString,它具有类型IList 的Span属性,Span对象的集合。 每个Span对象都是统一格式的文本块,由六个属性管理:
Text
FontFamily
FontSize
FontAttributes
ForegroundColor
BackgroundColor
下面是一个实例化FormattedString对象的方法,然后将Span实例添加到它的Spanscollection属性中:
点击(此处)折叠或打开
public class VariableFormattedTextPage : ContentPage
{
public VariableFormattedTextPage()
{
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))
};
}
}
当每个跨度被创建时,它被直接传递给Spans集合的Add方法。 请注意,标签被赋予了NamedSize.Large的FontSize,而粗体设置的Span也被明确地赋予了相同的大小。 当一个范围给出一个FontAttributes设置时,它不会继承该标签的FontSize设置。
或者,也可以用一对花括号来初始化Spans集合的内容。 在这些大括号内,Span对象被实例化。 由于不需要方法调用,因此整个FormattedString初始化可以在Label初始化中发生:
点击(此处)折叠或打开
public class VariableFormattedTextPage : ContentPage
{
public VariableFormattedTextPage()
{
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))
};
}
}
这是您将在本章的示例代码集合中看到的程序版本。 无论您使用哪种方法,下面是它的样子:
您还可以使用FormattedText属性在整个段落中嵌入斜体或粗体字,如VariableFormattedParagraph程序所示:
点击(此处)折叠或打开
public class VariableFormattedParagraphPage : ContentPage
{
public VariableFormattedParagraphPage()
{
Content = new Label
{
FormattedText = new FormattedString
{
Spans =
{
new Span
{
Text = "\u2003There was nothing so "
},
new Span
{
Text = "very",
FontAttributes = FontAttributes.Italic
},
new Span
{
Text = " remarkable in that; nor did Alice " +
"think it so "
},
new Span
{
Text = "very",
FontAttributes = FontAttributes.Italic
},
new Span
{
Text = " much out of the way to hear the " +
"Rabbit say to itself \u2018Oh " +
"dear! Oh dear! I shall be too late!" +
"\u2019 (when she thought it over " +
"afterwards, it occurred to her that " +
"she ought to have wondered at this, " +
"but at the time it all seemed quite " +
"natural); but, when the Rabbit actually "
},
new Span
{
Text = "took a watch out of its waistcoat-pocket",
FontAttributes = FontAttributes.Italic
},
new Span
{
Text = ", and looked at it, and then hurried on, " +
"Alice started to her feet, for it flashed " +
"across her mind that she had never before " +
"seen a rabbit with either a waistcoat-" +
"pocket, or a watch to take out of it, " +
"and, burning with curiosity, she ran " +
"across the field after it, and was just " +
"in time to see it pop down a large " +
"rabbit-hold under the hedge."
}
}
},
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
}
}
该段以em空格开始(Unicode u2003),并包含所谓的智能引号( u201C和 u201D),并且几个单词以斜体表示:
您可以说服一个标签显示多行或段落插入行尾字符。 这是在NamedFontSizes程序中演示的。 将多个Span对象添加到foreach循环中的FormattedString对象。 每个Span对象使用不同的NamedFont值,并显示从Device.GetNamedSize返回的实际大小:
点击(此处)折叠或打开
public class NamedFontSizesPage : ContentPage
{
public NamedFontSizesPage()
{
FormattedString formattedString = new FormattedString();
NamedSize[] namedSizes =
{
NamedSize.Default, NamedSize.Micro, NamedSize.Small,
NamedSize.Medium, NamedSize.Large
};
foreach (NamedSize namedSize in namedSizes)
{
double fontSize = Device.GetNamedSize(namedSize, typeof(Label));
formattedString.Spans.Add(new Span
{
Text = String.Format("Named Size = {0} ({1:F2})",
namedSize, fontSize),
FontSize = fontSize
});
if (namedSize != namedSizes.Last())
{
formattedString.Spans.Add(new Span
{
Text = Environment.NewLine + Environment.NewLine
});
}
}
Content = new Label
{
FormattedText = formattedString,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
}
}
请注意,单独的跨度包含两个平台特定的行尾字符串以间隔各行。 这确保行间距是基于默认字体大小,而不是刚刚显示的字体大小:
这些不是像素大小! 与iOS状态栏的高度一样,最好只是将这些尺寸模糊地称为某种“单位”。第5章将介绍一些额外的清晰度。
默认大小通常由操作系统选择,但其他大小由Xamarin.Forms开发人员选择。 在iOS上,默认值与“中”相同,但“Android默认值”与“小”相同,在Windows 10 Mobile上,“默认值”小于“微”。
iPad和Windows 10的尺寸分别与iPhone和Windows 10 Mobile相同。 但是,Windows 8.1和Windows Phone 8.1平台上的大小显示更多的差异:
当然,在单个Label中使用多个Span对象不是呈现多个文本段落的好方法。 而且,文本通常有很多段落,所以必须滚动。 这是下一章的工作,以及对StackLayout和ScrollView的探索。