[英]How to calculate the width of a text string of a specific font and font-size?
I have a UILabel that displays some chars.我有一个显示一些字符的 UILabel。 Like "x", "y" or "rpm".如“x”、“y”或“rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不使用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局,如果 UILabel 中的文本较小,则另一个视图将具有更大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定 UIFont 和字体大小时,是否有方法计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
I have a UILabel that displays some chars.我有一个显示一些字符的UILabel。 Like "x", "y" or "rpm".如“ x”,“ y”或“ rpm”。 How can I calculate the width of the text in the label (it does not ues the whole available space)?如何计算标签中文本的宽度(它不会占用整个可用空间)? This is for automatic layouting, where another view will have a bigger frame rectangle if that UILabel has a smaller text inside.这是用于自动布局的,如果UILabel的内部文本较小,则另一个视图将具有较大的框架矩形。 Are there methods to calculate that width of the text when a UIFont and font size is specified?指定了UIFont和字体大小时,是否有方法来计算文本的宽度? There's also no line-break and just one single line.也没有换行符,只有一行。
SwiftUI with Swift5 Swift5 的 SwiftUI
In SwiftUI, you could not find an easy way to convert UIFont to Font.在 SwiftUI 中,您找不到将 UIFont 转换为 Font 的简单方法。 So the previous answers may not work.所以以前的答案可能不起作用。 You could use GeometryReader{ geometryProxy in } inside the overlay() modifier to get the Text size.您可以在overlay()修饰符中使用GeometryReader{ geometryProxy in }来获取文本大小。 Be careful that if you don't use it inside overlay(), the View will expand to as much as it can.请注意,如果您不在 overlay() 中使用它,View 将尽可能地扩展。
If you want to pass the variable out, you may need to write a View extension function to do so.如果您想将变量传递出去,您可能需要编写一个 View 扩展函数来执行此操作。
Text("Lat.: \(latitude), Lon.: \(longitude) ")
.font(Font.custom("ChalkboardSE-Light",size: 20))
.overlay(
GeometryReader{ geometryProxy in
Image("button_img")
.resizable()
.frame(width: 10 , height: 10)
.offset(x: geometryProxy.size.width)
.extensionFunction{ //... }
})
I copied the extension function from another thread a couple days ago.几天前我从另一个线程复制了扩展功能。
extension View{
func extensionFunction(_ closure:() -> Void) -> Self{
closure()
return self
}
}
For this very old question/answers, I'm just putting in the current way to do it that always works, which is a combo of answers below.对于这个非常古老的问题/答案,我只是采用当前始终有效的方式来做,这是下面答案的组合。
By always I mean the most common case - which is unfortunately the trickest case - when you're literally modifying something inside of drawText#in rect
.我总是指的是最常见的情况——不幸的是,这是最棘手的情况——当你从字面上修改drawText#in rect
的某些内容时。
fileprivate extension String {
func realSize(font: UIFont) -> CGSize {
var s = self.size(withAttributes: [NSAttributedString.Key.font: font])
s.width = size.width.rounded(.up)
s.height = size.height.rounded(.up)
return s
}
}
Note that it rounds the size (specifically, up).请注意,它四舍五入大小(具体来说,向上)。 Why?为什么? In most cases you'll want this since typically layouts have even sized points and that's "what you'll need" to match other rendering.在大多数情况下,你会想要这个,因为通常布局具有均匀大小的点,这是“你需要的”来匹配其他渲染。
(Note that the difference is absolutely tiny anyway). (请注意,无论如何差异绝对很小)。 Try it both ways to see, as you wish.两种方式都试试看,如您所愿。
Hence for example something like因此,例如像
class StableLabel: UILabel {
override var intrinsicContentSize: CGSize {
// we arrange to return some fixed-always size ...
}
override func drawText(in rect: CGRect) {
let painful = text!.realSize(font: font)
// Assuming the size of this label is absolutely fixed by the layout,
// we want to ALWAYS draw the text sitting at the BOTTOM CENTER
// of the fixed size of this label, even as the size is changed.
// For example a focus or such may be changing the point size.
let left = knownSize.width - painful.width) / 2.0
let down = knownSize.height - painful.height)
let r = CGRect(origin: CGPoint(x: left, y: down), size: painful)
super.drawText(in: r)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.