我想把一些文本放在CGRect的中心位置,该图像位于该图像的顶部。以下是一段代码:使用Swift的drawInRect文本对齐
let textColor: UIColor = UIColor.whiteColor()
let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 17
)!
let textAlignment = NSTextAlignment.Center
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
NSTextAlignment: textAlignment
]
myString.drawInRect(rect, withAttributes: textFontAttributes)
问题在于文本对齐。当我把它写这样我得到一个错误:
Type of expression is ambiguous without more context
当我设置键和值类型[字符串:AnyObject]编译器再次抱怨:
Cannot convert value of type 'NSTextAlignment.Type' to expected dictionary key type 'String'
我理解。我研究了这两个小时,并没有找到任何最新的解决方案,而不是单一的云如何使用Swift写这个。