第三章:深入文本(6)

简介:

格式化的文本
​正如你所看到的,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))
        };
    }
}

这是您将在本章的示例代码集合中看到的程序版本。 无论您使用哪种方法,下面是它的样子:
2018_02_05_204905

​您还可以使用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),并且几个单词以斜体表示:
2018_02_05_205632

​您可以说服一个标签显示多行或段落插入行尾字符。 这是在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
        };
    }
} 

请注意,单独的跨度包含两个平台特定的行尾字符串以间隔各行。 这确保行间距是基于默认字体大小,而不是刚刚显示的字体大小:
2018_02_05_210059

这些不是像素大小! 与iOS状态栏的高度一样,最好只是将这些尺寸模糊地称为某种“单位”。第5章将介绍一些额外的清晰度。
默认大小通常由操作系统选择,但其他大小由Xamarin.Forms开发人员选择。 在iOS上,默认值与“中”相同,但“Android默认值”与“小”相同,在Windows 10 Mobile上,“默认值”小于“微”。
iPad和Windows 10的尺寸分别与iPhone和Windows 10 Mobile相同。 但是,Windows 8.1和Windows Phone 8.1平台上的大小显示更多的差异:
2018_02_05_210230

​当然,在单个Label中使用多个Span对象不是呈现多个文本段落的好方法。 而且,文本通常有很多段落,所以必须滚动。 这是下一章的工作,以及对StackLayout和ScrollView的探索。

目录
相关文章
|
5月前
|
机器学习/深度学习 人工智能 自然语言处理
【Python机器学习】文本特征提取及文本向量化讲解和实战(图文解释 附源码)
【Python机器学习】文本特征提取及文本向量化讲解和实战(图文解释 附源码)
345 0
|
3月前
|
算法 JavaScript
「AIGC算法」将word文档转换为纯文本
使用Node.js模块`mammoth`和`html-to-text`,该代码示例演示了如何将Word文档(.docx格式)转换为纯文本以适应AIGC的文本识别。流程包括将Word文档转化为HTML,然后进一步转换为纯文本,进行格式调整,并输出到控制台。转换过程中考虑了错误处理。提供的代码片段展示了具体的实现细节,包括关键库的导入和转换函数的调用。
27 0
|
5月前
|
自然语言处理 语音技术
语言大模型和文本大模型的区别
【2月更文挑战第16天】语言大模型和文本大模型的区别
114 2
语言大模型和文本大模型的区别
|
XML 自然语言处理 数据格式
如何使用中文维基百科语料
前言 在做自然语言处理时很多时候都会需要中文语料库,高质量的中文语料库较难找,维基百科和百度百科算是比较不错的语料库。
2861 0
|
自然语言处理 算法
如何用3行代码简单实现文本情感分析
如何用3行代码简单实现文本情感分析
362 0
|
机器学习/深度学习 存储 自然语言处理
文本特征工程——下篇
文本特征工程——下篇
202 0
文本特征工程——下篇
|
机器学习/深度学习 自然语言处理 并行计算
长文本表示学习概述
摘要: "**如果**你愿意一层一层的剥开我的心·································**那么**你会坐牢的我跟你说"。自然语言就是这么神奇,句子中的长距离特征对于理解语义也非常关键,本文基于Tranformer、RNN、CNN、TCN分别概述近期学界对长句表示学习的诸多方法。 ## 1.长文本表示学习挑战 ​ NLP任务的特点和图像有极大的不同,上图展示了
2154 0
|
Android开发 Windows
下一篇
无影云桌面