美文网首页
2021-11-09 UITextfield文字,占位符,光标居

2021-11-09 UITextfield文字,占位符,光标居

作者: cc帅气的昵称 | 来源:发表于2021-11-16 14:55 被阅读0次

#1.文字居中

self.TF.textAlignment = NSTextAlignmentCenter;

#2.占位符居中

NSMutableParagraphStyle *paragraphStyleR = NSMutableParagraphStyle.new;

paragraphStyleR.alignment = NSTextAlignmentCenter;

self.rightTF.attributedPlaceholder = [NSAttributedString.alloc initWithString:@"高100-1000"

                                                                         attributes:@{NSParagraphStyleAttributeName:paragraphStyleR}];

#3.光标居中

#import"ZRTradeTextField.h"

@interfaceZRTradeTextField:UITextField

@end

#import"ZRTradeTextField.h"

@implementationZRTradeTextField

-(CGRect)placeholderRectForBounds:(CGRect)bounds{

CGRect inset=CGRectMake(bounds.origin.x,bounds.origin.y,bounds.size.width,bounds.size.height);returninset;

}

-(CGRect)textRectForBounds:(CGRect)bounds{

CGRect inset=CGRectMake(bounds.origin.x,bounds.origin.y,bounds.size.width,bounds.size.height);returninset;

}

-(CGRect)editingRectForBounds:(CGRect)bounds{if(self.text.length>0){

return[supereditingRectForBounds:bounds];

}else{// 可通过默认文案来调整inset.origin.x保证光标在两个字之间

CGRect inset=CGRectMake(bounds.origin.x+bounds.size.width/2,bounds.origin.y,bounds.size.width-bounds.size.width/2,bounds.size.height);returninset;}

}

相关文章

网友评论

      本文标题:2021-11-09 UITextfield文字,占位符,光标居

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