美文网首页
UITextField 的Placeholder颜色字体、光标颜

UITextField 的Placeholder颜色字体、光标颜

作者: 木_穆 | 来源:发表于2019-08-13 21:09 被阅读0次

设计师和UITextField干上了,UITextField的原有格式设计大佬不满意。所以要各种魔改。
现记录一下魔改内容。(觉得写得有帮助的话不用赞赏,❤️就好)😎。
先看效果


Untitled.gif

修改TextField光标颜色不需要继承创建子类,其他创建子类重写方法

1 修改TextField光标颜色位置大小

重写 - (CGRect)caretRectForPosition:(UITextPosition *)position;方法

#define Flog_H 6  //比原来光标长度短多少
#define Flog_W 3
#define Flog_M 2  //距离文字的间距原来

- (CGRect)caretRectForPosition:(UITextPosition *)position{
    CGRect originalRect = [super caretRectForPosition:position];
    originalRect.size.height = originalRect.size.height - Flog_H;//新光标高度
    originalRect.size.width = Flog_W; //新光标宽度
    //计算光标位置
    originalRect.origin.x = originalRect.origin.x + Flog_M;
    originalRect.origin.y = originalRect.origin.y + ceil(Flog_H/2);
    return originalRect;
}
2 修改TextField光标颜色

代码修改:

修改textField.tintColor = newcolor;即可;

xib如下图:


修改TextField光标颜色
3 修改Placeholder颜色字体

重写 - (void)drawPlaceholderInRect:(CGRect)rect ;方法

- (void)drawPlaceholderInRect:(CGRect)rect {
    // NSMutableParagraphStyle
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = self.textAlignment; // 居中  不设置默认居左
    
    NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName : _placeholderColor?_placeholderColor:PlaceholderColor,//不设置默认位70%灰
                                 NSFontAttributeName : self.font,//也可以设置为其他你想要字体,我用的是和输入一样的字体
                                 NSParagraphStyleAttributeName : paragraph
                                 };


    // 计算位置
    CGSize textSize = [self.placeholder sizeWithAttributes:attributes];
    CGFloat hdif = rect.size.height - textSize.height;
    hdif = MAX(0, hdif);
    rect.origin.y += ceil(hdif/2.0);
    [[self placeholder] drawInRect:rect withAttributes:attributes];
}
4 修改Placeholder位置

重写 - (CGRect)placeholderRectForBounds:(CGRect)bounds;方法


- (CGRect)placeholderRectForBounds:(CGRect)bounds{
    CGRect originalRect = [super placeholderRectForBounds:bounds];
   
    originalRect.origin.x = originalRect.origin.x +20;
  
    return originalRect ;
}
  • (CGRect)textRectForBounds:(CGRect)bounds
5 修改text位置

重写 - (CGRect)textRectForBounds:(CGRect)bounds方法


- (CGRect)textRectForBounds:(CGRect)bounds{
    CGRect originalRect = [super placeholderRectForBounds:bounds];
   
    originalRect.origin.x = originalRect.origin.x +20;
  
    return originalRect ;
}
6 Placeholder和text使用不一样对齐方式
[_accountTF addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];

//判断是否是空字符串 空字符串
#define  NOEmptyStr(string)  (string == nil || string == NULL ||[string isKindOfClass:[NSNull class]] || [string isEqualToString: @""]  ||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0 ? NO : YES)
#define  IsEmptyStr(string) (!NOEmptyStr(string))


-(void)textChanged:(UITextField *)textField{
    if (IsEmptyStr(textField.text)) {
        textField.textAlignment = NSTextAlignmentCenter;
    }else{
        textField.textAlignment = NSTextAlignmentLeft;
    }
    
}

另外为了xib和storyboard使用方便请看使用“IBInspectable”XIB设置圆角、边框、阴影

相关文章

网友评论

      本文标题:UITextField 的Placeholder颜色字体、光标颜

      本文链接:https://www.haomeiwen.com/subject/dkhijctx.html