Get value of tapped UILabel [closed] - ios

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So I am currently creating dynamic labels, and I need to get their value when tapped. I am creating labels number 1 through 10 with a for loop. I then add them to the view along with UITapGestureRecognizer to detect when tapped. What I need to do is get the text of the tapped label. So if I clicked the label with the text of 1, I'd expect to have the 1 returned. Here's what I'm doing to create the label and adding the gesture recognizer.
for number in numbers.characters {
let touch = UITapGestureRecognizer(target:self, action: "numberClicked")
touch.numberOfTapsRequired = 1
let label = UILabel(frame: CGRectMake(CGFloat(x), CGFloat(y1), CGFloat(width), CGFloat(height)))
label.font = label.font.fontWithSize(38)
label.text = String(number)
label.userInteractionEnabled = true
label.addGestureRecognizer(touch)
self.view.addSubview(label)
}

Here's an example of getting the text of the label in your action function. The key change is to add a colon to the name of the tap action, indicating it takes a sender argument. Then you can access the view property of the sender to get at the UILabel itself.
for number in numbers.characters {
// add a colon after "numberClicked" to indicate it takes an argument
let touch = UITapGestureRecognizer(target:self, action: "numberClicked:")
touch.numberOfTapsRequired = 1
let label = UILabel(frame: CGRectMake(CGFloat(x), CGFloat(y1), CGFloat(width), CGFloat(height)))
label.font = label.font.fontWithSize(38)
label.text = String(number)
label.userInteractionEnabled = true
label.addGestureRecognizer(touch)
self.view.addSubview(label)
}
func numberClicked(gesture: UIGestureRecognizer) {
if gesture.state == .Ended {
if let theLabel = (gesture.view as? UILabel)?.text {
print(theLabel) // print the "1"
}
}
}

Related

how to add multiple button in stack view programmatically using array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have used DLRadioButton library. I need to add multiple buttons in vertical stack view using an array of string and load into stack view.
override func viewDidLoad() {
super.viewDidLoad()
for item in 0...3{
AccountStackView(at: item).setTitle("\(account[item])", for: .normal)
}
}
func AccountStackView(at index:Int) -> DLRadioButton {
return StackView.arrangedSubviews[index] as! DLRadioButton
}
Add in your UIViewController
override func viewDidLoad() {
super.viewDidLoad()
let array = [dLRadioButton0, dLRadioButton1, dLRadioButton2, dLRadioButton3] // change it if you already have an array of DLRadioButton buttons
for item in array {
item.setTitle("Your Text", for: .normal)
}
let yourStackView = addToStackViewButtons(array: array, andAddTo: self.view)
for item in yourStackView.arrangedSubviews as! [DLRadioButton] {
// do something with your DLRadioButton's
item.backgroundColor = .green
}
}
func addToStackViewButtons(array : [DLRadioButton], andAddTo yourView : UIView) -> UIStackView {
let sv = UIStackView(arrangedSubviews: array) // or just get link from storyboard via outlet link
sv.distribution = .fillEqually
sv.axis = .vertical
sv.frame = yourView.frame
yourView.addSubview(sv) // if you create stackview sv programmically
// set frame of add your constraints if you need
// for example:
sv.leftAnchor.constraint(equalTo: yourView.leftAnchor)
sv.topAnchor.constraint(equalTo: yourView.topAnchor)
sv.bottomAnchor.constraint(equalTo: yourView.bottomAnchor)
sv.rightAnchor.constraint(equalTo: yourView.rightAnchor)
return sv
}

Swift Syntax for Objective C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In Obj C I created a property
#property(nonatomic) UILabel *subscriptionText;
Then I created a setter method for that property UILabel. like below
-(UILabel *)subscriptionText{
if (!_subscriptionText) {
_subscriptionText = [UILabel new];
_subscriptionText.translatesAutoresizingMaskIntoConstraints = NO;
_subscriptionText.textAlignment = NSTextAlignmentJustified;
}
return _subscriptionText;
}
then In viewdidload I add this view by
[self.view addSubview:self.subscriptionText];
How can I do this same scenario in Swift 4.2.
A lazy initialization is what you need.
lazy var subscriptionText: UILabel = {
let label = UILabel()
label.textAlignment = .justified
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func viewDidLoad() {
super.viewDidLoad()
view.addSubview(subscriptionLabel)
// Label constraints
}

How to add something on an image in swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an anatomic picture and on it, I want to print a image like a dot or something when the user taps on the first image (the body) to point out where does it hurts.
I've already read something on UITapGestureRecognizer, but I don't really understood how it works.
Try this:
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap))
self.imageView.addGestureRecognizer(gestureRecognizer)
}
#objc func handleTap(tap: UITapGestureRecognizer) {
let circle = UIView()
circle.center = tap.locationInView(imageView)
circle.frame.size = CGSize(width: 30, height: 30)
circle.layer.backgroundColor = UIColor.redColor().CGColor
circle.layer.cornerRadius = 15
imageView.addSubview(circle)
}

Change typing color in Swift TextView [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to change the color by pressing typing in TextView?
For example, I type in black, and then press the button of color orange and more print text in orange color, then I press on the black button and print black text?
The studied methods can have the color of all typed text or placeholders etc.
Try this way:
#IBAction func blackButtonClicked(sender: UIButton) {
let attributedText = textView.attributedText
textView.textColor = UIColor.blackColor()
textView.attributedText = attributedText
}
#IBAction func redButtonClicked(sender: UIButton) {
let attributedText = textView.attributedText
textView.textColor = UIColor.redColor()
textView.attributedText = attributedText
}
You can also achieve this by setting the typingAttributes property of the textView, which seems to be a nicer solution:
#IBAction func redButtonClicked() {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.redColor()
}
#IBAction func blackButtonClicked() {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.blackColor()
}

Animate letters in a label or text view [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to perform an effect like this in my app:
How can I do this? I need to animate single letters in the text and not the entire label. I'm using Swift.
EDIT (more details):
I need to animate the letters from right to left using a sort of spring damping (like UIView animation), but I don't know how to achieve this. The most important part of this animation is the first one, where the letters enter from left to right with a variable space between the first ones and the others
A while back I asked a similar question and got this reply from
https://stackoverflow.com/users/4754400/chris-gulley
A very clever piece of code indeed.
class Timer {
typealias TimerFunction = (Int)->Bool
private var handler: TimerFunction
private var i = 0
init(interval: NSTimeInterval, handler: TimerFunction) {
self.handler = handler
NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "timerFired:", userInfo: nil, repeats: true)
}
#objc
private func timerFired(timer:NSTimer) {
if !handler(i++) {
timer.invalidate()
}
}
}
class ViewController: UIViewController {
let text: NSAttributedString = {
let font = UIFont(name: "Georgia", size: 18.0) ?? UIFont.systemFontOfSize(18.0)
return NSAttributedString(string: "A long time ago, in a galaxy far, far away ...", attributes: [NSFontAttributeName: font])
}()
override func viewDidLoad() {
super.viewDidLoad()
let textView = UITextView(frame: CGRect(x: 100, y: 120, width: CGRectGetWidth(self.view.frame), height: CGRectGetWidth(self.view.frame)-20))
self.view.addSubview(textView)
let _ = Timer(interval: 0.1) {i -> Bool in
textView.attributedText = self.text.attributedSubstringFromRange(NSRange(location: 0, length: i+1))
return i + 1 < self.text.string.characters.count
}
}
}
Displaying text one character at a time in swift 2.0

Resources