【发布时间】:2012-06-02 07:30:12
【问题描述】:
我需要在UITextField 中设置有限的文本宽度,因为我将按钮放在了UITextField 之上
看图:
如何做到这一点?
【问题讨论】:
-
这是 UITableViewCell 吗?如果是这样,它有一个简单的方法来使用附件视图。
标签: ios xcode uitextfield width limit
我需要在UITextField 中设置有限的文本宽度,因为我将按钮放在了UITextField 之上
看图:
如何做到这一点?
【问题讨论】:
标签: ios xcode uitextfield width limit
除此之外,您可能希望同时覆盖editingRectForBounds 和textRectForBounds,以便在编辑和不编辑时正确显示文本(请参阅this post)。在 Swift 中,这可能是:
class MyUITextField: UITextField {
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, 18, 0)
}
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectInset(bounds, 18, 0)
}
}
【讨论】:
您可以将 UITextView 的事件editingChanged 链接到以下方法:
- (IBAction)textFieldChanged:(id)sender{
if([sender.text length]>6){
sender.text = [sender.text substringToIndex: 6];
}
}
【讨论】:
您可能需要继承 UITextField 并覆盖 editingRectForBounds: 方法。尝试这样的事情。当然可以相应地调整值。
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset( bounds , 10 , 10 );
}
如果它可以解决您的问题,请接受此答案。
【讨论】: