Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I auto scale down a font in a Text widget to fit the max number of lines?

Tags:

flutter

dart

I'm trying to use BoxFit.scaleDown in a FittedBox's fit property to scale the font down in a Text widget to accommodate strings of varying length.

However, the below code will scale down the entire string and make it fit on one line, For the below example, I would like the font scaled down so that the string can fit on two lines (per maxLines property of the Text widget).

enter image description here

import 'package:flutter/material.dart';  void main() => runApp(new MyApp());  class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new MaterialApp(       title: 'Multi-Line Label Demo',       theme: new ThemeData(         primarySwatch: Colors.blue,       ),       home: new MyHomePage(title: 'Multi-Line Label Demo'),     );   } }  class MyHomePage extends StatefulWidget {   MyHomePage({Key key, this.title}) : super(key: key);    final String title;    @override   _MyHomePageState createState() => new _MyHomePageState(); }  class _MyHomePageState extends State<MyHomePage> {   @override   Widget build(BuildContext context) {     final textStyle = const TextStyle(fontSize: 28.0);     final text =         'Lorem Ipsum is simply dummy text of the printing and typesetting'         'industry. Lorem Ipsum has been the industry\'s standard dummy text'         'ever since the 1500s, when an unknown printer took a galley of type'         'and scrambled it to make a type specimen book.';     return new Scaffold(       appBar: new AppBar(         title: new Text(widget.title),       ),       body: new Center(         child: new Column(           mainAxisAlignment: MainAxisAlignment.center,           children: <Widget>[             new FittedBox(               fit: BoxFit.scaleDown,               child: new Text(                 text,                 maxLines: 2,                 style: textStyle,                 textAlign: TextAlign.center,               ),             ),           ],         ),       ),     );   } } 
like image 891
Albert Lardizabal Avatar asked Dec 06 '17 00:12

Albert Lardizabal


People also ask

How do you scale text in HTML?

In HTML, you can change the size of text with the <font> tag using the size attribute. The size attribute specifies how large a font will be displayed in either relative or absolute terms. Close the <font> tag with </font> to return to a normal text size.

How do I automatically resize text in flutter?

Simply just wrap your Text widget to FittedBox widget like, Here I want to resize my AppBar text based on width. AppBar( centerTitle: true, title: FittedBox( fit: BoxFit. fitWidth, child: Text('Hey this is my long text appbar title') ), ), Text will be resized based on width of AppBar.

How do you auto adjust text in HTML?

Syntax: font-size-adjust: number|none|initial|inherit; Below are the examples that illustrates the use of font-size-adjust property.


2 Answers

An easy drop-in solution is to use FittedBox with fit: BoxFit.scaleDown:

Container(   width: 100.0,   child: FittedBox(     fit: BoxFit.scaleDown,     // Optionally apply `alignment` to position the text inside the box:     //alignment: Alignment.topRight,     child: SizedBox(       child: Text('\$1,299.99'),   ) ) 
like image 93
Andrey Gordeev Avatar answered Sep 21 '22 07:09

Andrey Gordeev


you can use the auto text package

https://pub.dartlang.org/packages/auto_size_text

after install it, just replace all of 'Text' to 'AutoSizeText' and you will find every things will be good :)

like image 44
Winson Avatar answered Sep 21 '22 07:09

Winson