2016-10-29 1 views
1

コーナー半径をuitextfieldに適用しようとしていますが、シャープコーナーではありません。TextField Corner Radius:シャープコーナーではない

添付の画像を確認してください。 enter image description here

私はこのような結果を期待しています。 enter image description here

灰色のテキストフィールドのコードです。

self.layer.cornerRadius = 7.5 
self.layer.borderWidth = 1.5 
self.layer.borderColor = UIColor(red: 209/255.0, green: 209/255.0, blue: 209/255.0, alpha: 1.0).CGColor 
self.backgroundColor = UIColor.whiteColor() 

ここは私の赤いテキストフィールドです。

self.layer.cornerRadius = 7.5 
self.layer.borderWidth = 1.5 
self.layer.borderColor = TextFieldRedBorderColor.CGColor 
self.backgroundColor = TextFieldRedBackgroundColor 

両方のコードに色の違いがありますが、それでも機能しません。ストーリーボード

Here is output.

答えて

0

私のコードに間違いが1つありました。

TextFieldのボーダースタイルはライン

textField.borderStyle = .Line 

た後、私はそれがどれ

textField.borderStyle = .None 

作らないと私は(角丸)望んでいたようにそれが仕事...とにかく

。..してくれてありがとうサポートと回答...

1
self.layer.masksToBounds = true 

これは

1
@IBOutlet var passwordboarderview: UIView! 
@IBOutlet var textusername: UITextField! 
@IBOutlet var txtpassword: UITextField! 

passwordboarderview.layer.cornerRadius = 7.5 
passwordboarderview.layer.borderColor = UIColor.whiteColor().CGColor 
passwordboarderview.layer.borderWidth = 2.0 

passwordboarderview.clipsToBounds = true 
txtpassword.attributedPlaceholder = NSAttributedString(string:"Password",attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()]) 

let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20)) 
textusername.leftView = paddingView 
textusername.leftViewMode = .Always 
textusername.layer.borderWidth = 2.0 
textusername.layer.cornerRadius = 7.5 
textusername.layer.borderColor = UIColor.grayColor().CGColor 
textusername.clipsToBounds = true 

出力あなたの問題を解決します

0
// 
// FDTextField.swift 
// FilloutDocs 
// 
// Created by Janak Thakkar on 15/03/16. 
// Copyright © 2016 zetrixweb. All rights reserved. 
// 

import UIKit 

private var maxLengthDictionary = [UITextField : Int]() 

@IBDesignable 

class ZWTextField : UITextField { 

    var topBorder: UIView? 
    var bottomBorder: UIView? 
    var leftBorder: UIView? 
    var rightBorder: UIView? 
    var leftimageview : UIImageView? 
    var rightimageview : UIImageView? 
    @IBInspectable var borderColor: UIColor = UIColor.clearColor() { 
     didSet { 
      layer.borderColor = borderColor.CGColor 
     } 
    } 

