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}

];

你可能感兴趣的:(iOS-textField等placeholder 对齐方式)