(text as NSString).size(withAttributes: [.font: font]) to get the on-screen size in points. This replaces the deprecated sizeWithFont: method and works for single-line strings in UIKit.Why this happens
When building dynamic layouts in UIKit, you often need to know how much horizontal space a string will occupy at a given font and size. Common scenarios include sizing a UILabel to fit its content, positioning a sibling view relative to the text width, or calculating intrinsic content size for a custom view. The legacy sizeWithFont: method on NSString was removed in iOS 7, so code that still calls it will not compile on modern SDKs.
How to fix it
1. Single-line measurement with size(withAttributes:)
The current, non-deprecated API is NSString.size(withAttributes:). Pass a dictionary whose key is NSAttributedString.Key.font and whose value is the UIFont you intend to render with.
import UIKit
func width(of text: String, font: UIFont) -> CGFloat {
let attributes: [NSAttributedString.Key: Any] = [.font: font]
let size = (text as NSString).size(withAttributes: attributes)
return size.width
}
// Usage
let font = UIFont.systemFont(ofSize: 17, weight: .regular)
let labelWidth = width(of: "rpm", font: font) // e.g. 28.5 points
The returned CGSize uses points, matching the coordinate system of UIView frames. For a single-line label with no line-break mode, this width is exactly what you need for frame calculations.
2. Multi-line or constrained measurement with boundingRect(with:options:attributes:context:)
If the text may wrap or you have a maximum width, use boundingRect. This respects line-break modes and truncation.
func boundingRect(for text: String,
font: UIFont,
maxWidth: CGFloat,
maxLines: Int = 0) -> CGRect {
let attributes: [NSAttributedString.Key: Any] = [.font: font]
let constraintSize = CGSize(width: maxWidth, height: .greatestFiniteMagnitude)
var options: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
if maxLines > 0 { options.insert(.truncatesLastVisibleLine) }
return (text as NSString).boundingRect(with: constraintSize,
options: options,
attributes: attributes,
context: nil)
}
3. Convenience extension on String
For cleaner call sites, add a small extension:
extension String {
func width(withFont font: UIFont) -> CGFloat {
(self as NSString).size(withAttributes: [.font: font]).width
}
func height(withFont font: UIFont, constrainedToWidth width: CGFloat) -> CGFloat {
let rect = (self as NSString).boundingRect(with: CGSize(width: width, height: .greatestFiniteMagnitude),
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: [.font: font],
context: nil)
return ceil(rect.height)
}
}
// Usage
let w = "Hello".width(withFont: .systemFont(ofSize: 16))
let h = "Long text…".height(withFont: .systemFont(ofSize: 16), constrainedToWidth: 200)
How to prevent it
- Always use
size(withAttributes:)orboundingRectinstead of the deprecatedsizeWithFont:family. - Cache
UIFontobjects; creating them repeatedly is expensive. - When measuring inside
layoutSubviewsorsizeThatFits, reuse the same attribute dictionary to avoid allocation churn. - Remember that
UILabelalready implementssizeThatFitsandintrinsicContentSize; prefer Auto Layout constraints over manual frame math when possible.
FAQ
Does this work with custom fonts loaded at runtime?
Yes. As long as the UIFont instance is valid (created via UIFont(name:size:) or a font descriptor), the measurement reflects the exact glyph advances of that font.
Why is the width sometimes a fractional point value?
Core Text uses sub-pixel glyph positioning. The returned CGFloat preserves that precision. If you need an integer pixel width for bitmap rendering, apply ceil() or round() before converting to pixels via UIScreen.main.scale.
Can I measure attributed strings with mixed fonts?
Yes. Pass the full NSAttributedString to boundingRect(with:options:context:) (the instance method on NSAttributedString) instead of the NSString variant. The attributes dictionary is then taken from the attributed string itself.
Parts of this guide are adapted from content licensed under CC BY-SA 2.5; this article is shared under the same license.