代碼如下
<pre>
<code>
`
import UIKit
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let screen_w = UIScreen.mainScreen().bounds.width
let screen_h = UIScreen.mainScreen().bounds.height
//設(shè)置scrollView
let theScrollView = UIScrollView()
theScrollView.showsVerticalScrollIndicator = true//scrollView顯示縱向滾動條
theScrollView.showsHorizontalScrollIndicator = false//scrollView不顯示橫向滾動條
theScrollView.bounces = false//scrollView滾動到邊界沒有彈跳緩沖效果
theScrollView.layer.borderColor = UIColor.greenColor().CGColor//邊框顏色
theScrollView.layer.borderWidth = 2//邊框?qū)挾? theScrollView.frame = CGRectMake(10, 20, screen_w-20, screen_h-20)
//用label承載文本內(nèi)容即可
let highLabel = UILabel()
highLabel.backgroundColor = UIColor.yellowColor()//文本背景色
highLabel.textColor = UIColor.redColor()//文本字體顏色
highLabel.numberOfLines = 0//設(shè)置label多行顯示
highLabel.font = UIFont.systemFontOfSize(35)//文本字體大小
highLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail//設(shè)置換行模式
highLabel.text = " Swift 是一種新的編程語言,用于編寫 iOS 和 OS X 應(yīng)用。Swift 結(jié)合了 C 和 Objective-C 的優(yōu)點并且不受C兼容性的限制满俗。Swift 采用安全的編程模式并添加了很多新特性,這將使編程更簡單园细,更靈活,也更有趣接校。Swift 是基于成熟而且倍受喜愛得 Cocoa 和 Cocoa Touch 框架猛频,他的降臨將重新定義軟件開發(fā)。"
+ "\n Swift 的開發(fā)從很久之前就開始了蛛勉。為了給 Swift 打好基礎(chǔ)鹿寻,蘋果公司改進(jìn)了編譯器,調(diào)試器和框架結(jié)構(gòu)诽凌。我們使用自動引用計數(shù)(Automatic Reference Counting, ARC)來簡化內(nèi)存管理毡熏。我們在 Foundation 和 Cocoa的基礎(chǔ)上構(gòu)建框架棧并將其標(biāo)準(zhǔn)化。Objective-C 本身支持塊侣诵、集合語法和模塊痢法,所以框架可以輕松支持現(xiàn)代編程語言技術(shù)。正是得益于這些基礎(chǔ)工作杜顺,我們現(xiàn)在才能發(fā)布這樣一個用于未來蘋果軟件開發(fā)的新語言财搁。"
+ "\n Objective-C 開發(fā)者對 Swift 并不會感到陌生。它采用了 Objective-C 的命名參數(shù)以及動態(tài)對象模型哑舒,可以無縫對接到現(xiàn)有的 Cocoa 框架妇拯,并且可以兼容 Objective-C 代碼幻馁。在此基礎(chǔ)之上洗鸵,Swift 還有許多新特性并且支持過程式編程和面向?qū)ο缶幊獭?
//MARK:動態(tài)計算label高度
//設(shè)置highLabel的最大寬和高
let highLabelMaxSize = CGSizeMake(screen_w-20, 9999)
//計算highLabel的實際frame
let realSize = highLabel.sizeThatFits(highLabelMaxSize)//根據(jù)highLabel的最大寬和高計算highLabel實際尺寸
highLabel.frame = CGRectMake(0, 0, realSize.width,realSize.height)
//計算scrollview的內(nèi)容部分寬度越锈,高度
theScrollView.contentSize = CGSizeMake(highLabel.frame.width , highLabel.frame.height)
//在scrollview上添加label
theScrollView.addSubview(highLabel)
//在頁面中添加scrollview
self.view.addSubview(theScrollView)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
`
</code>
</pre>