How to Add Interactive Uilabels on Top of a Uiimageview

How to add interactive UILabels on top of a UIImageView?

Adding label on ImageView is best approach. but you can also do it by adding button on ImageView.

I created a example where i created a ImageView on storyboard and create its outlet in ViewController class and in viewDidLoad i created a label and add it to label and add UITapGestureRecognizer to label. when user taps label we changed the label text and it's position.

class ViewController: UIViewController {

@IBOutlet weak var winterImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

let label = UILabel(frame: CGRect(x: 10, y: 0, width: self.winterImageView.frame.width - 10, height: 30))
label.textColor = UIColor.redColor()

label.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
label.addGestureRecognizer(tapGesture)
label.text = "Is Winter is coming, My Friend?"
self.winterImageView.addSubview(label)

}

Sample Image

Change label text and position in handleTap

 /// handle tap here 
func handleTap(sender: UITapGestureRecognizer) {

let senderView = sender.view as! UILabel
senderView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: senderView, attribute: .CenterX, relatedBy: .Equal, toItem: self.winterImageView, attribute: .CenterX, multiplier: 1, constant: 0).active = true

NSLayoutConstraint(item: senderView, attribute: .CenterY, relatedBy: .Equal, toItem: self.winterImageView, attribute: .CenterY, multiplier: 1, constant: 0).active = true
print(senderView.text)
senderView.text = "Yes!!! Winter is coming, My Friend!!"

}

Sample Image

You can download project from here InteractiveLabel

How to move the UIImageVIew to the location of the UILabel in real time?

I'm a bit confused by the question.

Maybe the question is asking how to arrange an image in formation with a label, and the two views will move together if the label move somewhere else. In this case I would just put both the label and image in a containing view, and reposition the containing view when the label needs to move and the image needs to follow.

Maybe the question is asking about independent labels and images on screen that are matched up somehow and must move into formation, and then after pairing up and moving into formation the label may move again, and the image should follow. I would create some object that stored the label and an optional reference to a paired image. Then I'd write a method for this object that would animate an image view into formation with the label. Then I'd write another method that paired the label with the view which would call the first method to move the image. Then I'd write another method that would move the label and call the first method to move the image to the new destination. All pairing and moving logic in the program would use this object and its methods to keep the image in formation.

Message Bubble UILabel for making an interactive app

Create an imageview with the bubble image. add a uilabel as subview of the imageview and position it.

UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
imgv.image = [UIImage imageNamed:@"bubble.png"];

//position the label to center of imageview

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(20,30,60,40)];
[imgv addSubview:lbl];
[lbl release];

[self.view addSubview:imgv];
[imgv release];

How to add a label at bottom of imageView programmatically?

Here example of adding label to image

 let picImage:UIImageView = {
let imageView = UIImageView()
let image = UIImage(named: "test")
imageView.image = image
imageView.contentMode = .scaleAspectFill
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let durationLabel: UILabel = {
let label = UILabel()
label.text = "1:20:12"
label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)
label.textAlignment = NSTextAlignment.right
label.numberOfLines = 1
label.textColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func setupView(){
// I will skip part of image setup
self.picImage.addSubview(durationLabel)
durationLabel.bottomAnchor.constraint(equalTo: picImage.bottomAnchor).isActive = true
durationLabel.leftAnchor.constraint(equalTo: picImage.leftAnchor, constant: 5).isActive = true
durationLabel.rightAnchor.constraint(equalTo: picImage.rightAnchor, constant: -5).isActive = true
}

this code will add label to image that set constraint to bottom and left and right

Sample Image

How to add a subview behind other elements?

You can use:

– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– insertSubview:atIndex:

Instead of addSubView, or use sendSubviewToBack:

How do you add UILabel and UIImageView to a CollectionView programmatically?

Create Subclass of collection view cell and Instead of UICollectionViewCell use CustomCollectionViewCell

class CustomCollectionViewCell: UICollectionViewCell {
var imageView: UIImageView?
var label: UILabel?

override init(frame: CGRect) {
super.init(frame: frame)

imageView = UIImageView(frame: self.bounds)
//customise imageview
imageView?.backgroundColor = UIColor.red
contentView.addSubview(imageView!)
label = UILabel(frame: CGRect(x: 20, y: 20, width: self.bounds.width - 20, height: 20))
//Customsize label
label?.text = "Hello"
label?.textColor = UIColor.white
contentView.addSubview(label!)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override var bounds: CGRect {
didSet {
contentView.frame = bounds
}
}
}

In View Controller updated following code:

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.title = "Vote"
collectionView?.backgroundColor = UIColor.white
collectionView?.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: cell)

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cell, for: indexPath) as! CustomCollectionViewCell
cell.backgroundColor = UIColor.lightGray
return cell
}


Related Topics



Leave a reply



Submit