【问题标题】:How to capitalize text in Flutter如何在 Flutter 中将文本大写
【发布时间】:2020-12-24 20:19:43
【问题描述】:

我试图找到如何在 Flutter 中将文本大写,但找不到。

我的代码:

Center(
    heightFactor: 2,
    child: Text(
      'Strengthening the bond of owners and pets, more than ever...',
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: 20.0,
        fontStyle: FontStyle.italic,
        fontWeight: FontWeight.bold,
        color: Colors.cyanAccent[700],
        wordSpacing: 8,
      ),
    )),

【问题讨论】:

    标签: flutter text


    【解决方案1】:

    我不知道有没有办法通过Text 小部件做到这一点,但您可以使用string.toUppercase() 将单词大写:

    Center(
    heightFactor: 2,
    child: Text(
      'Strengthening the bond of owners and pets, more than ever...'.toUpperCase(),
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: 20.0,
        fontStyle: FontStyle.italic,
        fontWeight: FontWeight.bold,
        color: Colors.cyanAccent[700],
        wordSpacing: 8,
      ),
    )),
    

    【讨论】:

    • @bensel - 这不是大写,是大写
    • @tim-montague 但我希望我已经回答了他的问题
    【解决方案2】:

    好吧,您可以使用这个包来大写 Text 小部件中的内容:

    https://pub.dev/packages/text_tools.

    //This will put the letter in position 15 in UpperCase, will print 'Strengthening The bond of owners and pets, more than ever...'
    Center(
        heightFactor: 2,
        child: Text(
          TextTools.toUppercaseAnyLetter(text: 'Strengthening the bond of owners and pets, more than ever...', position: 15),
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 20.0,
            fontStyle: FontStyle.italic,
            fontWeight: FontWeight.bold,
            color: Colors.cyanAccent[700],
            wordSpacing: 8,
          ),
        )),
    

    【讨论】:

      【解决方案3】:

      大写和大写是不同的:

      • 大写:“加强...”
      • 大写:“STRENGTHING...”

      大写

      要在所有语言环境中大写字符串,我建议使用intl 包:

      import 'package:intl/intl.dart';
      
      toBeginningOfSentenceCase('strengthening...');
      

      要将 en-US 等语言环境的字符串大写,那么我建议:

      String text = 'strengthening...';
      text = text[0].toUpperCase() + text.substring(1).toLowerCase();
      

      您还可以让虚拟键盘在句子开头自动切换为大写:

      TextField(
        textCapitalization: TextCapitalization.sentences
      )
      

      https://api.flutter.dev/flutter/services/TextCapitalization.html

      大写

      您可以使用大写字符串:

      'strengthening...'.toUpperCase()
      

      您还可以让虚拟键盘在每个字符上自动切换为大写

      TextField(
        textCapitalization: TextCapitalization.characters
      )
      

      https://api.flutter.dev/flutter/services/TextCapitalization.html

      【讨论】:

        猜你喜欢
        • 2019-06-04
        • 2018-07-20
        • 2017-08-02
        • 2018-11-17
        • 1970-01-01
        • 2021-07-16
        • 2016-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多