简体   繁体   中英

dynamically change font size of NSTextfield

I want to change the font size of my Textfield depending on the height of my textfield.

Because I have code that sizes my interface depending on the screen size( resolution of the screen) and when I have put in the lowest resolution.

The text is a bit cut of that makes it unreadable.

How can I dynamically change the font size. The textfield has fixed height and width.

I is for a osx application so I can't use the font size adjustment like in iOS

I recently had to create a method to this myself, here is this code I came up with

-(float)updateFontSize : (UITextField *)textField : (int)targetLabelWidth : (int)targetLabelHeight
{
    //Current Label Size
    CGSize size = [textField.text sizeWithFont:textField.font];

    //Size label should be limited to
    CGSize targetSize = CGSizeMake(targetLabelWidth , targetLabelHeight);

    //Current FontSize
    int fontSize = textField.font.pointSize;

    if(size.height > targetSize.height)
    {
        while(size.height > targetSize.height)
        {
            //Create new font
            UIFont *newFont = textField.font;

            //Decrement font size
            fontSize-=1;

            //Set new font size
            newFont = [newFont fontWithSize:fontSize];

            //set new label size
            size = [textField.text sizeWithFont:newFont];
        }
    }

    if(size.height < targetSize.height)
    {
        while(size.height < targetSize.height)
        {
            //Create new font
            UIFont *newFont = textField.font;

            //Increment font size
            fontSize+=1;

            //Set new font size
            newFont = [newFont fontWithSize:fontSize];

            //set new label size
            size = [textField.text sizeWithFont:newFont];

        }
    }

    //Prevent fontsize ever being 0
    if(fontSize == 0)
    {
        fontSize = 1;
    }
    return fontSize;
}

I made a quick class just to test it. You can play about with the height, and it will alter its size accordingly.

int height = 10;

UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 200, height)];
field.borderStyle = UITextBorderStyleRoundedRect;

field.textAlignment = NSTextAlignmentLeft;
field.text = @"testing";

float size = [self updateFontSize:field :200 :height];
field.font = [UIFont fontWithName:@"Arial" size:size];

[self.view addSubview:field];

Edit: Updated

When I created this code, this was originally created when setting the width and height of a label so I could resize the label so it could fit it within a container. I quickly re-adapted for TextField, but it should work fine

Before I can resize it, I must figure out the current width/height of the text contained within the text field. Now in my method, I knew what the width and height should be, however you want the method like this

So we have a new method

 -(float)getFontSizeToFit:(NSTextField*)aTextField theMultilineTextToFitIn:(NSString*)aString 
{

}

Ok lets go with this. Note I am not on a mac, so I am doing this short hand

So you have a textfield with some text like this

"Hello this is some random text"

So you call

CGSize size = [textField.text sizeWithFont:textField.font];

This creates a size object which contains both the width and height of the text prior to doing any line breaks or anything like that

So lets say the width and height of the text was W = 200, H = 30 and lets say the textField only has a width 150, height 50 and we need the text to fit within here. So we have

Width = 200
Height = 30
textFieldWidth = 150
textFieldHeight = 50

I can simply do

NumberOfLines = Width /textFieldWidth . 

If this value is < 1, then I know the text will fit within the textField, however if it is greater than 1, then I know it will not fit within the textField and therefore need resized. In the above example, it would be 200/150 = 1.333. I always this value up, so I would round it up to 2. So I know I need at least 2 lines in order to fit this label inside the container. I know the height of the label which in the above example was 30, so I do

Height * NumberOfLines = NumberOfLinesNeeded 30 * 2 = 60

However if you look above, the height of the textField was only 50. Which means the object needs resized. So lets run through the method with the variables above

 CGSize size = [textField.text sizeWithFont:textField.font];

This creates the size object (200,30)

However the TextField itself is only (150,60)

Now we can the text style to word wrap, so the text will be split into 2 lines, however the problem is the text height would be greater than the height of the textField. So we need to start reducing the font size until the combined hieght of the 2 lines of text is less than 60.

This is where the while loop comes in. From here you can use a while loop to continually reduce the font size by 1 of the text, and then perform the above calculations, and keep doing it until the text fits within the text field itself.

As I mentioned before I am not on a mac, so I am unable to create this method for you. But hopefully using the above logic I described, you should be able to create something similar yourself.

Good luck, hope this helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM