6. 一种UITextField样式

原因:

想做个这样的东西:

解决:

找了半天(其实只有10分钟)只有placeholder,

于是笨办法,利用leftView,写一个自己的TextField

@interface TitleTextField : UITextField

然后顺理成章

- (void) title:(NSString*)title
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = title;
    label.font = [UIFont systemFontOfSize:16];
    [label sizeToFit];
    [label setWidth:label.width+20];
    
    self.leftView = label;
    self.leftViewMode = UITextFieldViewModeAlways;
}

- (CGRect) leftViewRectForBounds:(CGRect)bounds {
    
    CGRect textRect = [super leftViewRectForBounds:bounds];
    textRect.origin.x += 10;
    return textRect;
}

要说的话:

直觉应该有更clean的方法,但我太懒了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容