iOS-textField等placeholder 对齐方式

是不是 经常遇到placeholder 怎么设置对齐方式都是靠上?


UITextField*textField = [[UITextFieldalloc]initWithFrame:CGRectMake(100,100,200,50)];

textField.placeholder=@"搜索附近的办公楼、小区、商业街...";

textField.backgroundColor= [UIColorwhiteColor];

[textFieldsetValue [UIColorcolorWithWhite:0.800alpha:1.000]forKeyPath:@"_placeholderLabel.textColor"];

[textFieldsetValue:[UIFontboldSystemFontOfSize:14]forKeyPath:@"_placeholderLabel.font"];3

textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;

[self.viewaddSubview:textField];





这样解决:

使用 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 = [[NSAttributedString alloc] initWithString:@"Placeholder text" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],NSParagraphStyleAttributeName : style}

];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容