Swift 中的 UIControlState
UIControlState in Swift
这是我的代码:
let font = UIFont(name: "AvenirNext-Regular", size: 16.0)
let attributes: NSDictionary? = [ NSFontAttributeName : font! ]
self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)
我遇到错误“在最后一行找不到成员 'Normal'。怎么了?
更改代码的以下行:
self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)
对此:
self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.normal)
或者您也可以使用以下方法:
self.segmentedControl.setTitleTextAttributes(attributes as? [AnyHashable : Any], forState: UIControlState.normal)
您需要将 attributes
转换为 [NSObject : AnyObject]
,您的代码将是:
segmentedControl.setTitleTextAttributes(attributes as [NSObject : AnyObject]?, forState: .Normal)
这是 Apple Docs 的默认语法:
func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?, forState state: UIControlState)
或者您可以在创建时将 attributes
转换为 [NSObject : AnyObject]?
,代码代码为:
let font = UIFont(name: "AvenirNext-Regular", size: 16.0)
let attributes: [NSObject : AnyObject]? = [ NSFontAttributeName : font! ]
self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
您的代码应该可以正常工作。您只需重新启动 xcode.
试吹代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var segment: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let font = UIFont(name: "AvenirNext-Regular", size: 26.0)
let attributes: NSDictionary? = [ NSFontAttributeName : font! ]
segment.setTitleTextAttributes(attributes! as [NSObject : AnyObject], forState:.Normal)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我的代码:
let font = UIFont(name: "AvenirNext-Regular", size: 16.0)
let attributes: NSDictionary? = [ NSFontAttributeName : font! ]
self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)
我遇到错误“在最后一行找不到成员 'Normal'。怎么了?
更改代码的以下行:
self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)
对此:
self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.normal)
或者您也可以使用以下方法:
self.segmentedControl.setTitleTextAttributes(attributes as? [AnyHashable : Any], forState: UIControlState.normal)
您需要将 attributes
转换为 [NSObject : AnyObject]
,您的代码将是:
segmentedControl.setTitleTextAttributes(attributes as [NSObject : AnyObject]?, forState: .Normal)
这是 Apple Docs 的默认语法:
func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?, forState state: UIControlState)
或者您可以在创建时将 attributes
转换为 [NSObject : AnyObject]?
,代码代码为:
let font = UIFont(name: "AvenirNext-Regular", size: 16.0)
let attributes: [NSObject : AnyObject]? = [ NSFontAttributeName : font! ]
self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
您的代码应该可以正常工作。您只需重新启动 xcode.
试吹代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var segment: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let font = UIFont(name: "AvenirNext-Regular", size: 26.0)
let attributes: NSDictionary? = [ NSFontAttributeName : font! ]
segment.setTitleTextAttributes(attributes! as [NSObject : AnyObject], forState:.Normal)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}