是不是 經(jīng)常遇到placeholder 怎么設(shè)置對齊方式都是靠上胖替?
UITextField*textField = [[UITextFieldalloc]initWithFrame:CGRectMake(100,100,200,50)];
textField.placeholder=@"搜索附近的辦公樓目木、小區(qū)闸婴、商業(yè)街...";
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}
];