2016-01-05 150 views
2

我有一個UITextView來顯示文章的標題和內容。這是我的代碼:UITextView初始滾動位置不是0

static func setTextViewRichText(textView: UITextView, html: String, title: String) { 

    do { 
     let titleText = NSAttributedString(string: title + "\n", attributes: [NSFontAttributeName: UIFont(name: "Helvetica", size: 20)!]) 
     let contentText = try NSAttributedString(data: html.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSNumber(unsignedLong: NSUTF8StringEncoding)], documentAttributes: nil) 
     let text = NSMutableAttributedString(attributedString: titleText) 
     text.appendAttributedString(contentText) 
     textView.attributedText = text 

    } catch let err as NSError { 
     NSLog("%s", err) 
     textView.text = title + "\n" + html 
    } 
} 

初始滾動位置不是0,這不是我所期望的。我沒有在代碼中設置任何屬性。

+0

一個UITextView從UIScrollView的繼承,看看這裏:http://stackoverflow.com/questions/9450302/get-uiscrollview-to-scroll-to-the-最佳 –

回答

0

嘗試第一個佈局:

textView.layoutIfNeeded() 
textView.attributedText = text