-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dry-Run of _calculateFontSize
?
#43
Comments
Could you give me an example of what you are trying to achieve? |
Why I need this? because I'm using low-level text API from flutter To calculate "visible number of lines" correctly I need to figure out the font size first. Now that I wrote all of this. I'll just try copying the content of "_calculateFontSize" and give it a try.. |
Yes I understand that you want to get the number of lines but it would help if you could explain your goal. What do you want to do with the number of lines? Maybe |
The feature I implemented successfully (with constant I have a container widget of known width and height I have 3 labels: A (title), B (meta info), and C (subtitle), aligned vertically one after the other within the container widget.
The text of labels A and C could be really long so if we ever surpass maximum font-size, then truncation occurs. So the solution for labels A and C's problem is:
This opened issue is about Step1 in the the solution above. Here's the reference code I wrote for Step2: import 'dart:ui' as dartUI;
import 'package:flutter/material.dart';
/// Returns visible number of lines -- calcuates Line Height using TextPainter
int getVisibleNumberOfLines({
String text,
TextStyle textStyle,
TextDirection textDirection,
double availableWidth,
double availableHeight,
double minFontSize,
}) {
// TODO complete calculating max lines correctly using `AutoSizeText`'s private method `_calculateFontSize`
double fontSize = minFontSize ?? textStyle.fontSize;
double largestLineHeight;
// create new `TextStyle` for `TextPainter`
final TextStyle textPainterStyle = textStyle.copyWith(
fontSize: fontSize,
height: null,
);
// text span
final textSpan = TextSpan(
text: text,
style: textPainterStyle,
);
// text painter
final textPainter = TextPainter(
text: textSpan,
textDirection: textDirection,
);
// layout text painter to allow computing line metrics
textPainter.layout(
minWidth: 0,
maxWidth: availableWidth ?? availableHeight,
);
// compute line metrics
List<dartUI.LineMetrics> lines = textPainter.computeLineMetrics();
// sort lines descendingly using height property
lines.sort((line1, line2) => line2.height.compareTo(line1.height));
// store largest height
largestLineHeight = lines.first.height;
// calculate number of lines
int visibleLinesNumber = (availableHeight / largestLineHeight).floor();
return visibleLinesNumber;
} |
Question
Is it possible to do a dry-run of
_calculateFontSize
without having to useAutoSizeText
widget?My use case requires calculating maximum number of lines and use the output value as a parameter for
AutoSizeText
widget.What I'm trying to do is pass
width
andheight
with a maximum int formaxLines
, in addition to the usual style and size parameters ofAutoSizeText
. In return I'd getfontSize
from_calculateFontSize
. All of this would be done without having to actually useAutoSizeText
widget.Version
The text was updated successfully, but these errors were encountered: