ios - 关于 NSAttributes,我如何知道有哪些属性

标签 ios swift

今天,我想以编程方式更改 TextField 占位符的文本颜色。我找到了一个答案来实现它,如下所示:

 let placeholder = NSAttributedString(string: "(\(i),\(j))", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
 tf.attributedPlaceholder = placeholder

当我成功实现我的目标时,我开始考虑如何知道 UIObject 获得哪些属性,以及我应该对某些特定参数使用哪种 NSAttribute。


例如,我想更改导航栏的标题文本字体和颜色。最终的解决方案是:

navigationController?.navigationBar.titleTextAttributes = 
        [
            NSForegroundColorAttributeName : UIColor.whiteColor(),
            NSFontAttributeName : UIFont(name: "Futura", size: 20)!
        ]

您是否看到有两个不同的属性。首先,它们彼此非常相似,但又有所不同。它们也与第一个示例不同。

这真的让我很困惑!


所以我来这里寻求一些解释,让我再次澄清一下自己:

1.我怎么知道是否有属性可以让我自定义

2.如果我已经知道有任何属性,我如何为其分配正确的属性对象。

If anyone find that I am not clear enough, you can let me know, I will try my best to clear myself :D

感谢您的帮助!

最佳答案

如果我正确解释您的问题,您希望找到可以在 NSAttributedString 上设置的所有属性的文档。

您可以通过按住 Alt 键单击(按住键盘上的 Alt 键并单击)NSForegroundColorAttributeName 并单击底部的引用链接来找到该页面。

描述了每个属性名称,以及它期望的对象类型。

它也可以在 Apple 的 NSAttributedString UIKit Additions 页面上的“字符属性”部分中找到: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html#//apple_ref/doc/constant_group/Character_Attributes

关于ios - 关于 NSAttributes,我如何知道有哪些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33339610/

相关文章:

swift - 无法将字符串保存到 CoreData 中

ios - Core Data 删除同名对象

swift - Swift 中的公共(public)扩展 : Float vs Float. 类型

ios - 如何在uicollectionview中获取当前的cell.contentview。如何在 uicollectionview 单元格中添加 subview

ios - 如何快速实例化具有不同类型的同一通用协议(protocol)的多个实例?

ios - 更改高度时,UITextView 的 textContainer 在错误的帧呈现

ios - 来自 Firebase 的复杂查询

ios - dispatch_data_t 转换为 nsstring

ios - 做一个计算多次的计算器

iphone - 用户界面单元测试?