milo526
milo526

Reputation: 5083

Detect textfield overflowing

I'd like to be able to detect when a UITextfield overflows it bounds.

I want to detect Horizontal overflow (since there is only 1 line).

I was thinking about counting the amount of characters and multiplying that with a default value for the letter width and see if that fits inside the textfield bounds but that wouldn't work due too the letter having different widths.

I am aware of adjustsFontSizeToFitWidth but I want to increase the width of the textfield instead of decreasing the font size.

Upvotes: 1

Views: 211

Answers (1)

Nishant
Nishant

Reputation: 12617

Here is the code which first calculates the size of the text and then compares it to the label size:

let textSize: CGSize = self.label.text!.sizeWithAttributes([NSFontAttributeName: self.label.font])        
let isOverflowing: Bool = textSize.width > self.label.frame.size.width

Upvotes: 1

Related Questions