    @IBInspectable var ULText: Bool = false { 
     didSet { 
      let textRange = NSMakeRange(0, (self.text?.characters.count)!) 
      let attributedText = NSMutableAttributedString(string: (self.text)!) 
      attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange) 
      // Add other attributes if needed 

      self.attributedText = attributedText 
     } 
    } 
    @IBInspectable var borderWidth: CGFloat = 0 { 
     didSet { 
      layer.borderWidth = borderWidth 
     } 
    } 

    @IBInspectable var cornerRadius: CGFloat = 0 { 
     didSet { 
      layer.cornerRadius = cornerRadius 
     } 
    } 

    @IBInspectable var fitToWidth: Bool = false { 
     didSet { 
      adjustsFontSizeToFitWidth = fitToWidth 
     } 
    } 
    @IBInspectable var cursorColor : UIColor = UIColor.blueColor(){ 
     didSet { 
      tintColor = cursorColor 
     } 
    } 

    @IBInspectable var placeHolderColor : UIColor = UIColor.lightGrayColor(){ 
     didSet { 
      setValue(placeHolderColor, forKeyPath: "_placeholderLabel.textColor") 
     } 
    } 
    override func layoutSubviews() { 
     super.layoutSubviews() 
    } 
    @IBInspectable var rightImage : UIImage? { 
     didSet { 
      if rightImage != nil { 
       let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth : rightImage!.size.width + 10 
       rightViewMode = UITextFieldViewMode.Always 
       rightimageview = UIImageView() 
       rightimageview!.frame=CGRectMake(self.frame.size.width - width, self.frame.origin.y+2, width,self.frame.size.height-4) 
       rightimageview!.image = rightImage 
       rightView = rightimageview 
       self.rightViewMode = .Always 
       rightimageview!.contentMode = .Center 
      } 
      else { 
       if rightimageview != nil { 
        rightimageview?.removeFromSuperview() 
        rightimageview = nil 
       } 
      } 
     } 
    } 
    @IBInspectable var rightviewWidth : CGFloat = 0 { 
     didSet{ 
      if rightimageview != nil{ 
       let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth : rightImage!.size.width + 10 
       rightimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4) 
      } 
     } 
    } 
    @IBInspectable var leftImage : UIImage? { 
     didSet { 
      if leftImage != nil { 
       let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth : leftImage!.size.width + 10 
       leftViewMode = UITextFieldViewMode.Always 
       leftimageview = UIImageView(); 
       leftimageview!.frame=CGRectMake(self.frame.origin.x+50, self.frame.origin.y+5, width,self.frame.size.height-5) 
       //leftimageview!.frame = CGRectMake(10, 5, 40, 40) 
       leftimageview!.image = leftImage; 
       leftView = leftimageview; 
       self.leftViewMode = .Always 
       leftimageview!.contentMode = .Center 
      } 
     } 
    } 
    @IBInspectable var leftviewWidth : CGFloat = 0 { 
     didSet{ 
      if leftimageview != nil{ 
       let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth : leftImage!.size.width + 10 
       leftimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4) 
      } 
     } 
    } 
    @IBInspectable var bottomLineWidth : CGFloat = 1 { 
     didSet{ 
      let border: CALayer = CALayer() 
      border.borderColor = UIColor.darkGrayColor().CGColor 
      self.frame = CGRectMake(0, self.frame.size.height - bottomLineWidth, self.frame.size.width, self.frame.size.height) 
      border.borderWidth = borderWidth 
      self.layer.addSublayer(border) 
      self.layer.masksToBounds = true 
     } 
    } 

    @IBInspectable var bottomLineColor : UIColor = UIColor.lightGrayColor(){ 
     didSet { 
      let border: CALayer = CALayer() 
      border.borderColor = bottomLineColor.CGColor 
     } 
    } 
    @IBInspectable var paddingLeft: CGFloat = 0 
    @IBInspectable var paddingRight: CGFloat = 0 

    override func textRectForBounds(bounds: CGRect) -> CGRect { 
     return CGRectMake(bounds.origin.x + paddingLeft, bounds.origin.y, 
      bounds.size.width - paddingLeft - paddingRight, bounds.size.height); 
    } 

    override func editingRectForBounds(bounds: CGRect) -> CGRect { 
     return textRectForBounds(bounds) 
    } 
    @IBInspectable var topBorderColor : UIColor = UIColor.clearColor() 
    @IBInspectable var topBorderHeight : CGFloat = 0 { 
     didSet{ 
      if topBorder == nil{ 
       topBorder = UIView() 
       topBorder?.backgroundColor=topBorderColor; 
       topBorder?.frame = CGRectMake(0, 0, self.frame.size.width, topBorderHeight) 
       addSubview(topBorder!) 
      } 
     } 
    } 
    @IBInspectable var bottomBorderColor : UIColor = UIColor.clearColor() 
    @IBInspectable var bottomBorderHeight : CGFloat = 0 { 
     didSet{ 
      if bottomBorder == nil{ 
       bottomBorder = UIView() 
       bottomBorder?.backgroundColor=bottomBorderColor; 
       bottomBorder?.frame = CGRectMake(0, self.frame.size.height - bottomBorderHeight, self.frame.size.width, bottomBorderHeight) 
       addSubview(bottomBorder!) 
      } 
     } 
    } 
    @IBInspectable var leftBorderColor : UIColor = UIColor.clearColor() 
    @IBInspectable var leftBorderHeight : CGFloat = 0 { 
     didSet{ 
      if leftBorder == nil{ 
       leftBorder = UIView() 
       leftBorder?.backgroundColor=leftBorderColor; 
       leftBorder?.frame = CGRectMake(0, 0, leftBorderHeight, self.frame.size.height) 
       addSubview(leftBorder!) 
      } 
     } 
    } 
    @IBInspectable var rightBorderColor : UIColor = UIColor.clearColor() 
    @IBInspectable var rightBorderHeight : CGFloat = 0 { 
     didSet{ 
      if rightBorder == nil{ 
       rightBorder = UIView() 
       rightBorder?.backgroundColor=topBorderColor; 
       rightBorder?.frame = CGRectMake(self.frame.size.width - rightBorderHeight, 0, rightBorderHeight, self.frame.size.height) 
       addSubview(rightBorder!) 
      } 
     } 
    } 
    @IBInspectable var maxLength: Int { 
     get { 
      if let length = maxLengthDictionary[self] { 
       return length 
      } else { 
       return Int.max 
      } 
     } 
     set { 
      maxLengthDictionary[self] = newValue 
      addTarget(self, action: "checkMaxLength:", forControlEvents: UIControlEvents.EditingChanged) 
     } 
    } 

    func checkMaxLength(sender: UITextField) { 
     let newText = sender.text 
     if newText?.characters.count > maxLength { 
      let cursorPosition = selectedTextRange 
      text = (newText! as NSString).substringWithRange(NSRange(location: 0, length: maxLength)) 
      selectedTextRange = cursorPosition 
     } 
    } 


} 

ただ、プロジェクトにこのファイルを追加して、あなたはすべてのプロパティを設定することができますし、ストーリーボードを介して設定することができ、リアルタイムout.Allのプロパティを見ることができます。

+0

興味深いコードかもしれませんが、それは非常に多くを追加して、 stionは見えません。 – vikingosegundo

+0

@IBInspectableですべてのプロパティをカバーしようとしましたが、さらに必要がある場合はお知らせください。 –

+0

サンプルコードをgithubにアップロードしました::: https://github.com/ZetrixWeb-iOS/TextField---IBDesignable –

関連する問題