gpt4 book ai didi

iphone - 重置 UITextField 左边距

转载 作者:行者123 更新时间:2023-12-03 19:32:09 25 4
gpt4 key购买 nike

我想将 UITextField.text 的左边距对齐到 10Px。请建议我最好的方法?与 roundedRect TesxtField 相同,其中文本从左侧开始 10 px

几乎通过覆盖 - (CGRect)textRectForBounds:(CGRect)bounds 来实现。 现在的问题是,当 TextField 进入编辑模式时,它们的左边距重置为零.......

@implementation UITextField(UITextFieldCatagory)

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

最佳答案

你可以试试UITextField的实例方法drawTextInRect:

我认为您可以使用 leftView 属性来实现此目的。

您可以将 leftViewrightView 添加到 UITextfield。这些 View 可用于显示图标,但如果它是空 View ,它只会占用空间,这正是您想要的。

CGFloat leftInset = 5.0f;
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, leftInset, self.bounds.size.height)];
self.leftView = leftView;
self.leftViewMode = UITextFieldViewModeAlways;
[leftView release];

引用SO问题UITextField custom background view and shifting text

关于iphone - 重置 UITextField 左边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5699617/

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