KVC的妙用之UIAlertController的UIAlertAction外观改变

最近赶项目,也遇到了很多,无论是技术还是与同事的交往,感触颇深:1.不要理想的认为你方便了对方,对方就会给你方便;2.程序猿要注重沟通能力;3.学会制作开发计划,复杂的事情要分组来做;4。要学会整理项目
好了,还是切入今天的主题:大家对于KVC和KVO都是很熟悉,但是真正用的时候却不知道从何入手:测试提出个问题:我想把这个文字靠左(如下图)
请问大家如何做:很多人的第一感觉就是去API里面看,可是一看傻眼了,没有属性啊,瞬间崩溃要死的感觉。
聪明的人会根据今天的标题答出:使用KVC。可是到底怎么用,没有暴漏熟悉我怎么根据KVC来做呢?万事不怕有心人,咱们照样有办法,会打断点不,打个断点不就出来了。
没错你没有看错,咱们的首页图片就是断点出来的,所谓八仙过海,咱们就是这样搞到的。
alertView = UIAlertController(title: "controller", message: "测试小例子", preferredStyle: UIAlertControllerStyle.ActionSheet)
var actionTitles = ["1","2"]
for actionTitle in actionTitles {
var action = UIAlertAction(title: actionTitle, style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) -> Voidin
})
self.alertView!.addAction(action)
}
presentViewController(self.alertView!, animated: true) { () -> Voidin
}
下面咱们先让它的文字靠左:
看一下文字对齐的方式:
enum NSTextAlignment : Int {
case Left // Visually left aligned
case Center // Visually centered
case Right // Visually right aligned
/* !TARGET_OS_IPHONE */
// Visually right aligned
// Visually centered
case Justified // Fully-justified. The last line in a paragraph is natural-aligned.
case Natural // Indicates the default alignment for script
}
那么咱么需要Left
action.setValue(NSNumber(integer:NSTextAlignment.Left.rawValue), forKeyPath: "titleTextAlignment")
下面看一下,奇迹出现了:
咱们用同样的方法是文字变为其他颜色
action.setValue(UIColor.greenColor(), forKeyPath: "titleTextColor")
我们来看一下整体效果