UILabel with Border and background Issue - swift

Here is the code I have for UILabel
descriptionText.layer.cornerRadius = 8
descriptionText.layer.borderColor = UIColor.lightGray.cgColor
descriptionText.layer.borderWidth = 2
descriptionText.text = ""
descriptionText.layer.shadowOffset = CGSize(width: -10, height: -10)
descriptionText.layer.shadowRadius = -5.0
descriptionText.backgroundColor = .white
descriptionText.textColor = .black
descriptionText.numberOfLines = 0
descriptionText.lineBreakMode = .byWordWrapping
However This is what it looks like
Why is the Corner Not Transparent or how do you make what is outside the border transparent
Thanks in advance

You need to hide everything outside of the cornerRadius by setting .clipsToBounds on the label to true. Problem is that if you do this you will lose the shadow, as this is outside the bounds as well. Try this:
Place your label inside a container view, constrain the label to the container edges, and create an outlet to it. Then try the following code:
containerView.borderColor = UIColor.lightGray.cgColor
containerView.layer.borderColor = UIColor.lightGray.cgColor
containerView.layer.borderWidth = 2
containerView.layer.shadowOffset = CGSize(width: -10, height: -10)
containerView.layer.shadowRadius = -5.0
containerView.backgroundColor = UIColor.clear
descriptionText.text = "Testing"
descriptionText.cornerRadius = 8
descriptionText.clipsToBounds = true // or descriptionText.layer.masksToBounds = true
descriptionText.backgroundColor = .white
descriptionText.textColor = .black
descriptionText.numberOfLines = 0
descriptionText.lineBreakMode = .byWordWrapping

Related

Add shadow to UILabel with rounded corners swift

i want to achieve something like this:
I have set rounded corners on my UILabel like this:
label.layer.cornerRadius = 8.0
label.clipsToBounds = true
and I have tried adding a shadow like this:
func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowOpacity = opacity
layer.shadowOffset = offSet
layer.shadowRadius = radius
layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
I try to achieve by adding a UILabel inside UIView
viewlbl.layer.cornerRadius = 20
viewlbl.clipsToBounds = true
viewlbl.layer.shadowRadius = 10
viewlbl.layer.shadowOpacity = 1.0
viewlbl.layer.shadowOffset = CGSize(width: 3, height: 3)
viewlbl.layer.shadowColor = UIColor.red.cgColor
viewlbl.layer.masksToBounds = false

How to increase highlighted color of a particular date in FSCalendar(Swift 5.0)

I am trying to increase the size of highlighted colour of a particular date in FSCalendar.The below is the code am using also am attaching.Please let me know what i am missing.Thank you
func setUpCalendar() {
calendar = FSCalendar(frame: CGRect(x: 20, y: 150, width: self.view.frame.size.width - 30, height: 150))
calendar.scrollDirection = .horizontal
calendar.scope = .week
calendar.allowsMultipleSelection = false
calendar.appearance.separators = .interRows
calendar.rowHeight = 30.0
calendar.appearance.headerTitleColor = UIColor(hex: "FF7271")
calendar.appearance.todayColor = UIColor(hex: "FF7271")
calendar.appearance.selectionColor = UIColor(hex: "FFEFEF")
calendar.appearance.titleSelectionColor = .black
calendar.appearance.weekdayTextColor = .systemGray
calendar.appearance.headerMinimumDissolvedAlpha = 0
calendar.appearance.headerTitleFont = UIFont(name: "System", size: 0)
}
hide like this. go to storyboard / xib what you used.
goto design -> go to Atrributed inspector and set header height = 0
as per you define it programatically than just write this line
calendar.headerHeight = 0

How to add label to SCNNode?

I’m trying to add a label to an SCNNode. So far I’ve managed to set a SKLabelNode as the material for the SCNNode. It sort of works, I can the SCNNode becomes the background colour of the SKLabelNode but I can’t see the text. Sometime I can see a red haze (the text colour is red) but no readable text.
I also tried setting the material as a UIView and adding a UiLabel as a sub view. Again it sets as I can the whole SCNNode becomes the background colour of the UiLabel but I can’t see any text.
var block = SCNNode()
var spriteScene = SKScene()
var Lbl = SKLabelNode()
lbl.text = “Hello World”
lbl.color = blue (playground colour literal)
lbl. = 2 //I tried various numbers
lbl.fontColor = SKColor.red
spriteScene.addChild(lbl)
I got it after some hit and trial. I had to try different values before I got these size, scale and rotation to display the label as I want.
note: My node here is SCNPlane with 0.3 width and 0.2 height, so size of SKScene and rectangle and position of label are hard coded accordingly.
func addLabel(text: String){
let sk = SKScene(size: CGSize(width: 3000, height: 2000))
sk.backgroundColor = UIColor.clear
let rectangle = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 3000, height: 2000), cornerRadius: 10)
rectangle.fillColor = UIColor.black
rectangle.strokeColor = UIColor.white
rectangle.lineWidth = 5
rectangle.alpha = 0.5
let lbl = SKLabelNode(text: text)
lbl.fontSize = 160
lbl.numberOfLines = 0
lbl.fontColor = UIColor.white
lbl.fontName = "Helvetica-Bold"
lbl.position = CGPoint(x:1500,y:1000)
lbl.preferredMaxLayoutWidth = 2900
lbl.horizontalAlignmentMode = .center
lbl.verticalAlignmentMode = .center
lbl.zRotation = .pi
sk.addChild(rectangle)
sk.addChild(lbl)
let material = SCNMaterial()
material.isDoubleSided = true
material.diffuse.contents = sk
node.geometry?.materials = [material]
node.geometry?.firstMaterial?.diffuse.contentsTransform = SCNMatrix4MakeScale(Float(1), Float(1), 1)
node.geometry?.firstMaterial?.diffuse.wrapS = .repeat
node.geometry?.firstMaterial?.diffuse.wrapS = .repeat
}

Layer: Set corder radius then add a shadow

I want an image to appear as a circle with a shadow. Doing the following don't work because the shadow is probably cut off by the clipping...
let layer = myImageView.layer
// cut circle
layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.whiteColor().CGColor
layer.cornerRadius = myImageView.frame.height/2
myImageView.clipsToBounds = true
// add shadow
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOffset = CGSize(width: 0, height: 10)
layer.shadowOpacity = 0.4
layer.shadowRadius = 5
I had the same problem...
I solve this by putting an image on top of the button(in my case was a button not an image) to act like a shadow.
button.sizeToFit()
shadow.layer.cornerRadius = 6
shadow.backgroundColor = UIColor.whiteColor()
shadow.layer.shadowOpacity = 0.8
shadow.layer.shadowRadius = 6
shadow.layer.shadowOffset = CGSizeMake(0, 2)
button.layer.cornerRadius = 6
button.clipsToBounds = true
shadow is the imageView that will be the shadow, of course.
button in your case will be your image.

Font Luckiest-Guy not vertically Centered in UILabel

I am using a font called: Luckiest Guy in a Swift project (Link to the font).
However, in UILabels, it's not vertically aligned to the center even after setting baselineAlignment to the center...
Could you check my code and help me out. Thanks a lot!!
I attached a screen shot here to show the problem (https://www.dropbox.com/s/1hxq68dewtzzkxi/IMG_9733.png?dl=0)
var dynamicLabel: UILabel = UILabel()
dynamicLabel.frame = CGRectMake(0, 0, 60, 35) //Frame (the coordinates are changed here to simply the code)
// *** Color and offset *** //
dynamicLabel.backgroundColor = UIColor.blueColor()
dynamicLabel.textColor = UIColor.whiteColor()
dynamicLabel.shadowColor = UIColor.blackColor()
dynamicLabel.shadowOffset = CGSizeMake(0, 1.0)
// *** Text alignment *** //
dynamicLabel.textAlignment = NSTextAlignment.Center
dynamicLabel.font = UIFont(name: "LuckiestGuy-Regular", size: 25)
dynamicLabel.adjustsFontSizeToFitWidth = true
dynamicLabel.minimumScaleFactor = 0.5
dynamicLabel.baselineAdjustment = UIBaselineAdjustment.AlignCenters
// *** Label corner and border *** //
dynamicLabel.layer.masksToBounds = true
dynamicLabel.layer.cornerRadius = 15.0
dynamicLabel.layer.borderColor = UIColor.whiteColor().CGColor
dynamicLabel.layer.borderWidth = 1.0
// Text
dynamicLabel.text = "Excellent!"
As your explanation to what is happening instead is rather vague I assume that the Label isn't really visible, because with the implementation of dynamicLabel.textAlignment = NSTextAlignment.Center the text should be aligned
I've tried your code in a sample project and the only two things I noticed that were off were the x and y coordinates of the label (as they're set to zero, so the label doesn't really get displayed anyways) and that you didn't add the label to the view:
let dynamicLabel = UILabel(frame: CGRectMake(100, 200, 200, 100))
// *** Color and offset *** //
dynamicLabel.backgroundColor = UIColor.blueColor()
dynamicLabel.textColor = UIColor.whiteColor()
dynamicLabel.shadowColor = UIColor.blackColor()
dynamicLabel.shadowOffset = CGSizeMake(0, 1.0)
dynamicLabel.text = "Excellent!"
// *** Text alignment *** //
dynamicLabel.textAlignment = NSTextAlignment.Center
dynamicLabel.adjustsFontSizeToFitWidth = true
dynamicLabel.minimumScaleFactor = 0.5
dynamicLabel.baselineAdjustment = UIBaselineAdjustment.AlignCenters
// *** Label corner and border *** //
dynamicLabel.layer.masksToBounds = true
dynamicLabel.layer.cornerRadius = 15.0
dynamicLabel.layer.borderColor = UIColor.whiteColor().CGColor
dynamicLabel.layer.borderWidth = 1.0
view.addSubview(dynamicLabel)