改变UITextField的font

2018-01-29  本文已影响0人  Desert_Eagle

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"]}];
        }
上一篇 下一篇

猜你喜欢

热点阅读