我正在用下面的代码更改占位符文本的 colored颜色 ,但是当我try 添加NSFontAttribute时,我得到了编译器错误"too many arguments to method call, expect 2 have 3"

 UIColor *color = [UIColor blackColor];
        _nameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Name" attributes:@{NSForegroundColorAttributeName: color},@{NSFontAttributeName:@"Roboto-Bold"}]; 

This Works Fine:

 UIColor *color = [UIColor blackColor];
        _nameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Name" attributes:@{NSForegroundColorAttributeName: color}]; 

推荐答案

Objective-C:

UIColor *color = [UIColor blackColor];    
someUITextField.attributedPlaceholder =
  [[NSAttributedString alloc] initWithString:@"Placeholder Text"
    attributes:@{
       NSForegroundColorAttributeName: color,
       NSFontAttributeName : [UIFont fontWithName:@"Roboto-Bold" size:17.0]
    }
  ];

(文本dictionary个键值对之间没有括号.)

Swift:

let attributes = [
    NSForegroundColorAttributeName: UIColor.blackColor(),
    NSFontAttributeName : UIFont(name: "Roboto-Bold", size: 17)! // Note the !
]

someUITextField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes:attributes)

Objective-c相关问答推荐

如何删除Objective-C中未定义的符号&错误?

恢复 iOS7 之前的 UINavigationController pushViewController 动画

iOS:警告try 呈现其视图不在窗口层次 struct 中的 ViewController

isEqualTo: 和 isEqual 之间的区别:

滚动 NSScrollView 时的回调?

右对齐的 UITextField 空格键在 iOS 7 中不前进光标

如何正确获取文件大小并将其转换为 Cocoa 中的 MB、GB?

在 ARC 中清零弱引用

何时使用静态字符串与 #define

didReceiveRemoteNotification:当应用程序处于后台并且未连接到Xcode时未调用fetchCompletionHandler

iOS:使用设备修饰符加载 xib 文件?

更改后退导航栏按钮的字体

使用正则表达式在 Xcode 中查找/替换

具有多个嵌入 segues 的 ContainerView

Lipo错误!!无法打开输入文件

'supportedInterfaceOrientations' 实现中的返回类型冲突: - 警告

动画 UIButton 状态变化

如何将 NSOperationQueue 与 NSURLSession 一起使用?

如何为 iPhone 6/7 自定义边到边图像指定尺寸?

如何在对象内使用 objc_setAssociatedObject/objc_getAssociatedObject?