2016-01-05 85 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