我试图在uitextfield效果上添加角半径,但不是锐角。
这是我的灰色文本域代码。
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
两个代码之间只有颜色差异,但它仍无效。
答案 0 :(得分:3)
self.layer.masksToBounds = true
这将解决您的问题
答案 1 :(得分:2)
@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
输出:
<强>故事板强>:
答案 2 :(得分: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
}
}
}
只需在项目中添加此文件,即可设置所有属性,并可以看到实时输出。您可以通过故事板设置所有属性。
答案 3 :(得分:0)
我的代码中只有一个错误。
TextField边框样式为行
textField.borderStyle = .Line
然后我将其设为无
textField.borderStyle = .None
它就像我想要的那样工作(圆角)...
无论如何..感谢您的支持和回答......