2012-07-09 32 views
5

Tôi sẽ cần hiển thị UISearchBar theo các căn bản khác nhau vì các ngôn ngữ nhập khác nhau.
Tôi đã thấy this answer, nhưng tôi không thể tìm thấy nơi bạn thực sự căn chỉnh văn bản ở bên phải trên UISearchBar.Có thể thay đổi căn chỉnh của trình giữ chỗ của UISearchBar không?

Bất kỳ ý tưởng nào?
Cảm ơn!

+0

Đối Swift 3. Tôi tìm thấy một giải pháp ở đây: [* * Tùy chỉnh textfield dễ dàng **] (http://stackoverflow.com/a/40105165/4593553) – Jerome

Trả lời

6

Đây là những gì tôi đã thực hiện:

for (UIView *subView in self.subviews){ 
     if ([subView isKindOfClass:[UITextField class]]) { 
      UITextField *text = (UITextField*)subView; 
      text.textAlignment = UITextAlignmentRight; 
      text.rightViewMode = UITextFieldViewModeAlways; 
     } 
    } 

Nếu bạn cũng muốn di chuyển các biểu tượng kính lúp, bạn có thể làm điều này:

text.leftView = nil; 
text.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_icon.png"]]; 

Nhưng bạn sẽ phải cung cấp các search_icon .png hình ảnh.

+0

Btw, để làm việc này với iOS 7 bạn cần: [[self.subviews objectAtIndex: 0] subviews] trong cho vòng lặp thay vì chỉ tự.subviews –

3

Dưới đây là giải pháp

UITextField *searchTextField = [searchBar valueForKey:@"_searchField"]; 
UILabel *placeholderLabel = [searchTextField valueForKey:@"_placeholderLabel"]; 
[placeholderLabel setTextAlignment:NSTextAlignmentLeft]; 
+0

tôi nhận được một lỗi "dự kiến]" trên dòng thứ hai – SleepsOnNewspapers

+1

Có một đại tràng bị mất tích, vui lòng đặt tht. –

+1

Đã cập nhật câu trả lời! –

0

Set do văn bản giữ chỗ để đạt được trái thẳng hàng giữ chỗ, hãy thử mã này ...

//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; 
Các vấn đề liên quan