由于输入语言不同,我将需要以不同的对齐方式显示 UISearchBar。
我看到了这个答案,但我找不到你实际将文本与UISearchBar
.
有任何想法吗?
谢谢!
由于输入语言不同,我将需要以不同的对齐方式显示 UISearchBar。
我看到了这个答案,但我找不到你实际将文本与UISearchBar
.
有任何想法吗?
谢谢!
这就是我所做的:
for (UIView *subView in self.subviews){
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *text = (UITextField*)subView;
text.textAlignment = UITextAlignmentRight;
text.rightViewMode = UITextFieldViewModeAlways;
}
}
如果您还想移动放大镜图标,可以这样做:
text.leftView = nil;
text.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_icon.png"]];
但是您必须提供 search_icon.png 图像。
这是解决方案
UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
UILabel *placeholderLabel = [searchTextField valueForKey:@"_placeholderLabel"];
[placeholderLabel setTextAlignment:NSTextAlignmentLeft];
要设置属性占位符文本以实现左对齐占位符,请尝试此代码...
//Step1 : Make blank attributed string
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
//Step2 : Append placeholder to blank attributed string
NSString *strSearchHere = @"Search here...";
NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
NSAttributedString * subString = [[NSAttributedString alloc] initWithString:strSearchHere attributes:attributes];
[attributedString appendAttributedString:subString];
//Step3 : Append dummy text to blank attributed string
NSString *strLongText = @"LongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongText";
NSDictionary * attributesLong = [NSDictionary dictionaryWithObject:[UIColor clearColor] forKey:NSForegroundColorAttributeName];
NSAttributedString * subStringLong = [[NSAttributedString alloc] initWithString:strLongText attributes:attributesLong];
[attributedString appendAttributedString:subStringLong];
//Step4 : Set attributed placeholder string to searchBar textfield
UITextField *searchTextField = [searchBarr valueForKey:@"_searchField"];
searchTextField.attributedPlaceholder = attributedString;
searchTextField.leftView = nil; //To Remove Search icon
searchTextField.textAlignment = NSTextAlignmentLeft;