相关疑难解决方法(0)

无法让UITextField自动收缩文本

我有一个UITextField表视图单元格,当它的文本变得太长时,我希望字体大小减少.我想说清楚我说的是a UITextField,而不是a UILabel或a UITextView.我说这个的原因是因为我看到这个问题多次弹出,答案都是基于UILabel而不是UITextField.例如,有人问"我不能让我UITextField自动收缩",答案是"确保它numberOfLines被设置为1".据我所知,a UITextField甚至不具备该属性并且是单行控制.

我试过了:

  • 在IB中将字体设置为系统14.0,minFontSize到7并检查"调整到适合"框
  • 在代码中 tableView:cellForRowAtIndexPath:
ptCell.name.font = [UIFont systemFontOfSize: 14.0];
ptCell.name.adjustsFontSizeToFitWidth = YES;
ptCell.name.minimumFontSize = 7.0;
Run Code Online (Sandbox Code Playgroud)

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

有谁知道我错过了什么?据推测,这应该有效,因为我已经看到其他问题抱怨当用户不想要它时它正在这样做.

iphone objective-c uitextfield ios

19
推荐指数
3
解决办法
2万
查看次数

UITextField AdjustsFontSizeToFitWidth不起作用

在iOS 7 textField.adjustsFontSizeToFitWidth = YES上无效。minimumFontSize也已正确设置。我希望如果文本太长而无法放入textField的框架中,字体大小将减小。但是,实际上发生的是文本被剪切,并且在末尾附加了“ ...”。

我试过了:

  • 设置textField的宽度约束
  • 在包含视图的init方法以外的地方设置textField的字体

有任何想法吗?

编辑:

代码有点复杂,但是这里是:

self.captionView = [[CaptionView alloc] initWithFrame:CGRectMake(0, 0, self.width, 50)];
self.captionView.textField.font = [self.captionView.textField.font fontWithSize:20.f];
self.captionView.textField.leftPadding = 2.f;
self.captionView.textField.rightPadding = 2.f;
self.captionView.textField.adjustsFontSizeToFitWidth = YES;
self.captionView.textField.minimumFontSize = 2.f;
self.captionView.captionViewDelegate = self;
[self addSubview:self.captionView];
Run Code Online (Sandbox Code Playgroud)

CaptionView具有属性textField,它是的子类UITextField

该子类覆盖方法:

 - (void)drawRect:(CGRect)rect;
 - (void) drawPlaceholderInRect:(CGRect)rect;
 - (CGRect) editingRectForBounds: (CGRect) bounds;
 - (CGRect) textRectForBounds: (CGRect) bounds;
Run Code Online (Sandbox Code Playgroud)

编辑:

我只是注释掉了每个替代项,字体仍然没有很好地调整。

编辑:

我发现的最相似的问题在这里。公认的答案是放弃使用adjustsFontSizeToFitWidth

uitextfield ios

5
推荐指数
0
解决办法
4210
查看次数

UITextField子类搞乱adjustFontSizeToWidth

我在视图控制器中创建了一个子类文本视图,字体大小为15,启用了AdjustFontSizeToWidth,最小字体大小为10.我的textfield的占位符文本太大,无法容纳视图.将字体大小调整为宽度,系统将字体大小减小到14.我不知道为什么14,因为占位符仍然不适合(见截图)任何想法为什么会发生这种情况?我错过了什么吗?我将UITextfield子类化并覆盖以下方法(不确定它是否相关,但这似乎是可能导致此错误的方法):

- (CGRect)leftViewRectForBounds:(CGRect)bounds {
CGRect leftViewRect = [super leftViewRectForBounds:bounds];
if ([self shouldShowLeftView] == YES) {
    CGFloat newHeight = self.frame.size.height * 0.5f;
    if (self.leftImage) {
        leftViewRect.size.width = newHeight;
    }
    leftViewRect.origin.x = kMargins;
    leftViewRect.origin.y =  (self.frame.size.height - leftViewRect.size.height) / 2.0f;
 }
 return leftViewRect;
}


- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect rectForBounds = [super textRectForBounds:bounds];

if ([self shouldShowLeftView] == YES) {
    rectForBounds.origin.x += kMargins;
    rectForBounds.size.width -= 2.0 * kMargins;
} else {
    rectForBounds.origin.x += kMargins / 2.0; …
Run Code Online (Sandbox Code Playgroud)

subclass objective-c uitextfield uikit ios

5
推荐指数
1
解决办法
349
查看次数

标签 统计

ios ×3

uitextfield ×3

objective-c ×2

iphone ×1

subclass ×1

uikit ×1