ios - UITextField 溢出

标签 ios objective-c uiview uitextfield quartz-core

我为 UITextField 创建了一个简单的子类,它按预期工作。我遇到的唯一问题是当文本值变得太大时,它会溢出到清除按钮中。

enter image description here

我似乎无法找到如何仅更改文本的右侧以具有一些填充而不与清除按钮相交的方法。

#import "TextField.h"
#import <QuartzCore/QuartzCore.h>

IB_DESIGNABLE

@implementation TextField

- (void)awakeFromNib {
    self.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor];
    [self.layer setBorderWidth:0.6];
    self.layer.cornerRadius = 4;
    self.layer.masksToBounds = YES;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 12, 0);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}

@end

我已经设法移动了文本的两侧,因此它不会溢出到按钮中,但是左侧看起来很奇怪,因为它有额外的间距。如何只在文本的右侧或清除按钮的左侧添加填充?

最佳答案

只需将您的矩形移动一点。

- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width - 10, bounds.size.height);
}

通过扩展,您的编辑矩形也应该更新,因为您将它基于 textRect。

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

相关文章:

objective-c - 导出带有淡入淡出音频的 QTMovie 的最佳方式

ios - 多个 UITableViews 在一个 UIViewController 中一次可见

ios - Swift @IBDesignable/@IBInspectable UIView 样式

ios - 在 UIView 中画线

ios - AVAudioEngine.connect 在硬件而非模拟器上崩溃

android - Xamarin Studio 可以导出 native 库吗

ios - 为什么状态栏与半透明导航栏混在一起

objective-c - 对象何时获得非临时的 objectID?

ios - Box2D:如何使用 b2ChainShape 制作带有正方形的基于图 block 的 map

iOS 在辅助方法中初始化并返回一个新对象