简体   繁体   English

动态更改NSTextfield的字体大小

[英]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 我适用于osx应用程序,因此无法使用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 我很快重新适应了TextField,但是应该可以正常工作

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 请注意,我不在Mac电脑上,因此我正在做此简写

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. 因此,假设文本的宽度和高度为W = 200,H = 30,并且假设textField的宽度仅为150,高度为50,我们需要文本适合此处。 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. 如果此值<1,那么我知道该文本将适合textField,但是如果它大于1,那么我知道它将不适合textField,因此需要调整大小。 In the above example, it would be 200/150 = 1.333. 在上面的示例中,它将是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. 我总是将此值向上取整,因此我会将其四舍五入为2。因此,我知道我至少需要2行才能将该标签放入容器中。 I know the height of the label which in the above example was 30, so I do 我知道标签的高度在上面的示例中是30,所以我知道

Height * NumberOfLines = NumberOfLinesNeeded 30 * 2 = 60 高度* NumberOfLines =需要NumberOfLines 30 * 2 = 60

However if you look above, the height of the textField was only 50. Which means the object needs resized. 但是,如果从上方看,textField的高度仅为50。这意味着需要调整对象的大小。 So lets run through the method with the variables above 因此,让我们使用上述变量遍历该方法

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

This creates the size object (200,30) 这将创建大小对象(200,30)

However the TextField itself is only (150,60) 但是,TextField本身仅为(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. 现在我们可以将文本样式进行自动换行,因此文本将被分成两行,但是问题是文本高度将大于textField的高度。 So we need to start reducing the font size until the combined hieght of the 2 lines of text is less than 60. 因此,我们需要开始减小字体大小,直到两行文本的组合高度小于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. 这是while循环进入的地方。从这里开始,您可以使用while循环将文本的字体大小连续减小1,然后执行上述计算,并继续进行操作,直到文本适合文本字段本身为止。

As I mentioned before I am not on a mac, so I am unable to create this method for you. 如前所述,我不在Mac上,因此无法为您创建此方法。 But hopefully using the above logic I described, you should be able to create something similar yourself. 但是希望使用上述逻辑,您应该可以自己创建类似的东西。

Good luck, hope this helps!! 祝你好运,希望这会有帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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