UITextField的placehold字体大小是以光标的上端点为起始点的,当设置placehold的font值大于或小于UITextField的font值时placehold不是垂直居中的,垂直居中方法如下:
if ([UIDevice currentDevice].systemVersion.floatValue >= 11) {
_inputtextFiled.placeholder = placehold;
[_inputtextFiled setValue:placeholdFont forKeyPath:@"_placeholderLabel.font"];
} else {
//设置textFiled的段落格式
NSMutableParagraphStyle *style = [_inputtextFiled.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
//设置垂直对齐
style.minimumLineHeight = _inputtextFiled.font.lineHeight - (_inputtextFiled.font.lineHeight - placeholdFont.lineHeight) / 2.0;
//设置placeholder的样式
_inputtextFiled.attributedPlaceholder = [[NSAttributedString alloc]initWithString:placehold attributes:@{NSFontAttributeName:placeholdFont,NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColor hexStringToColor:@"#cccccc"]}];
}
网友评论