3. Flutter - 基础组件 之 Text

1. Text 属性介绍

Text继承自StatelessWidget,Text 主要通过设置 文本布局 及 文本样式 控制显示方式。

1. 文本布局:例如文本对齐方式textAlign、文本排版方向 textDirection,文本显示最大行数maxLines、文本截断规则 overflow 等等

2 文本样式: 如字体名称fontFamily、字体大小 fontSize、文本颜色color、文本阴影 shadows等等,这些参数被统一封装到了构造函数中的参数 style(TextStyle)

以下属性介绍摘录自:flutter基础之组件基础Text
原文作者: 陌问MW

  • 文本布局 属性介绍
const Text(
  //String类型必传参数,为要显示的文本字符串
  this.data, {
  //以下为可选命名参数
  //  
  Key key,
  //TextStyle类型可选参数,用于设置文本的样式  
  this.style,
  //StrutStyle类型可选参数,用于设置文本支撑样式  
  this.strutStyle,
  //TextAlign类型可选参数,用于设置文本水平方向如何对齐
  this.textAlign,
  //TextDirection类型参数,用于设置文本的显示方向(从左到右或从右到左)  
  this.textDirection,
  //Locale类型参数,用于设置多语言,可以根据不同的区域设置以不同的方式呈现相同的 Unicode 字符。
  this.locale,
  //bool类型参数,用于设置文本是否自动换行,如果为 `true` ,自动换行显示,`false` 则不换行
  //在一行显示,超出屏幕部分不显示。默认为 `true`   
  this.softWrap,
  //TextOverflow类型参数,用于设置文本溢出时如何显示
  this.overflow,
  //double类型参数,用于设置文本的缩放倍数  
  this.textScaleFactor,
  //int类型参数,用于设置最大显示行数,如果超出限制则会根据 overflow 设置的溢出样式显示   
  this.maxLines,
  //String类型参数,于设置显示文本的替代语义标签 
  this.semanticsLabel,
  //TextWidthBasis类型参数 ,用于设置如何测量渲染文本的宽度  
  this.textWidthBasis,
})
  • 文本样式 属性介绍
 const TextStyle({
  //是否将空值替换为父级Widget文本样式中的值,也就是子Widget在没有定义样式
  //的情况下是否继承父级Widget中的文本样式
  this.inherit = true,
  //Color类型对象,用于设置文本字体颜色
  this.color,
  //Color类型对象,用于设置文本背景色
  this.backgroundColor,
  //double类型可选参数,字体大小
  this.fontSize,
  //FontWeight类型参数,粗体
  this.fontWeight,
  //FontStyle类型参数,设置字体样式,如斜体等
  this.fontStyle,
  //double类型可选参数,设置字母间距(空格)
  this.letterSpacing,
  //double类型可选参数,设置单词之间的间距
  this.wordSpacing,
  //TextBaseline类型可选参数,用于设置不同范围文本间的对齐基线
  this.textBaseline,
  //double类型可选参数,设置文本跨度的高度。当height为null或省略时,
  //行高将直接由字体的度量标准确定,这可能与fontSize不同。当height
  //为非空时,文本范围的行高将是fontSize的倍数,并且正好是fontSize * height逻辑像素高
  this.height,
  //Locale类型可选参数,多语言
  this.locale,
  //Paint类型可选参数,绘制文本的前景样式,比如描边文字等
  this.foreground,
  //Paint类型可选参数,绘制文本的背景样式,可以设置填充,描边,画笔宽度等
  this.background,
  //List类型可选参数,用于在文字下方绘制阴影
  this.shadows,
  //List类型可选参数,用于设置影响显示字体样式的列表
  this.fontFeatures,
  //TextDecoration类型可选参数,用于设置文字附近的装饰,例如下划线
  this.decoration,
  //Color类型可选参数,用于设置文字装饰的颜色
  this.decorationColor,
  //TextDecorationStyle类型参数,用于设置文字装饰的样式
  this.decorationStyle,
  //double类型可选参数,设置文字装饰的笔触的粗细诚意字体定义的粗细
  this.decorationThickness,
  //String类型可选参数,文本风格调式,调试版本可用
  this.debugLabel,
  //String类型可选参数,用于设置绘制文本时使用的字体名称
  String fontFamily,
  //list类型可选参数,当在较高优先级的字体系列中找不到字形时,字体系列的有序列表将重新出现
  List fontFamilyFallback,
  //String类型可选参数,要使用包中定义的字体系列,必须提供package参数
  String package,
})

2. 富文本

  在iOS中,我们可以使用NSAttributedString进行富文本设置。在Flutter中 我们可以通过Text.rich()RichText()进行富文本设置。

const Text.rich(
    InlineSpan this.textSpan, {
    Key? key,
    this.style,
    ...,
  })
RichText({
    Key? key,
    required this.text, // InlineSpan 类型
    this.textAlign = TextAlign.start,
    this.textDirection,
    ...  
  })

Text.rich 方法中的textSpan参数类型: InlineSpan
RichText构造函数的 text参数类型: InlineSpan

两者均为InlineSpan 类型,InlineSpan 为抽象类。

  • InlineSpan及其子类介绍
abstract class InlineSpan extends DiagnosticableTree {
  const InlineSpan({
    this.style,
  });

抽象类不能直接实例化。Flutter已为我们提供了

一个抽象子类:PlaceholderSpan
两个可实例化子类:WidgetSpanTextSpan

WidgetSpan 继承自 PlaceholderSpan

abstract class PlaceholderSpan extends InlineSpan {
  const PlaceholderSpan({
    this.alignment = ui.PlaceholderAlignment.bottom,
    this.baseline,
    TextStyle? style,
  })
class WidgetSpan extends PlaceholderSpan {  const WidgetSpan({
    required this.child,
    ui.PlaceholderAlignment alignment = ui.PlaceholderAlignment.bottom,
    TextBaseline? baseline,
    TextStyle? style,
  })
class TextSpan extends InlineSpan {
  const TextSpan({
    this.text,
    this.children,
    TextStyle? style,
    this.recognizer,
    this.semanticsLabel,
  }) 
  • 示例代码:

方式1 Text.rich()
//富文本设置 Text.rich()
class Text_RichDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text.rich(TextSpan(children: [
      TextSpan(
          text: "《定风波》",
          style: TextStyle(
              fontSize: 25, fontWeight: FontWeight.bold, color: Colors.black)),
      TextSpan(
          text: "苏轼", style: TextStyle(fontSize: 18, color: Colors.redAccent)),
      TextSpan(
          text: "\n莫听穿林打叶声,何妨吟啸且徐行。\n竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。",
          style: TextStyle(color: Colors.blue, fontSize: 20))
    ]));
  }
}
方式 2 RichText()
//富文本设置 RichText()
class RishTextDemo2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RichText(
      text: TextSpan(children: [
        TextSpan(
            text: "《定风波》",
            style: TextStyle(
                fontSize: 25,
                fontWeight: FontWeight.bold,
                color: Colors.black)),
        TextSpan(
            text: "苏轼",
            style: TextStyle(fontSize: 18, color: Colors.redAccent)),
        TextSpan(
            text: "\n莫听穿林打叶声,何妨吟啸且徐行。\n竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。",
            style: TextStyle(color: Colors.blue, fontSize: 20))
      ]),
    );
  }
}
Simulator Screen Shot - iPhone 11 - 2021-03-13 at 17.59.50.png

你可能感兴趣的:(3. Flutter - 基础组件 之 Text)