将目标c代码添加到swift文件中

时间:2016-05-02 19:29:08

标签: ios objective-c swift

我需要以某种方式将以下Objective-c代码添加到AppDelegate.swift文件中。

    // Define some colors.
    UIColor *darkGray = [UIColor darkGrayColor];
    UIColor *lightGray = [UIColor lightGrayColor];

    // Navigation bar background.
    [[UINavigationBar appearance] setBarTintColor:darkGray];
    [[UINavigationBar appearance] setTintColor:lightGray];

    // Color of typed text in the search bar.
    NSDictionary *searchBarTextAttributes = @{
        NSForegroundColorAttributeName: lightGray,
        NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]
    };
    [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
    .defaultTextAttributes = searchBarTextAttributes;

    // Color of the placeholder text in the search bar prior to text entry.
    NSDictionary *placeholderAttributes = @{
        NSForegroundColorAttributeName: lightGray,
        NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]
    };

    // Color of the default search text.
    // NOTE: In a production scenario, "Search" would be a localized string.
    NSAttributedString *attributedPlaceholder =
        [[NSAttributedString alloc] initWithString:@"Search"
    attributes:placeholderAttributes];
    [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
    .attributedPlaceholder = attributedPlaceholder;

最佳解决方案是什么?我应该只创建Objective-C文件还是标题?

1 个答案:

答案 0 :(得分:0)

这是对Swift的粗略翻译......没有经过测试。

// Define some colors.
var darkGray: UIColor = UIColor.darkGrayColor()
var lightGray: UIColor = UIColor.lightGrayColor()
// Navigation bar background.
UINavigationBar.appearance().barTintColor = darkGray
UINavigationBar.appearance().tintColor = lightGray
    // Color of typed text in the search bar.
var searchBarTextAttributes: [NSObject : AnyObject] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
    // Color of the placeholder text in the search bar prior to text entry.
var placeholderAttributes: [NSObject : AnyObject] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]

   // Color of the default search text.
    // NOTE: In a production scenario, "Search" would be a localized string.
var attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Search", attributes: placeholderAttributes)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder