UITextField seems to ignore clipsToBounds property - ios

I am creating a custom search bar by subclassing the UITextField. For some reason, the search bar has a very subtle, weird white line/border around the edges. The issue only exists on my iPhone 13 Pro Max - on the simulator everything works perfectly fine.
notice the slight white border around the pill shaped search text field. Almost like it has a white background color.
I tried to battle this by setting layer.masksToBounds and clipsToBounds to true, but unfortunately that doesn't work either.
This is my code:
public class MyCustomSearchBar: UITextField {
public override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = Theme.GRAY800
self.borderStyle = .roundedRect
self.layer.masksToBounds = true
self.clipsToBounds = true
self.autocorrectionType = .no
self.textColor = .white
self.attributedPlaceholder = NSAttributedString(string: "Search", attributes: [.foregroundColor : Theme.GRAY400, .font: FontFamily.ProximaNova.regular.font(size: 16)])
}
}
UPDATE:
If I add a border width and color, everything looks normal again:
Question:
Why is this a problem on my iPhone but not in the simulator? Why do I manually have to set the border width and color to avoid those white "fragments"?

create border and corner manually, this is an example:
let myTextField = MyCustomSearchBar()
in viewDidLoad set constraints
view.addSubview(myTextField)
myTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
myTextField.widthAnchor.constraint(equalToConstant: view.frame.width - 40).isActive = true
myTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myTextField.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
now assign border width, border color e round corner in MyCustomSearchBar class
public class MyCustomSearchBar: UITextField {
public override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .yourColor
self.borderStyle = .none
self.layer.masksToBounds = true
self.layer.cornerRadius = 16
self.clipsToBounds = true
self.layer.borderWidth = 2
self.layer.borderColor = UIColor.white.cgColor
self.autocorrectionType = .no
self.textColor = .white
self.attributedPlaceholder = NSAttributedString(string: "Search", attributes: [.foregroundColor : UIColor.lightGray, .font: UIFont.systemFont(ofSize: 16, weight: .regular)])
self.addPadding(padding: .left(20)) // this is a extra
self.translatesAutoresizingMaskIntoConstraints = false
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
to add padding spacing to your textField use my extension:
extension UITextField {
enum PaddingSpace {
case left(CGFloat)
case right(CGFloat)
case equalSpacing(CGFloat)
}
func addPadding(padding: PaddingSpace) {
self.leftViewMode = .always
self.layer.masksToBounds = true
switch padding {
case .left(let spacing):
let leftPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
self.leftView = leftPaddingView
self.leftViewMode = .always
case .right(let spacing):
let rightPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
self.rightView = rightPaddingView
self.rightViewMode = .always
case .equalSpacing(let spacing):
let equalPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
// left
self.leftView = equalPaddingView
self.leftViewMode = .always
// right
self.rightView = equalPaddingView
self.rightViewMode = .always
}
}
}
This is the result:

Related

Search Bar textfield cursor color not changing in mac Catalyst it's showing black color ios swift

i also try to change with "searchController.searchBar.searchTextField.tintColor = .white"
but it's not working issue facing after xcode 13 update.
Try to create a custom searchController and into the setup to change the tintColor of all the subviews that are different of UIButton .
Here an example :
class CustomSearchController: UISearchController {
var placeHolder:String?
private var catalogSearchBar = CatalogSearchBar()
override public var searchBar: UISearchBar {
get {
catalogSearchBar.placeholder = placeHolder
return catalogSearchBar
}
}
}
class CatalogSearchBar: UISearchBar {
init() {
super.init(frame: .zero)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
private func setup() {
backgroundColor = Constants.shared.navigationBar.lightModeBgColor
// text field
let textField = searchTextField
textField.subviews.forEach { (view) in
if ((view as? UIButton) != nil) {
view.tintColor = UIColor.white
}
}
textField.frame.size.height = 35
self.searchTextPositionAdjustment = UIOffset(horizontal: 4, vertical: 0)
textField.layer.cornerRadius = 15
textField.placeholder = self.placeholder
textField.attributedPlaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder! : "", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
textField.layer.masksToBounds = true
textField.layer.backgroundColor = UIColor.white.withAlphaComponent(0.25).cgColor
if let view = textField.value(forKey: "backgroundView") as? UIView {
view.removeFromSuperview()
}
textField.font = UIFont(name: "Montserrat-Regular", size: 15)
textField.textColor = UIColor.white
textField.tintColor = UIColor.white
// search icon
let leftView: UIView = {
let image = UIImage(named: "search")
let padding = 8
let size = 20
let outerView = UIView(frame: CGRect(x: 0, y: 0, width: size + padding, height: size) )
let iconView = UIImageView(frame: CGRect(x: padding, y: 0, width: size, height: size))
iconView.tintColor = UIColor.white
iconView.image = image
outerView.addSubview(iconView)
return outerView
}()
textField.leftView = leftView
}
}

Problem with rounded corner in UITableView in Objective C [duplicate]

Hello i have some views with rounded corners, and I'd like to apply a shadow to this views.
SO first I round the view :
view.layer.cornerRadius = 15
view.clipsToBounds = true
then I apply the shadow :
func dropShadow(scale: Bool = true) {
self.layer.masksToBounds = false
self.layer.cornerRadius = 15
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
self.layer.shadowOffset = CGSize(width: 3, height: 3)
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 5
}
view.dropShadow()
I got my rounded view with a shadow but the shadow is not rounded like my view. The shadow is not rounded at all
You cannot cast a shadow from a view whose clipsToBounds is true. If a view's masksToBounds is true, its clipsToBounds is true; they are the same thing.
If you want a shadow to appear to come from from a view that clips, you need to use two views: one that the user can see, with rounded corners and clipsToBounds set to true, and another that the user can't see because it's behind the first one, also with rounded corners, but with clipsToBounds set to false, to cast the shadow.
class ShadowView : UIView {
override init(frame: CGRect) {
super.init(frame:frame)
self.isOpaque = true
self.backgroundColor = .black
self.dropShadow()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func dropShadow() {
self.layer.masksToBounds = false
self.layer.cornerRadius = 15
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 3, height: 3)
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 5
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let r = CGRect(x: 100, y: 100, width: 100, height: 100)
let v = UIImageView(frame:r)
v.image = UIImage(named:"marsSurface.jpg")
v.clipsToBounds = true
v.backgroundColor = .red
v.layer.cornerRadius = 15
self.view.addSubview(ShadowView(frame:r))
self.view.addSubview(v)
}
}
Note that neither view is a subview of the other, nor do they have a superview that clips, as that would clip the shadow.
Trying to have a single view handle both corner rounding and shadow logic is not recommended. The best way to create the effect you are looking for is to wrap your rounded view with another parent view that owns the shadow.
This can be best illustrated with a playground. Try this code out in a playground.
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let shadowView = UIView()
shadowView.frame = CGRect(x: 50, y: 200, width: 200, height: 100)
shadowView.backgroundColor = nil
shadowView.layer.shadowColor = UIColor.black.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 4.0)
shadowView.layer.shadowRadius = 8.0
shadowView.layer.shadowOpacity = 1
let roundedView = UIView()
roundedView.frame = shadowView.bounds
roundedView.backgroundColor = .gray
roundedView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner,.layerMaxXMinYCorner]
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
shadowView.addSubview(roundedView)
view.addSubview(shadowView)
view.backgroundColor = UIColor.white
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Don't forget to set the shadowPath property on your shadow view's layer in either viewWillLayoutSubviews or layoutSubviews. If this property is set to nil, UIKit has to perform off screen rendering to figure out how to draw the shadow rect.

Extension causing issue with clear button in Swift

I'm using a placeHolder extension to give padding to the placeholder. But when I apply this class to my input field it doesn't show the clear button even if I select "Appears while editing" on the storyboard.
Can someone tell me how to fix it?
import UIKit
class textFiledplaceholder: UITextField {
static let font_size : CGFloat = 16
static let leftPadding : CGFloat = 15
static let righPadding : CGFloat = 15
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.comminIt()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.comminIt()
}
func comminIt()
{
borderStyle = .none
backgroundColor = .white
// layer.masksToBounds = true
setLeftPaddingPoints(textFiledplaceholder.leftPadding)
setRightPaddingPoints(textFiledplaceholder.righPadding)
}
}
extension UITextField {
func setLeftPaddingPoints(_ amount:CGFloat){
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.leftView = paddingView
self.leftViewMode = .always
}
func setRightPaddingPoints(_ amount:CGFloat) {
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.rightView = paddingView
self.rightViewMode = .always
}
}
You can not use rightview and clearbutton together. And if you are going to use a clear button then I don't think there is any use of right padding. Remove right padding and it will resolve your issue.

How to make a drop shadow effect on a label in Swift?

I can't figure out how to code a drop shadow on a label. I have a score label that changes so just photoshopping text with shadows wont be possible. I need to code it so it automatically has a blurry shadow behind the text at all times. Can anyone come with some examples or help?
People saying this is a duplicate, the "duplicate" is about drop shadows on UIView, mine is about UILabel. It's not the same thing.
Give this a try - you can run it directly in a Playground page:
import UIKit
import PlaygroundSupport
let container = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))
container.backgroundColor = UIColor.lightGray
PlaygroundPage.current.liveView = container
var r = CGRect(x: 40, y: 40, width: 300, height: 60)
let label = UILabel(frame: r)
label.font = UIFont.systemFont(ofSize: 44.0)
label.textColor = .white
label.frame = r
label.text = "Hello Blur"
container.addSubview(label)
label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowRadius = 3.0
label.layer.shadowOpacity = 1.0
label.layer.shadowOffset = CGSize(width: 4, height: 4)
label.layer.masksToBounds = false
Play around with different values for the shadow Color, Opacity, Radius and Offset
Result:
UILabel has a property for changing its shadow, the image below shows the property in attributes inspector and the result.
Result of that effect on label
You can write an extension and use it. Place the extension code outside of class ViewController.
I like subtle shadow.
extension UILabel {
func textDropShadow() {
self.layer.masksToBounds = false
self.layer.shadowRadius = 2.0
self.layer.shadowOpacity = 0.2
self.layer.shadowOffset = CGSize(width: 1, height: 2)
}
static func createCustomLabel() -> UILabel {
let label = UILabel()
label.textDropShadow()
return label
}
}
On your label simply call this method
myLabel.textDropShadow()
works fine but add shadow to ALL label, not to text.
in this case:
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let shadow = NSShadow()
shadow.shadowColor = UIColor.blue
shadow.shadowBlurRadius = 10
let attrs: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 36),
.foregroundColor: UIColor.red,
.shadow: shadow
]
let s = "MY TEXT"
let attributedText = NSAttributedString(string: s, attributes: attrs)
self.label.attributedText = attributedText
}
}
You will get:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/CRMpg.png
**note:** You must add attributed string every time, as shadow is an attribute of string, not label, otherwise you can also derive class and override "setText". (keeping attributes inside the object in a a property you can set on init/setter)
Swift 4, IBInspectable using extension
extension UILabel {
#IBInspectable var isShadowOnText: Bool {
get {
return self.isShadowOnText
}
set {
guard (newValue as? Bool) != nil else {
return
}
if newValue == true{
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowRadius = 2.0
self.layer.shadowOpacity = 1.0
self.layer.shadowOffset = CGSize(width: 2, height: 2)
self.layer.masksToBounds = false
}
}
}
}
Swift 4 - Extension with shadow parameters:
// Label Shadow
extension UILabel {
func lblShadow(color: UIColor , radius: CGFloat, opacity: Float){
self.textColor = color
self.layer.masksToBounds = false
self.layer.shadowRadius = radius
self.layer.shadowOpacity = opacity
self.layer.shadowOffset = CGSize(width: 1, height: 1)
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.main.scale
}
}
On your label simply call this method
let titleColor = UIColor(red:0.08, green:0.08, blue:0.08, alpha:1.0)
titleLbl.lblShadow(color: titleColor, radius: 3, opacity: 0.25)
U can make a extension method for all UIView subclasses.
extension UIView {
func drawShadow(offset: CGSize, opacity: Float = 0.25, color: UIColor = .black, radius: CGFloat = 1) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowOffset = offset
layer.shadowOpacity = opacity
layer.shadowRadius = radius
}
}

Why is the label not being added to the view?

I am trying to add a label to the UIView. I am seeing the view, but I am not seeing the label. What am I missing?
var StLabel: UILabel = {
let label = UILabel()
label.font = UIFont(name: "Arial-Regular", size: 20)
label.textColor = UIColor.black
label.sizeToFit()
return label
}()
init(frame: CGRect, text: String) {
super.init(frame: frame)
self.frame = CGRect(x: 0, y: 0, width: 300, height: 50)
self.layer.cornerRadius = 10
self.layer.masksToBounds = true
self.backgroundColor = UIColor.white
StLabel.text = text
self.addSubview(StLabel)
}
You are calling sizeToFit() on the label before its text is set so the label's frame ends up with a 0 size.
Add a call to StLabel.sizeToFit() after the line StLabel.text = text.

Resources