Swift Syntax for Objective C [closed] - ios

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
}

Related

Display multiple buttons side-by-side by using single button iOS 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 3 years ago.
Improve this question
In stackoverflow is showing only 2 or 3 button ie., through storyboard
Images: https://ibb.co/gSFQX2k
var consolebtn = [UIButton]()
var x = Int()
x = 0
#IBAction func pushButton(sender: UIButton) {
let btn = UIButton()
btn.frame = CGRect(x:x, y:10, width:10, height:10)
btn.setTitle("Button Title",for: .normal)
consolebtn.append(btn)
view.addSubview(btn)
x += 20 // 10 is width & 10 is gap between two button
}
Try going through blog, It uses collection view to solve the problem
https://medium.freecodecamp.org/how-to-make-height-collection-views-dynamic-in-your-ios-apps-7d6ca94d2212

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
}

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()
}

Get value of tapped UILabel [closed]

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"
}
}
}

Resources