I am looking for a way to find the value(text) of a UILabel at a given (x,y) coordinate.
All other questions seem to be about getting the (x,y) of a label, I am trying to do the opposite...
For example (30,40) would return the value of the Label at that location.
Thank you!
Update
func getLabel(location: CGPoint){
let view = self.view.hitTest(location, withEvent:nil)
//I would like to get the value of the label here but I'm not sure how to.
}
#IBAction func tap(sender: UITapGestureRecognizer) {
var coordinate = sender.locationInView(GraphView?())
getLabel(coordinate)
}
You can accomplish this by hit-testing.
Pass the point you want to the top-level view, and it will return the deepest view within the view hierarchy that contains that point.
UIView *view = [self.view hitTest:location withEvent:nil];
In Swift, it would look like
let selectedView = view.hitTest(location, withEvent: nil)
Update:
You need to set the label's userInteractionEnabled to true for your labels to register the hit.
You'll need to check the returned view to see if it is a label, then check to see if it has any text.
if let label = selectedView as? UILabel
{
if label.text!
{
// .. do what you want with the label's text
}
}
Related
I am having a problem and have searched all across StackO and did not see a solution.
I have a UITextview extension with TextViewDelegate that I call inside of my VC so that i can have a placeholder label. The problem is i now need to add a func that checks for remaining chars in that same textView which i am able to get to work properly. But i cant grab a label to present it on the VC from that extension. I have been trying delegates but since it is a delegate itself i cant use my normal methods. What is the best route to go about this? Thank You for your help!
Here is the code. The placeholder label code is left out since it will make everything longer and I do not feel its needed for a solution. But I can add if necessary. And i can not move this code straight into VC as i need this extension to stay like this.
extension UITextView: UITextViewDelegate {
/// When the UITextView change, show or hide the label based on if the UITextView is empty or not
public func textViewDidChange(_ textView: UITextView) {
if let placeholderLabel = self.viewWithTag(100) as? UILabel {
placeholderLabel.isHidden = !self.text.isEmpty
}
checkRemainingChars(textView: textView)
}
func checkRemainingChars(textView: UITextView) {
let allowedChars = 140
if let charsInTextField = textView.text?.count {
let charsInLabel = charsInTextField
let remainingChars = charsInLabel
if remainingChars <= allowedChars {
//Need to grab this label
charsLeftLabel.textColor = UIColor.lightGray
}
if remainingChars >= 120 {
//Need to grab this label
charsLeftLabel.textColor = UIColor.orange
}
if remainingChars >= allowedChars {
//Need to grab this label
charsLeftLabel.textColor = UIColor.red
}
//This prints fine
print("Remaining chars is \(remainingChars)/140")
//Need to grab this label
charsLeftLabel.text = String(remainingChars)
}
}
Thanks again.
I'm trying to work out the best method for finding the exact subview (if any) at a given CGPoint in a UIView.
Part of my scene/ViewController is a custom UIView subclass (designed in a XIB), which presents a UIStackView of subviews (also custom, XIB-designed).
The VC has a UITapGestureRecognizer on that custom view, and in my #IBAction, I want to identify which specific one of its subviews was tapped, then handle it accordingly:
#IBAction func handleTap(recognizer:UITapGestureRecognizer) {
let tapLocation = recognizer.location(in: recognizer.view)
if let subviewTapped = customView.subviewAtLocation(tapLocation) {
handleTapForSubview(subviewTapped)
}
}
However, I don't know how to implement that subviewAtLocation(CGPoint) method. I wasn't able to find anything in the standard UIView methods.
Any advice on how it should be done would be welcome.
Alternatively, I considered adding a tap recognizer to each of the subviews instead, then delegating to the parent, then to the VC, but that feels inefficient, and like it's putting too much control logic in the views, rather than the VC.
Thank you.
A solution would be using the contains(point:) method of CGRect. The idea is to iterate over the stack view's subviews and check which subviews contains the touched point. Here:
#IBAction func handleTap(recognizer:UITapGestureRecognizer) {
let tapLocation = recognizer.location(in: recognizer.view)
let filteredSubviews = stackView.subviews.filter { subView -> Bool in
return subView.frame.contains(tapLocation)
}
guard let subviewTapped = filteredSubviews.first else {
// No subview touched
return
}
// process subviewTapped however you want
}
//use hitTest() method. this gives the view which contain the point
let subView = parentView.hitTest(point, with: nil)
I have messages screen and implement custom tableviewcell for the display message. A message should be text or image and some case I need to display boxes with information(see image sender and receiver). it's working fine but some time messages view cut off(see image messages). I have used many stackViews to hiding and show some views.
Please find the code here for more understanding.
The possible cause for such a behaviour is setting up the layers of the view in cell, I can see in your cell, you are adding the corner radius to the background. I could be able to fix it in my app by using the following approach.
Define a optional data varibale in your cell.
var currentData: MessageModel?
set that value in the method you are calling to provide the data to cell.
func loadData(_ data:MessageModel) -> Void {
currentData = data
// YOUR EXISTING CODE GOES HERE.
// Move your code to the function which do the setup of corner radius.
// Call this method.
setupCornerRadius()
}
Add the following methods to your cells
open override func layoutIfNeeded() {
super.layoutIfNeeded()
setupCornerRadius()
}
open override func layoutSubviews() {
super.layoutSubviews()
setupCornerRadius()
}
func setupCornerRadius() {
if let data = currentData {
let strMsg = data.body ?? ""
lblMsgBody.text = strMsg
if strMsg != "" {
viewBG.backgroundColor = UIColor.primaryGreen
if strMsg.count > 5 {
viewBG.layer.cornerRadius = 18.0
}else{
viewBG.layer.cornerRadius = 12.0
}
}
else{
viewBG.backgroundColor = UIColor.clear
}
}
}
Try the above approach.
Along with that what I did was, I removed the stackView from the cell and managed to implement the required UI by setting up the constraints.
Label with numberOfLines = 0 and setting Leading, Trailing, Bottom and Top constraints. with value = 8 (You can set it as per your margin and spacing you needs.)
Try and share the results.
Use UI debugger and see what exactly is going on.
I want to create a stack of rounded circle views like this.
UIView stack
I want to return this whole stack of UIViews at once. So I tried in this way.
open func setupCirclestack(parentFrame:CGRect)->UIView
{
let arrayColor=[UIColor.yellow,UIColor.blue,UIColor.red]
let baseCircle=Circle.init(frame: parentFrame)
baseCircle.backgroundColor=UIColor.purple
var parentview=baseCircle
// var existingFrame=baseCircle.frame
for i in 0...2//<CircleValues().numberOfCircles-1
{
let circle=self.getInnerCircle(currentFrame: parentFrame)
circle.backgroundColor=arrayColor[i]
parentview.addSubview(circle)
parentview=circle as! Circle
//existingFrame=circle.frame
}
return parentview
}
func getInnerCircle(currentFrame:CGRect)->UIView
{
CircleValues.sharedInstance.radius=CircleValues.sharedInstance.radius-30
print("New Radius------\(CircleValues.sharedInstance.radius)")
let circle=Circle.init(frame: currentFrame)
return circle
}
But I can get only the last (inner most view) view. How can I return the whole stack of UIViews from this method
It is because your all the frames for all the circles are same as parentFrame.
So, your all views are adding but you can only able to see last view as it is overlaps other views!
You have to decrease your frame size for every iteration of your for loop for every child view you are adding!
for i in 0...2//<CircleValues().numberOfCircles-1
{
let circle=self.getInnerCircle(currentFrame: parentFrame) // decrease size(height and width) here every time to achieve result attached in screenshot in your question
circle.backgroundColor=arrayColor[i]
parentview.addSubview(circle)
parentview=circle as! Circle
//existingFrame=circle.frame
}
You reasign a parentView variable at the end of for loop, So it will replace existing object of parentView in which you added the circle as a subview. Therefore it will return a last circle object which is stored in parentView.
for i in 0...2//<CircleValues().numberOfCircles-1
{
let circle=self.getInnerCircle(currentFrame: parentFrame)
circle.backgroundColor=arrayColor[i]
parentview.addSubview(circle)
//parentview=circle as! Circle
//existingFrame=circle.frame
}
If you want to return views inside your parent view you need to change your function to something like this:
open func setupCirclestack(parentFrame:CGRect)->UIView
{
let arrayColor=[UIColor.yellow,UIColor.blue,UIColor.red]
let baseCircle=Circle.init(frame: parentFrame)
baseCircle.backgroundColor=UIColor.purple
var parentview=baseCircle
// var existingFrame=baseCircle.frame
for i in 0...2//<CircleValues().numberOfCircles-1
{
let circle=self.getInnerCircle(currentFrame: parentFrame)
circle.backgroundColor=arrayColor[i]
parentview.addSubview(circle)
}
return parentview
}
But if you need to return array of views, try this:
open func setupCirclestack(parentFrame:CGRect)->[UIView]
{
let arrayColor = [UIColor.yellow,UIColor.blue,UIColor.red]
let baseCircle = Circle.init(frame: parentFrame)
baseCircle.backgroundColor=UIColor.purple
var circleArray = [Circle]()
for i in 0...2
{
let circle = self.getInnerCircle(currentFrame: parentFrame)
circle.backgroundColor = arrayColor[i]
circleArray.append(circle)
}
return circleArray
}
I am using a custom path animation on UIImageView items for a Swift 3 project. The code outline is as follows:
// parentView and other parameters are configured externally
let imageView = UIImageView(image: image)
imageView.isUserInteractionEnabled = true
let gr = UITapGestureRecognizer(target: self, action: #selector(onTap(gesture:)))
parentView.addGestureRecognizer(gr)
parentView.addSubview(imageView)
// Then I set up animation, including:
let animation = CAKeyframeAnimation(keyPath: "position")
// .... eventually ....
imageView.layer.add(animation, forKey: nil)
The onTap method is declared in a standard way:
func onTap(gesture:UITapGestureRecognizer) {
print("ImageView frame is \(self.imageView.layer.visibleRect)")
print("Gesture occurred at \(gesture.location(in: FloatingImageHandler.parentView))")
}
The problem is that each time I call addGestureRecognizer, the previous gesture recognizer gets overwritten, so any detected tap always points to the LAST added image, and the location is not detected accurately (so if someone tapped anywhere on the parentView, it would still trigger the onTap method).
How can I detect a tap accurately on per-imageView basis? I cannot use UIView.animate or other methods due to a custom path animation requirement, and I also cannot create an overlay transparent UIView to cover the parent view as I need these "floaters" to not swallow the events.
It is not very clear what are you trying to achieve, but i think you should add gesture recognizer to an imageView and not to a parentView.
So this:
parentView.addGestureRecognizer(gr)
Should be replaced by this:
imageView.addGestureRecognizer(gr)
And in your onTap function you probably should do something like this:
print("ImageView frame is \(gesture.view.layer.visibleRect)")
print("Gesture occurred at \(gesture.location(in: gesture.view))")
I think you can check the tap location that belongs imageView or not on the onTap function.
Like this:
func ontap(gesture:UITapGestureRecognizer) {
let point = gesture.location(in: parentView)
if imageView.layer.frame.contains(point) {
print("ImageView frame is \(self.imageView.layer.visibleRect)")
print("Gesture occurred at \(point)")
}
}
As the layers don't update their frame/position etc, I needed to add the following in the image view subclass I wrote (FloatingImageView):
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let pres = self.layer.presentation()!
let suppt = self.convert(point, to: self.superview!)
let prespt = self.superview!.layer.convert(suppt, to: pres)
return super.hitTest(prespt, with: event)
}
I also moved the gesture recognizer to the parent view so there was only one GR at any time, and created a unique tag for each of the subviews being added. The handler looks like the following:
func onTap(gesture:UITapGestureRecognizer) {
let p = gesture.location(in: gesture.view)
let v = gesture.view?.hitTest(p, with: nil)
if let v = v as? FloatingImageView {
print("The tapped view was \(v.tag)")
}
}
where FloatingImageView is the UIImageView subclass.
This method was described in an iOS 10 book (as well as in WWDC), and works for iOS 9 as well. I am still evaluating UIViewPropertyAnimator based tap detection, so if you can give me an example of how to use UIViewPropertyAnimator to do the above, I will mark your answer as the correct one.