gpt4 book ai didi

ios - UITextView 中的 UITextField?

转载 作者:行者123 更新时间:2023-11-30 13:17:03 31 4
gpt4 key购买 nike

有没有办法在 UITextView 内的 NSAttributedString 范围内附加 UITextField?

我想重新创建这样的东西:

enter image description here

用户可以按空行来输入文本。

最佳答案

您可以像这样计算 UITextField 的框架:

- (CGRect)textFieldFrameForCharacterRange:(NSRange)charRange inTextView:(UITextView *)textView {
NSRange glyphRange = [textView.layoutManager glyphRangeForCharacterRange:charRange actualCharacterRange:NULL];
CGRect boundingRect = [textView.layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textView.textContainer];
return boundingRect;
}

然后将其添加为 UITextView 的 subview 。确定要传递给上述方法的字符范围也很容易 - 只需搜索一系列下划线,或者使用 - enumerateAttribute:inRange:options:usingBlock: 搜索自定义属性。

问题是,如果下划线被分成多行,则此方法将无法正常工作,但 UITextField 无论如何都不适合这种情况。因此,您需要一种方法来确保下划线保持不间断。 U+2060 unicode 字符可能会有所帮助,请参阅 Prevent line break in a NSAttributedString了解更多。下划线表示为图像的 NSTextAttachment 也可以保证不会自动换行。

或者,您可以实现一个UITextViewDelegate,甚至可能是一个UITextView子类,它不允许在下划线范围之外进行选择和编辑。这样您就可以避免 UITextView 中的 UITextField 出现奇怪的情况。但这种方法的缺点是,即使用户每次键入字符时删除下划线字符,用户仍然会看到下划线的整体长度略有波动。

关于ios - UITextView 中的 UITextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38110906/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com