Swift - UILable背景圖層圓角,Lable大小隨文字自適應


        // 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM