I have a UILabel that displays some chars. Like "x", "y" or "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. Are there methods to calculate that width of the text when a UIFont and font size is specified? There's also no line-break and just one single line.
问题:
回答1:
You can do exactly that via the various sizeWithFont: methods in NSString UIKit Additions. In your case the simplest variant should suffice (since you don't have multi-line labels):
NSString *someString = @"Hello World"; UIFont *yourFont = // [UIFont ...] CGSize stringBoundingBox = [someString sizeWithFont:yourFont]; There are several variations of this method, eg. some consider line break modes or maximum sizes.
回答2:
sizeWithFont: is now deprecated, use sizeWithAttributes: instead:
UIFont *font = [UIFont fontWithName:@"Helvetica" size:30]; NSDictionary *userAttributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor blackColor]}; NSString *text = @"hello"; ... const CGSize textSize = [text sizeWithAttributes: userAttributes]; 回答3:
Since sizeWithFont is deprecated, I'm just going to update my original answer to using Swift 4 and .size
//: Playground - noun: a place where people can play import UIKit if let font = UIFont(name: "Helvetica", size: 24) { let fontAttributes = [NSAttributedStringKey.font: font] let myText = "Your Text Here" let size = (myText as NSString).size(withAttributes: fontAttributes) } The size should be the onscreen size of "Your Text Here" in points.
回答4:
Based on Glenn Howes' excellent answer, I created an extension to calculate the width of a string. If you're doing something like setting the width of a UISegmentedControl, this can set the width based on the segment's title string.
extension String { func widthOfString(usingFont font: UIFont) -> CGFloat { let fontAttributes = [NSFontAttributeName: font] let size = self.size(attributes: fontAttributes) return size.width } func heightOfString(usingFont font: UIFont) -> CGFloat { let fontAttributes = [NSFontAttributeName: font] let size = self.size(attributes: fontAttributes) return size.height } } usage:
Swift 4
func widthOfString(usingFont font: UIFont) -> CGFloat { let fontAttributes = [NSAttributedStringKey.font: font] let size = self.size(withAttributes: fontAttributes) return size.width } func heightOfString(usingFont font: UIFont) -> CGFloat { let fontAttributes = [NSAttributedStringKey.font: font] let size = self.size(withAttributes: fontAttributes) return size.height } func sizeOfString(usingFont font: UIFont) -> CGSize { let fontAttributes = [NSAttributedStringKey.font: font] return self.size(withAttributes: fontAttributes) } 回答5:
This simple extension in Swift works well.
extension String { func size(OfFont font: UIFont) -> CGSize { return (self as NSString).size(attributes: [NSFontAttributeName: font]) } } Usage:
let string = "hello world!" let font = UIFont.systemFont(ofSize: 12) let width = string.size(OfFont: font).width // size: {w: 98.912 h: 14.32} 回答6:
This is for swift 2.3 Version. You can get the width of string.
var sizeOfString = CGSize() if let font = UIFont(name: "Helvetica", size: 14.0) { let finalDate = "Your Text Here" let fontAttributes = [NSFontAttributeName: font] // it says name, but a UIFont works sizeOfString = (finalDate as NSString).sizeWithAttributes(fontAttributes) } 回答7:
For Swift 4.2
func SizeOf_String( font: UIFont) -> CGSize { let fontAttribute = [NSAttributedStringKey.font: font] let size = self.size(withAttributes: fontAttribute) // for Single Line return size; } Not tested yet...
回答8:
For Swift 3.0+
extension String { func SizeOf_String( font: UIFont) -> CGSize { let fontAttribute = [NSFontAttributeName: font] let size = self.size(attributes: fontAttribute) // for Single Line return size; } } Use it like...
let Str = "ABCDEF" let Font = UIFont.systemFontOfSize(19.0) let SizeOfString = Str.SizeOfString(font: Font!)