【问题标题】:Swift ---UIView addSubView(UItextView),nothing can be displayedSwift ---UIView addSubView(UItextView),什么都不能显示
【发布时间】:2016-12-08 04:09:51
【问题描述】:
class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //let attstring = NSMutableAttributedString
    let testTextView = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))

    let attstring = NSMutableAttributedString.init(string: "You can check flights from the past 4 days, or plan 3 days ahead.\n\nTo find out when and where we fly for the rest of the year, please view our Timetable.", attributes: [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 12),NSForegroundColorAttributeName:UIColor.red])

    attstring.setAttributes([NSForegroundColorAttributeName:UIColor.blue], range: NSRange.init(location: attstring.length-10, length: 9))

    print("test test!")
    self.view.addSubview(testTextView)
}

什么都没有显示,这是什么问题!?

【问题讨论】:

    标签: uiview swift3 addsubview


    【解决方案1】:

    在 swift 3 中你应该这样写:

    let testTextView = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
        let attstring = NSMutableAttributedString.init(string: "You can check flights from the past 4 days, or plan 3 days ahead.\n\nTo find out when and where we fly for the rest of the year, please view our Timetable.", attributes: [NSFontAttributeName:UIFont.boldSystemFont(ofSize: 12),NSForegroundColorAttributeName:UIColor.red])
        testTextView.attributedText = attstring
        attstring.setAttributes([NSForegroundColorAttributeName:UIColor.blue], range: NSRange.init(location: attstring.length-10, length: 9))
        self.view.addSubview(testTextView)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多