我需要一个文本框,需要允许用户键入文本到文本框和文本内的文本框边界停留。如果用户输入了太多的文本,它会减小字体,以适应文本框的范围。我不知道哪个对象将能够满足这个要求。
xytpbqjk1#
我假设您正在使用UITextField(正如您所说的,它是一个文本框)。因此,您可以使用以下内容
[yourTextField setMinimumFontSize:7.0]; [yourTextField setAdjustsFontSizeToFitWidth:YES];
sirbozc52#
有趣的是,我()只是()实现了这一点。我是这样做的:
- (void)fitTextView { CGFloat height = [[UIScreen mainScreen] bounds].size.height - 16; _fontSize = 96; _text.font = [UIFont fontWithName:_fontName size:_fontSize]; while (height < _text.contentSize.height) { if (_fontSize < 20) { break; } _fontSize -= 0.5; _text.font = [UIFont fontWithName:_fontName size:_fontSize]; } while (height > _text.contentSize.height) { if (_fontSize > 96) { break; } _fontSize += 0.5; _text.font = [UIFont fontWithName:_fontName size:_fontSize]; } } - (void)textViewDidChange:(UITextView *)textView { [self fitTextView]; }
不过,我的代码有一个小问题,如果输入速度足够快,输入的文本会反弹。我很想得到一些关于如何解决这个问题的反馈。
多一点代码。
_fontSize = 96; _fontName = @"Helvetica"; _text = [[UITextView alloc] initWithFrame:self.view.bounds]; _text.autocapitalizationType = UITextAutocapitalizationTypeNone; _text.autocorrectionType = UITextAutocorrectionTypeNo; _text.delegate = self; _text.text = @""; _text.font = [UIFont fontWithName:_fontName size:_fontSize]; _text.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; _text.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; [self.view addSubview:_text];
并记住声明您的类实现了UITextViewDelegate协议。
UITextViewDelegate
2条答案
按热度按时间xytpbqjk1#
我假设您正在使用UITextField(正如您所说的,它是一个文本框)。
因此,您可以使用以下内容
sirbozc52#
有趣的是,我()只是()实现了这一点。我是这样做的:
不过,我的代码有一个小问题,如果输入速度足够快,输入的文本会反弹。我很想得到一些关于如何解决这个问题的反馈。
编辑
多一点代码。
并记住声明您的类实现了
UITextViewDelegate
协议。