#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;}
}
网友评论