Is it possible to access a shadowed top level function in Swift?
You can prepend the module name, in this case Swift
:
extension Int {
func clamp(left: Int, right: Int) -> Int {
return Swift.min(Swift.max(self, left), right)
}
}
And just for fun: You get the same result with
extension Int {
func clamp(left: Int, right: Int) -> Int {
return (left ... right).clamp(self ... self).start
}
}
using the clamp()
method from ClosedInterval
.
add Shadow on UIView using swift 3
CODE SNIPPET:
extension UIView {
// OUTPUT 1
func dropShadow(scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSize(width: -1, height: 1)
layer.shadowRadius = 1
layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
// OUTPUT 2
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
}
}
NOTE: If you don't pass any parameter to that function, then the scale argument will be true by default. You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.
OUTPUT 1:
shadowView.dropShadow()
OUTPUT 2:
shadowView.dropShadow(color: .red, opacity: 1, offSet: CGSize(width: -1, height: 1), radius: 3, scale: true)
layer.shouldRasterize = true
will make the shadow static and cause a shadow for the initial state of theUIView
. So I would recommend not to uselayer.shouldRasterize = true
in dynamic layouts like view inside aUITableViewCell
.
Swift How to DRY code in variable closures?
You can create a function and reuse it
func descriptionTextView(with text: String) -> UITextView {
let textView = UITextView()
let shadow = NSShadow()
shadow.shadowColor = UIColor.white
shadow.shadowOffset = CGSize(width: 1, height: 1)
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.init(name: "Marker felt", size: 25)!,
.foregroundColor: UIColor.init(red: 91.0/255.0, green: 91.0/255.0, blue: 91.0/255.0, alpha: 1.0),
.shadow: shadow
]
let attributedText = NSAttributedString(string: text, attributes: attributes)
textView.attributedText = attributedText
textView.textAlignment = .center
textView.isEditable = false
textView.isScrollEnabled = false
textView.isSelectable = false
textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = .clear
return textView
}
lazy var descriptionTextViewOne: UITextView = descriptionTextView(with: "Tap anywhere to start\nyour day right!")
lazy var descriptionTextViewTwo: UITextView = descriptionTextView(with: "A happy video a day\nmakes the heartache\ngo away.")
How do I get and store an initial value of a property of a UIView?
You cannot declare it as a class level property because it won't be created until the ViewController is instantiated. I think what you want to do is as follows.
//instantiate with an initial value of 0.0
public var inputRest : CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
//you can do this here, because the userInputView will have been created
self.inputRest = self.userInputView.frame.origin.y
}
This is an inherent limitation of ViewControllers which are instantiated from a storyboard not being able to have let properties which can only be defined once. There are ways around this by overwriting the constructor used to create the view controller from the coder, but it's a lot of work, not easy to read, and very much a lot of work for nothing.
Fix error in Swift simd library (and missing documentation?)
float2
already has a min
method that takes no arguments. (There is an explanation for the methods when you choose to go to their definition in Xcode)
Try using Swift.min
to explicitly call swift's implementation
Related Topics
Convert Between Decimal, Binary and Hexadecimal in Swift
Init(Start:End:)' Is Deprecated: It Will Be Removed in Swift 3. Use the '..<' Operator
Why Non Optional Any Can Hold Nil
Delete Data from Coredata Swift
Padding a Swift String for Printing
Swift: 'Hashable.Hashvalue' Is Deprecated as a Protocol Requirement;
String Convert to Int and Replace Comma to Plus Sign
Swift 3: the Difference Between Public and Internal Access Modifiers
Swift 2: Expression Pattern of Type 'Bool' Cannot Match Values of Type 'Int'
What's the Difference Between Aranchor and Anchorentity
Do Swift Inner Classes Have Access to Self of Outer Class
Add "For In" Support to Iterate Over Swift Custom Classes
How to Convert Timeinterval into Minutes, Seconds and Milliseconds in Swift
Cannot Resolve Swift Packages After 15Th March 2022 in Xcode
Swift Custom Context Menu Previewprovider Can Not Click Any View Inside(Using Tapgesture)