如何在Google地方自动填充功能中更改取消按钮文字和字体?

时间:2017-04-17 05:35:42

标签: ios swift google-maps google-places-api uisearchbar

我为自己的应用使用“自动填充功能”。 请帮我。 我想改变:

1 - 取消按钮文字和字体 2 - 主文本字体和辅助文本字体 3 - 错误和消息文本字体 4 - “再试一次”按钮文本和字体 enter image description here

import UIKit
import GooglePlaces

class ViewController: UIViewController {

  // Present the Autocomplete view controller when the button is pressed.
  @IBAction func autocompleteClicked(_ sender: UIButton) {
    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self
    present(autocompleteController, animated: true, completion: nil)
  }
}

extension ViewController: GMSAutocompleteViewControllerDelegate {

  // Handle the user's selection.
  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    print("Place address: \(place.formattedAddress)")
    print("Place attributions: \(place.attributions)")
    dismiss(animated: true, completion: nil)
  }

  func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    // TODO: handle the error.
    print("Error: ", error.localizedDescription)
  }

  // User canceled the operation.
  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
  }

  // Turn the network activity indicator on and off again.
  func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
  }

  func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
  }

}

https://developers.google.com/places/ios-api/autocomplete#add_a_full-screen_control

3 个答案:

答案 0 :(得分:2)

遗憾的是,

GMSAutocompleteViewController不支持更改UI元素的字体。

答案 1 :(得分:0)

在对话框中更改表格视图字体的一种方法是使用Objective c swizzling

 @implementation GMSAutocompleteTableDataSource(ae_custom_font)

-(UITableViewCell *) ae_swizzle_tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell =  [self ae_swizzle_tableView:tableView cellForRowAtIndexPath:indexPath];
    if([(UILabel*)cell.subviews[0].subviews[0] isKindOfClass:[UILabel class]])
    ((UILabel*)cell.subviews[0].subviews[0]).font = [UIFont systemFontOfSize:10];
    return cell;
}

@end

@interface AutoCompleteVC: GMSAutocompleteViewController
@end

@implementation AutoCompleteVC

-(void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    static dispatch_once_t once_token;
    dispatch_once(&once_token,  ^{
        SEL cellForRowSelector = @selector(tableView:cellForRowAtIndexPath:);
        SEL customCellForRowSelector = @selector(ae_swizzle_tableView:cellForRowAtIndexPath:);
        Method originalMethod = class_getInstanceMethod([GMSAutocompleteTableDataSource class], cellForRowSelector);
        Method extendedMethod = class_getInstanceMethod([GMSAutocompleteTableDataSource class], customCellForRowSelector);
        method_exchangeImplementations(originalMethod, extendedMethod);
    });
}

@end

答案 2 :(得分:0)

使用外观代理修改有关GMSAutocompleteViewController的内容

这会将占位符文本更改为黑色,并且将“取消”按钮更改为黑色。

    let blackColorAttribute: [NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.black]
    let attributePlaceHolder = NSAttributedString(string: "Ingrese dirección", attributes: blackColorAttribute)
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = attributePlaceHolder
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(blackColorAttribute, for: .normal)