1. 程式人生 > >iOS UIView及其子控制元件的常用設定

iOS UIView及其子控制元件的常用設定

為控制元件新增邊框

 view.layer.borderWidth = 1;
 view.layer.borderColor = [[UIColor blackColor] CGColor];

btn.isEnabled = false
btn.alpha = 0.4



UITextView設定“placeholder”

1.建立

let detailAddTextView = UITextView()
                detailAddTextView.frame = CGRectMake(10, 10, self.myTableView!.frame.size.width - 20, 70)
                detailAddTextView.delegate = self
                detailAddTextView.font = Font(14.0)
                cell?.contentView.addSubview(detailAddTextView)
                //儲存的地址
                detailAddTextView.text = self.details
                self.details = detailAddTextView.text
                //覆蓋label
                coverLabel = UILabel()
                coverLabel!.frame = CGRectMake(detailAddTextView.frame.origin.x + 5, detailAddTextView.frame.origin.y + 8, detailAddTextView.frame.size.width, 20)
                coverLabel?.enabled = false
                if(detailAddTextView.text.length == 0)
                {
                    coverLabel?.text = "請填寫詳細地址,不少於5個字"
                }
                else
                {
                    coverLabel?.text = ""
                }
                
                coverLabel?.textColor = UIColor.lightGrayColor()
                coverLabel?.font = detailAddTextView.font
                cell?.contentView.addSubview(coverLabel!)

2.textView代理方法裡
func textViewDidChange(textView: UITextView)
    {
        if(textView.text?.length == 0)
        {
            coverLabel?.text = "請填寫詳細地址,不少於5個字"
        }
        else
        {
            coverLabel?.text = ""
        }
        self.details = textView.text
    }

UILabel文字大小隨寬度調整

label.adjustsFontSizeToFitWidth=true  
//此處的0.6是縮小到0.6倍就不縮小了,如果還是超過那麼就省略顯示多餘部分  
label3.minimumScaleFactor=0.6 


改變sb或xib中做過約束的控制元件的frame

把約束像控制元件一樣與程式碼關聯

然後程式碼設定 

self.cmyNameWidth.constant = 100

為UITextField新增字數限制並提示

//新增監聽
textField.addTarget(self, action: #selector(InfomationView.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
func textFieldDidChange(textField:UITextField)
    {
        let lang = textInputMode?.primaryLanguage
        if (lang == "zh-Hans") {//簡體漢字拼音
            let range = textField.markedTextRange
            if range == nil {
                if (NSString(string:textField.text!).length > 30) {
                    textField.text = NSString(string: textField.text!).substring(to: 30)
                    self.showAlert(messageStr: "姓名請在30字以內")
                }
            }
        }
    }

    //彈出提示框
    func showAlert(messageStr:String)
    {
        let myAlert = UIAlertView(title: "提示",message: messageStr,delegate: self,cancelButtonTitle: "好的")
        myAlert.show()
    }



為 UITextField 設定左邊距

//設定textField邊距
    func setTextFieldLeftPadding(textField:UITextField,forWidth width:CGFloat){
        var frame:CGRect = textField.frame
        frame.size.width = width
        let leftView:UIView = UIView(frame:frame)
        textField.leftViewMode = .Always
        textField.leftView = leftView
    }

修改 TextField placeHolder 字型樣式

func newPlaceholder(text:String,font:UIFont,color:UIColor) -> NSAttributedString{
    let str = NSAttributedString(string: text, attributes: [NSFontAttributeName:font,NSForegroundColorAttributeName:color])
    return str
}

textField.attributedPlaceholder = newPlaceholder(defaultPlaceolder, font: Font(14.0), color: UIColor.lightGrayColor())
也可以在xib中進行設定  參考:http://blog.csdn.net/wang631106979/article/details/51278531

UIImageView 新增模糊效果

//新增毛玻璃效果
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame = self.BgImgView!.frame
        self.BgImgView!.addSubview(blurView)


用自定義圖片替換 pageControl 的點

pageControl.setValue(UIImage(named:"pageImage"), forKey: "pageImage")
        pageControl.setValue(UIImage(named:"currentPageImage"), forKey: "currentPageImage")

pageControl 在大屏裝置上圓點不居中

要以當前區域的寬度

pageControl.frame  = CGRect(x:0,y:84,width:SCREEN_WIDTH,height:26)
        self.contentView.addSubview(pageControl)


獲取單行Label的寬度和高度

let width = lab.intrinsicContentSize.width
let height = lab.intrinsicContentSize.height