1

My problem: I am building a MacOS app in Swift 3 and am looking to auto-resize (change font-size) text in a NSTextFieldCell, such that the text is always visible in its container.

I have come across Get NSTextField contents to scale but am not sure how to apply this to Swift 3 within my app. Does anyone have any advice or an explanation (or even link) on how to achieve my goal?

Community
  • 1
  • 1
Kirill Miniaev
  • 151
  • 2
  • 6

1 Answers1

4

I've made a function that return a NSFont object :

func calculateFont(toFit textField: NSTextField, withString string: NSString, minSize min: Int, maxSize max: Int) -> NSFont {
    for i in min...max {
        var attr: [String: Any] = [:] as Dictionary
        attr[NSFontSizeAttribute] = NSFont(name: textField.font!.fontName, size: CGFloat(i))!
        let strSize = string.size(withAttributes: [NSFontAttributeName: NSFont.systemFont(ofSize: CGFloat(i))])
        let linesNumber = Int(textField.bounds.height/strSize.height)
        if strSize.width/CGFloat(linesNumber) > textField.bounds.width {
            return (i == min ? NSFont(name: "\(textField.font!.fontName)", size: CGFloat(min)) : NSFont(name: "\(textField.font!.fontName)", size: CGFloat(i-1)))!
        }
    }
    return NSFont(name: "\(textField.font!.fontName)", size: CGFloat(max))!
}

You can use it like that :

    // set multines
    self.textField.lineBreakMode = .byWordWrapping // this looks like it doesn't work, set it in the storyboard instead
    self.textField.maximumNumberOfLines = 100
    // set font
    self.textField.font = self.calculateFont(toFit: self.textField, withString: self.textField.stringValue as NSString, minSize: 8, maxSize: 50)

Tell me if that works for you

Damien
  • 3,322
  • 3
  • 19
  • 29
  • This worked beautifully! Thank you so much. I adjusted the `CGFloat(i-1)` to `CGFloat(i-5)` to give myself just a bit more font buffer, but it works like a charm. – Kirill Miniaev Oct 12 '16 at 23:54
  • Glad I can help, and that I didn't struggled for 45min for nothing :p – Damien Oct 13 '16 at 10:22
  • How do you know when the window changes it size? I want to adjust the font size when the window size is changed by the user. – Alexander Ko Oct 27 '17 at 15:59
  • I don't really dev for mac os, you might want to post a question if you have a problem ;) – Damien Oct 27 '17 at 16:20