有没有直接测量文字高度的方法?我现在这样做的方法是使用Paint measureText()来获取宽度,然后通过试验和错误找到一个值来获得近似高度.我也一直在搞乱FontMetrics,但所有这些似乎都是近似的方法.
我正在尝试扩展不同分辨率的东西.我可以做到,但我最终得到了令人难以置信的冗长代码,并通过大量计算来确定相对大小.我讨厌它!一定有更好的方法.
我创建了一个盒子,在里面创建了图像和文本。我将框的内容对齐设置为TopStart但问题是文本相对于图像没有正确对齐。水平方向没问题,但垂直方向看起来有某种填充,但我尝试将填充设置为 0,但结果仍然相同。
即使将其从底部对齐,它也会设置比图像更高的像素
Box(contentAlignment = Alignment.TopStart,) {
Image(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "",
modifier = Modifier.size(125.dp),
colorFilter = ColorFilter.tint(MaterialTheme.colors.onBackground)
)
Text(text = "4",
color = MaterialTheme.colors.primary,
fontSize = 44.sp,
textAlign = TextAlign.Center
)
}
Run Code Online (Sandbox Code Playgroud)

我在Android中寻找将采用输入(text,text_font_size,device_width)的方法,并根据这些计算,它将返回显示特定文本所需的高度?
我正在根据他的内容设置文本视图/ webview高度运行时,我知道warp内容但我不能在我的情况下使用因为一些web视图最小高度问题.
所以我试图计算高度,并根据我设置视图高度.
我尝试过以下方法
Paint paint = new Paint();
paint.setTextSize(text.length());
Rect bounds = new Rect();
paint.getTextBounds(text, 0, 1, bounds);
mTextViewHeight= bounds.height();
Run Code Online (Sandbox Code Playgroud)
所以输出是
1)"Hello World"返回字体15的高度13
2)"Jelly Bean的最新版本在这里,性能优化"返回字体15的高度16
然后我试过了
Paint paint = new Paint();
paint.setTextSize(15);
paint.setTypeface(Typeface.SANS_SERIF);
paint.setColor(Color.BLACK);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), result);
Paint.FontMetrics metrics = brush.getFontMetrics();
int totalHeight = (int) (metrics.descent - metrics.ascent + metrics.leading);
Run Code Online (Sandbox Code Playgroud)
所以输出是
1)"Hello World"返回字体15的高度17
2)"最新版本的Jelly Bean在这里,性能优化"返回字体15的高度17
如果我将这些值设置为我的视图然后剪切一些文本,它不会显示所有内容.
在某些桌子上它看起来还不错,因为它有很大的宽度而不是在手机上.
有没有办法根据内容计算高度?
我一直在关注排版的Google官方Material Design指南(http://www.google.com/design/spec/style/typography.html),但我发现它们非常缺乏.例如,他们列出了你应该用于身体,小标题,标题,标题显示1,显示2等类型的10种样式.在你的应用程序中键入文本...这引出了我的第一个问题.
问题#1:如何知道"小标题"是什么,或者"标题"与"标题"是什么,或何时应该使用"display1"而不是"display4",这些项目是否有标准?
此外,指南规定领先的空间应该建立在文本的行高.因此,问题2.
问题2#:如何在Android中为文本添加"领先"空间,这是相关的Android指南吗?
问题3:虽然我已经下载了最新版本的Roboto,但我的中等字体看起来绝对大胆,而不是谷歌的优雅和稍厚的中等字体,我如何确认我的字体实际上与谷歌不同?
非常感谢你
fonts android typography material-design android-5.0-lollipop
当我看到javadoc时,FontMetric.getAscent()我看到:
字体上升是从字体的基线到大多数字母数字字符顶部的距离.Font中的某些字符可能会延伸到字体上升线之上.
但我写了一个快速的演示程序,我看到了这个:

其中每行文本的4条水平线是:
getDescent()getAscent()getHeight()注意getAscent()行和字符顶部之间的空格.我看过大多数字体和大小,总是存在这种差距.(而字体下降看起来恰到好处.)是什么给出的?
package com.example.fonts;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class FontMetricsExample extends JFrame
{
static final int marg = 10;
public FontMetricsExample()
{
super(FontMetricsExample.class.getSimpleName());
JPanel panel = new JPanel(new BorderLayout());
JPanel fontPanel = …Run Code Online (Sandbox Code Playgroud) 领先保证金的含义是什么?
文件LeadingMarginSpan 说
影响前导边距的段落样式.单个段落可以有多个主要边距跨度; 它们将按顺序呈现,每个都将其边距添加到它之前的边距.右边到左边的段落中的前导边距位于右边.
但它并没有真正说明领先优势是什么.
它是否像段落第一行的制表符缩进?或者它是整个段落缩进的地方?我认为它是/lidɪŋ/而不是/lɛdɪŋ/就像在行之间的空间一样.
我想知道的原因是我正在尝试使用StaticLayout创建自己的TextView.我指的是想法的Layout和StaticLayout 源代码.我试图删除所有不必要的部分,但我不知道这是什么.
以下是一些SO问题,也询问领先优势,但问题似乎知道这意味着什么.
图像确实很有用,但并非绝对必要.
对于某些字体,当line-height元素小于阈值时,scrollHeight大于clientHeight.
因此,字体属性中有一些东西会导致这种情况,但是这是什么以及如何避免使用CSS甚至是自定义字体的字体编辑器?
例如,在这个片段中scrollHeight的Tahoma是超过clientHeight虽然sans-serif时似乎确定line-height为1 时,该页面在浏览器(CTRL +)被缩小这种差异会增加,甚至无衬线发生.当行高低于1或字体大小变大时,差异会增加.
对于其他一些字体,它在行高1处达到5px,并通过将行高增加到2而减小,这导致计算错误.
var a = document.getElementById('a');
console.log('tahoma - a.clientHeight: ' + a.clientHeight);
console.log('tahoma - a.scrollHeight: ' + a.scrollHeight);
var b = document.getElementById('b');
console.log('sans - b.clientHeight: ' + b.clientHeight);
console.log('sans - b.scrollHeight: ' + b.scrollHeight);
var c = document.getElementById('c');
console.log('sans - lineHeight:0.5 - c.clientHeight: ' + c.clientHeight);
console.log('sans - lineHeight:0.5 - c.scrollHeight: ' + c.scrollHeight);
var …Run Code Online (Sandbox Code Playgroud)我正在使用a NSMutableAttribtuedString来构建带有格式的字符串,然后我将其传递给Core Text以渲染到框架中.问题是,我需要使用上标和下标.除非这些字符在字体中可用(大多数字体不支持它),否则设置属性kCTSuperscriptAttributeName什么都不做.
所以我想我只留下了唯一的选择,即通过改变字体大小和移动基线来伪造它.我可以做字体大小位,但不知道改变基线的代码.有人可以帮忙吗?
谢谢!
编辑:我正在考虑,考虑到我可以用来解决这个问题的时间,编辑一个字体,以便给它一个下标"2"......或者找到一个内置的iPad字体.有没有人知道任何带有下标"2"的衬线字体我可以使用?
我需要得到一个ascender/descender和x-height ..
通过使用以下代码,我可以找到下降器和总高度:
descender_height = paint.descent();
total_height = descender_height - paint.ascent();
//ascender = ?; is this always equal to descender height?
//x_height = ?; total_height - 2*descender_height ?
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尽力将文本垂直居中,但我无法做到.问题是我有一行按钮,其高度= fill_parent和weight = 1,随着行变小,我的文本开始触摸视图的底部,如下所示:

我尝试去除填充,边距,更改高度等.但似乎没有做到这一点.
即使文本大小接近视图高度,如何将其垂直对齐?
这是包含数字5的视图的代码:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
tools:ignore="HardcodedText"
android:clickable="false"
android:textSize="90dp"
android:textColor="?attr/color1"
android:gravity="center"
android:paddingLeft="@dimen/normal_margin"
android:paddingRight="@dimen/normal_margin"
android:padding="0dp" />
Run Code Online (Sandbox Code Playgroud) android ×7
fontmetrics ×2
fonts ×2
view ×2
android-view ×1
core-text ×1
css ×1
graphics ×1
html ×1
ipad ×1
java ×1
javascript ×1
kotlin ×1
margins ×1
objective-c ×1
staticlayout ×1
subscript ×1
textview ×1
typography ×1