iphone - UITextField边缘的黑色像素

标签 iphone ios uitextfield

我使用以下代码以编程方式创建了一个UITextField:

self._maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
self._maxPriceField.borderStyle     = UITextBorderStyleRoundedRect;
self._maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
self._maxPriceField.font            = fieldFont;
self._maxPriceField.delegate        = self;

我遇到的问题是我的UITextField最终在边缘上有这些奇怪的黑色像素。这在设备和模拟器中都会发生。您可以在下面的屏幕截图中看到:

当我使用IB创建具有相同规格和背景的相同UITextField时,我没有问题。不幸的是,我需要以编程方式创建此UITextField。

有人看过吗?该怎么办?

最佳答案

似乎这是在屏幕上绘制文本字段的方式。我稍微玩了一下您的代码,如果我将文本字段的高度设置为低于20左右,您就会开始看到明显的“阴影”,显然没有正确绘制。

我的建议是为文本字段使用20或更高的高度,或者使用其他样式(例如“边框”或“线条”)并将背景设置为白色。

这是演示的屏幕截图:

这是我用来绘制这些代码的代码:

int labelWidth = 100;
int labelHeight = 10;

_maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, labelWidth, labelHeight)];
_maxPriceField.borderStyle     = UITextBorderStyleRoundedRect;
_maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;

//_maxPriceField.font            = fieldFont;
//_maxPriceField.delegate        = self;
[self.view addSubview:_maxPriceField];

UITextField *secondField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, labelWidth, labelHeight + 10)];
secondField.borderStyle     = UITextBorderStyleRoundedRect;
[self.view addSubview:secondField];
[secondField release];

UITextField *thirdField = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, labelWidth, labelHeight + 20)];
thirdField.borderStyle     = UITextBorderStyleRoundedRect;
[self.view addSubview:thirdField];
[thirdField release];

UITextField *fourthField = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, labelWidth, labelHeight + 30)];
fourthField.borderStyle     = UITextBorderStyleRoundedRect;
[self.view addSubview:fourthField];
[fourthField release];

UITextField *noRoundFirst = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, labelWidth, labelHeight)];
noRoundFirst.borderStyle = UITextBorderStyleBezel;
noRoundFirst.backgroundColor = [UIColor whiteColor];
[self.view addSubview:noRoundFirst];
[noRoundFirst release];

希望这可以帮助。

Mk

关于iphone - UITextField边缘的黑色像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5249615/

相关文章:

iphone - UIImageView 对比 UIView 的 drawRect 显示图片

ios - 如何访问项目 > build设置 > 其他 Swift 标志中定义的值?

ios - 在不禁用上下文菜单的情况下管理 UITextfiled 上的长按?

ios - 禁止用户在编辑文本字段时与 UI 交互(键盘除外)

ios - UITextField 在执行字符限制的同时附加/在日期之间

iphone - 如何在 ios 中锁定文本字段和图像?

iphone - 为什么我的 CLLocationmanager 委托(delegate)没有被调用?

iphone - 按手机代数划分的 iPhone 用户分割

ios - UITableViewController 的圆角

ios - Cocos2d 纵向模式下的偏移问题