Uitextfield Minimum Fontsize

How can I prevent UITextfield font size from becoming smaller as you type

In Interface Builder, if you select the text field, you can deselect the 'Adjust to Fit' checkbox in the TextField Attributes. The font should remain the same as you set it and will show an elipsis (50...) if the content exceeds the space available.

How can I adjust the font size according to the size of the text field?

Set font size for wR hR 160. To handle font for specified device, you should catch iPhone device model.

Sample Image

You should care about constraints if UITextField for iPad/iPhone in Storyboard.

Or use next code, for example, in func textFieldDidEndEditing(_ textField: UITextField):

textField.adjustsFontSizeToFitWidth = true

Or here:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

textField.adjustsFontSizeToFitWidth = true
return true
}

Can't get UITextField to autoshrink text

I used the answer posted by @Purva as a starting point to come up with this method that gives the required font size starting at the configured font size, and not to drop below the configured minimum font size. Whereas @Purva tested for the height of the text I required the width to fit. This method can be put in either a category or a subclass of UITextField. I have it in a subclass which also captures the UITextFieldTextDidChangeNotification. From this notification handler I call the new method and resize the font if required. I also call it when I am assigning new text to the textfield to make sure it will fit by subclassing - (void)setText: (NSString*)text.

In each case, when I call it I am using the following code:

    CGFloat requiredFontSize = self.requiredFontSize;
if( self.font.pointSize != requiredFontSize )
{
self.font = [self.font fontWithSize: requiredFontSize];
}

Here is the new method:

- (CGFloat)requiredFontSize
{
const CGRect textBounds = [self textRectForBounds: self.frame];
const CGFloat maxWidth = textBounds.size.width;

if( _originalFontSize == -1 ) _originalFontSize = self.font.pointSize;

UIFont* font = self.font;
CGFloat fontSize = _originalFontSize;

BOOL found = NO;
do
{
if( font.pointSize != fontSize )
{
font = [font fontWithSize: fontSize];
}

CGSize size = [self.text sizeWithFont: font];
if( size.width <= maxWidth )
{
found = YES;
break;
}

fontSize -= 1.0;
if( fontSize < self.minimumFontSize )
{
fontSize = self.minimumFontSize;
break;
}

} while( TRUE );

return( fontSize );
}

How to change textfield font size programmatically?

If you want to simply change your textfield's font size , and not change it's font style at the same time , code below may be work : `

UITextField *yourTextField = [[UITextField alloc]init];
CGFloat yourSelectedFontSize = 14.0 ;
UIFont *yourNewSameStyleFont = [yourTextField.font fontWithSize:yourSelectedFontSize];
yourTextField.font = yourNewSameStyleFont ;

One thing you have to note is that : when you change your textfield's font size , you should always pay attention to your textfield view's height , keep your textfiled's height taller than your font height !

You can have a try !



Related Topics



Leave a reply



Submit