iOS   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 垂直居中UITextField的attributionPlaceholder大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前无法在UITextField中垂直对齐一个attributionPlaceholder,我不知道为什么.

这就是我在做的事情:

self.addressBar = [[UITextField alloc] initWithFrame: CGRectMake(...)];
self.addressBar.BACkgroundColor = [UIColor whiteColor];
self.addressBar.attributedPlaceholder = [[NSAttributed@R_197_10495@ng alloc] initWith@R_197_10495@ng:@"Placeholder text"
                                attributes:@{
                                             NSForegroundColorAttributename: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],NSFontAttributename : [UIFont fontWithName:@"Gotham-BookItalic" size:14.0],}
 ];
self.addressBar.delegate = self;
self.addressBar.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:self.addressBar];

以下是发生的事情:

很明显,这是因为UITextFieldLabel的高度比UItextField本身小10px,但我似乎无法改变它.

这只发生在使用attributionPlaceholder时;认的占位符属性工作得很好.

有任何想法吗?

解决方法

使用NSParagraphStyle增加最小行高:
NSMutableParagraphStyle *style = [self.addressBar.defaultTextAttributes[NSParagraphStyleAttributename] mutableCopy];
style.minimumLineHeight = self.addressBar.font.lineHeight - (self.addressBar.font.lineHeight - [UIFont fontWithName:@"Gotham-BookItalic" size:14.0].lineHeight) / 2.0;

self.addressBar.attributedPlaceholder = [[NSAttributed@R_197_10495@ng alloc] initWith@R_197_10495@ng:@"Placeholder text"
                            attributes:@{
                                         NSForegroundColorAttributename: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],NSParagraphStyleAttributename : style
                                         }
 ];

您还可以覆盖 – (CGRect)placeholderRectForBounds:(CGRect)边界; UITextField子类中的方法.

它很乱,但它的工作原理:)

大佬总结

以上是大佬教程为你收集整理的ios – 垂直居中UITextField的attributionPlaceholder全部内容,希望文章能够帮你解决ios – 垂直居中UITextField的attributionPlaceholder所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。