【问题标题】:How to use custom UITextField in storyboards如何在故事板中使用自定义 UITextField
【发布时间】:2015-08-27 00:38:05
【问题描述】:

请让我知道如何在我的UIViewController 中使用此自定义UITextField

我正在使用故事板。

#import <UIKit/UIKit.h>

@interface MYTextField : UITextField

@end


@implementation MYTextField

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

- (CGRect)editingRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
    return inset;
}

【问题讨论】:

  • 这应该不难回答,但我不明白你到底想做什么?你在使用故事板吗?在发布代码之前编辑您的问题并留出 4 个空格,使其更具可读性。
  • 是的,我正在使用故事板,我想在我的文本字段中应用左边距,我遵循此代码但我无法实现此。stackoverflow.com/questions/5674655/…

标签: ios objective-c ios7


【解决方案1】:

您必须使用自定义的MYTextField,而不是使用普通的UITextField

在代码中:

MYTextField *myTextField = [[MYTextField alloc] init];

如果您使用界面生成器:

在您的 Interface Builder 文件中选择您的 UITextField,转到 Identity Inspector 并设置正确的子类:

【讨论】:

    【解决方案2】:
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
    textField.leftView = paddingView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    

    此代码在文本字段的左侧添加一些边距。 试试这个对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 2023-03-08
      • 2017-12-06
      相关资源
      最近更新 更多