// 1.創建Lable var lbl:UILabel? = UILabel() lbl!.text = app!.name! + "正在下載……" lbl!.textAlignment = NSTextAlignment.Center; lbl!.textColor = UIColor.whiteColor() lbl!.font = UIFont.systemFontOfSize(13) // 2.通過Lable的文字獲取自適應區域 var rect:CGRect = lbl!.text!.boundingRectWithSize(CGSize(width: 260.0 ,height: 10000.0), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:lbl!.font], context: nil) // 3.設置Label的frame var marginBottom:CGFloat = 100 // 3.1.根據剛才的所獲取到自適應寬高加上適當值來設置lable的寬高 var width:CGFloat = rect.width + 48 var height:CGFloat = rect.height + 12 var x = (self.superview!.frame.width - width) / 2 var y = self.superview!.frame.height - marginBottom lbl!.frame = CGRect(x: x, y: y, width: width, height: height) // 4設置lable背景圓角 // 4.1.設置圓角圖層背景顏色 lbl!.layer.backgroundColor = UIColor.blackColor().CGColor // 4.2.設置圓角值 lbl!.layer.cornerRadius = 5
//該處應該用lbl.clip^方法。切掉超出layer的的區域
lbl!clipsToBounds = true
self.superview?.addSubview(lbl!) // 5.實現動畫移除控件 UIView .animateWithDuration(5, animations: { () -> Void in lbl!.alpha = 0.7 lbl!.alpha = 0 }) { (Bool) -> Void in lbl!.removeFromSuperview() lbl = nil }