无法让UITextField自动收缩文本 [英] Can't get UITextField to autoshrink text

查看:85
本文介绍了无法让UITextField自动收缩文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格视图单元格上有一个 UITextField ,当它的文字变得太长时,我希望字体大小减小。我想说清楚我说的是 UITextField ,而不是 UILabel 的UITextView 。我说这个的原因是因为我看到这个问题多次弹出,答案都是基于 UILabel 而不是 UITextField 。例如,有人问我无法让我的 UITextField 自动收缩,答案是确保它是 numberOfLines 设置为 1 。据我所知, UITextField 甚至没有该属性,只是一个单行控件。

I have a UITextField on a table view cell, and when it's text becomes too long I would like the font size to decrease. I want to make it very clear that I am talking about a UITextField, not a UILabel or a UITextView. The reason I say this is because I have seen this question pop up several times and the answers were all based on UILabel instead of UITextField. For example, someone asked "I can't get my UITextField to autoshrink" and the answer was "make sure it's numberOfLines is set to 1". To the best of my knowledge, a UITextField does not even have that property and is a single line control.

我试过:


  • 在IB中将字体设置为系统14.0, minFontSize 到7,并检查中的代码中的调整以适合框

  • tableView:cellForRowAtIndexPath:

  • in IB setting the font to system 14.0, minFontSize to 7 and checking the "adjust to fit" box
  • in code in tableView:cellForRowAtIndexPath:
ptCell.name.font = [UIFont systemFontOfSize: 14.0];
ptCell.name.adjustsFontSizeToFitWidth = YES;
ptCell.name.minimumFontSize = 7.0;

但这些都没有奏效。我的意思是,它不是缩小文本而是截断尾部。

but neither of these have worked. By that I mean that instead of the text shrinking it truncates the tail.

有人知道我错过了什么吗?据推测,这应该有效,因为我看到其他问题抱怨它是在用户不想要的时候这样做。

Does anyone know what I am missing? Presumably this should work because I have seen other questions complaining that it is doing that when the user does not want it to.

推荐答案

我使用@Purva发布的答案作为起点来提出这种方法,该方法从配置的字体大小开始提供所需的字体大小,而不是低于配置的最小字体大小。而@Purva测试文本的高度,我需要宽度适合。此方法可以放在UITextField的类别或子类中。我在一个子类中也有它,它也捕获 UITextFieldTextDidChangeNotification 。从此通知处理程序中,我调用新方法并根据需要调整字体大小。当我将新文本分配给文本字段以确保它适合通过子类 - (void)setText:(NSString *)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];
    }

以下是新方法:

- (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 );
}

这篇关于无法让UITextField自动收缩文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