如何根据fontMetrics.stringWidth(message)分割字符串
时间: 2024-05-05 11:19:42 浏览: 17
如果你想根据字符串的宽度来进行分割,可以使用以下代码:
```
FontMetrics fontMetrics = graphics.getFontMetrics();
int messageWidth = fontMetrics.stringWidth(message);
int startIndex = 0;
int endIndex = message.length() - 1;
while (startIndex < endIndex) {
int midIndex = (startIndex + endIndex) / 2;
int midWidth = fontMetrics.stringWidth(message.substring(0, midIndex));
if (midWidth < messageWidth) {
startIndex = midIndex + 1;
} else {
endIndex = midIndex;
}
}
String firstPart = message.substring(0, endIndex);
String secondPart = message.substring(endIndex);
```
这段代码可以将字符串按照给定的宽度进行分割。其中,首先获取字符串的宽度,并且初始化起始位置和结束位置。然后使用二分法来找到分割点,最后将字符串分成两部分。
相关问题
import QtQuick 2.15 Rectangle { width: 400 height: 200 Text { id: textItem text: "Hello, World!" font.pointSize: fontSize anchors.centerIn: parent } Binding { target: textItem property: "font.pixelSize" value: { var fontMetrics = new QtQuick.FontMetrics(textItem.font) var desiredSize = Math.min(textItem.width, textItem.height) * 0.3 // 设置为控件宽高的30% fontMetrics.tightBoundingRect(textItem.text).height > textItem.height ? desiredSize * textItem.height / fontMetrics.tightBoundingRect(textItem.text).height : desiredSize } } } 程序有什么问题?
根据你提供的代码,有一个问题需要注意:
在Binding中,将target的property属性设置为"font.pixelSize"时,应该使用双引号而不是单引号。修改为双引号后的代码如下:
```qml
Binding {
target: textItem
property: "font.pixelSize"
value: {
var fontMetrics = new QtQuick.FontMetrics(textItem.font)
var desiredSize = Math.min(textItem.width, textItem.height) * 0.3 // 设置为控件宽高的30%
fontMetrics.tightBoundingRect(textItem.text).height > textItem.height ?
desiredSize * textItem.height / fontMetrics.tightBoundingRect(textItem.text).height :
desiredSize
}
}
```
这样修改之后,程序应该能够正确运行了。请注意在QML中使用正确的引号用法。
如果还有其他问题,请随时提问。
字符串的长度,宽度该如何分别获取
在大多数编程语言中,可以使用以下方法获取字符串的长度和宽度:
长度(Length):
Python:使用len()函数
Java:使用length()方法
C++:使用strlen()函数
C#:使用Length属性
JavaScript:使用length属性
宽度(Width):
宽度一般指字符串在屏幕上显示所占用的字符数,因此需要根据当前的字体、字号等因素来计算宽度。在不同的编程语言中,计算字符串宽度的方法可能有所不同。
Python:可以使用第三方库pyautogui中的size()函数来获取字符串宽度
Java:可以使用FontMetrics类来计算字符串宽度
C++:可以使用GDI+中的函数GetTextExtentPoint32()来计算字符串宽度
C#:可以使用Graphics类中的MeasureString()方法来计算字符串宽度
JavaScript:可以使用canvas元素来计算字符串宽度
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)