0

using this function, whenever I have a '\n' character in the text of my UITextView, it doesn't resize text size properly to fit the specified frame size, but when i remove the '\n' character, it works perfect does anyone know why?

extension UITextView{
open override func didMoveToSuperview() {
    var defaultFontSize = CGFloat(44.0)
    self.font = UIFont(name: (self.font?.fontName)!, size: defaultFontSize)
    if (self.contentSize.height > self.frame.size.height) {
        var fontIncrement = CGFloat(1.0)
        while (self.contentSize.height > self.frame.size.height) {
            print(defaultFontSize - fontIncrement)
            self.font = UIFont(name: (self.font?.fontName)!, size: defaultFontSize - fontIncrement)
            fontIncrement += 1.0
        }
    }

 }
}
Chris Hutchison
  • 601
  • 6
  • 20
  • FYI - extensions can't be used to override methods. They are only to be used to add new methods. – rmaddy Jun 01 '17 at 18:03
  • what do you mean? I am using this so when a uitextview is added to a view the size will adjust accordingly to frame size. Are you implying this won't work, or are you just saying for future reference? – Chris Hutchison Jun 01 '17 at 18:06
  • I'm saying you are attempting to override an existing method with an extension. That's isn't going to work. Swift extensions do not support overriding existing methods. *"Extensions can add new functionality to a type, but they cannot override existing functionality."* - from the [Extensions](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html#//apple_ref/doc/uid/TP40014097-CH24-ID151) chapter of the Swift book. If you want to override a method, create a subclass. – rmaddy Jun 01 '17 at 18:11
  • so pretty much that if i already have a didmovetosuper method for the uitextview that it won't call this method, but if i don't than it will?Sorry, but I was confused by this because the function works and gets called whenever i add a uitextview to a view. – Chris Hutchison Jun 01 '17 at 18:13
  • Please see https://stackoverflow.com/questions/38213286/overriding-methods-in-swift-extensions – rmaddy Jun 01 '17 at 18:15

0 Answers0