not added label to textField like right view - swift

I'm trying to add a label to the text field but nothing is displayed
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let my = UILabel()
myLabel.text = "text"
myLabel.textColor = .black
myTextField.rightView = myLabel
myTextField.rightViewMode = .always
}

Try to add a frame to the label such as myLabel = UILabel(frame: CGRectMake(0, 0, 50, 21)).

Make sure that the Textfield is behind the Label! Besides that it should be showing properly.

set sizeToFit(), in my case it's help

Related

Swift UIKIT components do not show on a working view

I'm turning my hand from years of Java / C++ / C to trying to learn Swift and how to develop on Apple.
I've tried a lot of tutorials and for what I want to achieve I need to use a splitview controller.
I need to have a few different detail views and can't work out how to do this via storyboard and thought I'd try to do it with code.
Following a tutorial and several google searches Im hitting a problem which I realise is me being a newbiee and ask for your help.
To keep things simple I created a test viewcontroller class and set it in the storyboard no problem.
import UIKit;
class tesctVC : UIViewController {
var scoreLabel: UILabel!
override func loadView() {
view = UIView()
view.backgroundColor = .white
scoreLabel = UILabel()
scoreLabel.translatesAutoresizingMaskIntoConstraints = false
scoreLabel.textAlignment = .right
scoreLabel.text = "Score: 0"
view.addSubview(scoreLabel)
// more code to come!
}
}
When I change to background color it shows as expected.
But when I try to add anything else, I've tried UILabels, UIButtons etc, they do not show.
Would you please give me some pointers?
Thanks again.
I thought this may help others.
This is still WIP but shows some hints I found along the way.
I want to add a bunch of labels & buttons etc.
This in so far as is written does the job. It needs more formatting and so on.
The extension is needed to set a stacks background color. I think someone explained its because a StackView is not a drawable object.
extension UIStackView {
func addBackground(color: UIColor) {
let subView = UIView(frame: bounds)
subView.backgroundColor = color
subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
insertSubview(subView, at: 0)
}
}
class DetailViewController: UIViewController , UIWebViewDelegate {
var usernameEdt: UITextField!
var passwordEdt: UITextField!
var statusLabel: UILabel!
var LogubButton = UIButton()
// Draw the login page
func loginPage() {
// Create the top level statck
let stackView = UIStackView()
stackView.addBackground(color: .white)
stackView.axis = .vertical
stackView.alignment = .center // .leading .firstBaseline .center .trailing .lastBaseline
stackView.distribution = .fillEqually // .fillEqually .fillProportionally .equalSpacing .equalCentering
// Add the title
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.font = UIFont.boldSystemFont(ofSize: 20.0)
label.text = "Login Details"
label.textColor = .blue
stackView.addArrangedSubview(label)
// Add Username
usernameEdt = UITextField()
usernameEdt.placeholder = "Enter Your Username"
stackView.addArrangedSubview(usernameEdt)
// Add Password
passwordEdt = UITextField()
passwordEdt.placeholder = "Enter Your Password"
stackView.addArrangedSubview(passwordEdt)
self.view = stackView
}
Thanks
Jeff

tableView not scrolling properly inside UIView

I have a tableView made programmatic. I initialised the size of the section and the row. I also put a containerView inside the storyboard which would contain the tableView. My problem is, my table view wouldn't scroll properly.
Here would be my storyboard:
Here would be my outlet for the view in the storyboard:
#IBOutlet weak var trainingDetailsContainerView: UIView!
Here would be my initialized table:
var moduleLessonsTableView: UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
// tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = Themes.gray_dec
tableView.sectionHeaderHeight = 35
tableView.rowHeight = 50
tableView.isScrollEnabled = true
tableView.register(TrainingModulesTableViewCell.self, forCellReuseIdentifier: "ModuleLessonsCell")
return tableView
}()
And here is where I would set up my tableView, heights and all:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
moduleLessonsTableView.frame = CGRect(x: 0, y: 0, width: trainingDetailsContainerView.frame.width, height: trainingDetailsContainerView.frame.height)
trainingDetailsContainerView.addSubview(moduleLessonsTableView)
}
I can try putting it in viewWillAppear and the height would render correctly, which had me realizing the bounds of the trainingDetailsContainerView is set before the other views, I think. But the problem with it is it will suddenly pop up. I really don't want that as it looks like a lag from a user's perspective.
Any help would be greatly appreciated.
I also can't tag view lifecycles so I'll just put this here.
Try to add constraints to your table view in viewDidLoad instead of setting frame. Also remove code from viewWillAppear method.
Example code:
override func viewDidLoad() {
super.viewDidLoad()
trainingDetailsContainerView.addSubview(moduleLessonsTableView)
moduleLessonsTableView.topAnchor.constraint(equalTo: trainingDetailsContainerView.topAnchor).isActive = true
moduleLessonsTableView.leadingAnchor.constraint(equalTo: trainingDetailsContainerView.leadingAnchor).isActive = true
moduleLessonsTableView.trailingAnchor.constraint(equalTo: trainingDetailsContainerView.trailingAnchor).isActive = true
moduleLessonsTableView.bottomAnchor.constraint(equalTo: trainingDetailsContainerView.bottomAnchor).isActive = true
}
I have checked - this ViewController should work:
class ViewController: UIViewController {
#IBOutlet weak var trainingDetailsContainerView: UIView!
var moduleLessonsTableView: UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = Themes.gray_dec
tableView.sectionHeaderHeight = 35
tableView.rowHeight = 50
tableView.isScrollEnabled = true
tableView.register(TrainingModulesTableViewCell.self, forCellReuseIdentifier: "ModuleLessonsCell")
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
trainingDetailsContainerView.addSubview(moduleLessonsTableView)
moduleLessonsTableView.topAnchor.constraint(equalTo: trainingDetailsContainerView.topAnchor).isActive = true
moduleLessonsTableView.leadingAnchor.constraint(equalTo: trainingDetailsContainerView.leadingAnchor).isActive = true
moduleLessonsTableView.trailingAnchor.constraint(equalTo: trainingDetailsContainerView.trailingAnchor).isActive = true
moduleLessonsTableView.bottomAnchor.constraint(equalTo: trainingDetailsContainerView.bottomAnchor).isActive = true
}
}

I do not know how to convert UILabel to UIImage.[swift] [duplicate]

This question already has answers here:
How to convert a UIView to an image
(24 answers)
Closed 5 years ago.
I am using UITextField.
I enter characters using the keyboard in UITextField.
When I tap the return button on the keyboard, I will display sentences in UILabel.
I would like to convert from UILabel to image after I display the characters in UILabel.
However, I do not know how to convert from UILabel to image.
Is there a better way?
#IBOutlet var messageLabel: UILabel!
#IBOutlet var messageField: UITextField!
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
messageLabel.text = messageField.text
//Here I would like to convert from UILabel to image.
//UILabel → image
}
You can use UIGraphicsBeginImageContextWithOptions to begin an Image context and then you can use UIView method func drawHierarchy(in rect: CGRect, afterScreenUpdates afterUpdates: Bool) -> Bool to draw the label contents on it:
let label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
label.text = "StackOverflow"
label.backgroundColor = UIColor.red.withAlphaComponent(0.2)
label.textColor = .blue
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 24)
UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0)
label.drawHierarchy(in: label.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
You can create a new IBOutlet of type UIImage, messageImage, and add the image you want into there. When you want to convert the label use the isHidden function to hide messageLabel and show messageImage.
messageLabel.isHidden = true
messageImage.isHidden = false

Edit a UITextView in a ScrollView swift

I've been looking for an answer for a while to this basic problem but can't find anything to solve it.
I have a UITextView in a UIScrollVIew, but I can't edit the text of my textView, it means when I run the project and click on the textView the keyboard does not show up and I can't edit the text.
I tried different configuration but it seems that none works.
My code :
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var imageView2: UIImageView!
var scrollView: UIScrollView!
var textView = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
imageView2 = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.contentSize = imageView2.bounds.size
scrollView.isUserInteractionEnabled = true
scrollView.isExclusiveTouch = true
scrollView.canCancelContentTouches = true
self.scrollView.delegate = self
view.addSubview(scrollView)
scrollView.addSubview(imageView2)
textView = UITextView(frame: CGRect(x:24,y: 100,width: 340,height: 290))
textView.backgroundColor = UIColor(red: 0.00, green: 1.00, blue: 0.00, alpha: 1.00)
textView.text = "bla bla bla"
imageView2.addSubview(textView)
I will probably be ashamed when I will get the answer.. I guess it's something easy but can't seem to find it. I enabled the user to interact, I thought it would be enough, which is not.
Thanks
you need to add textview in imageview before adding it into scroll view.

Text field leftview image color

how can I change the leftview image color while the user is using the keyboard? I want the image to be red if the userNameTextField doesn't contain 6 letter.
Thank you very much
here's my code for the leftview image:
userNameTextField.leftViewMode = UITextFieldViewMode.Always
userNameTextField.leftView = UIImageView(image: UIImage(named: "Clipboard-20-2"))
Just add a target to your UITextField for control event EditingChanged and check if the sender character count is less than 6. You also need to set your image rendering mode to always template and set your UITextField leftView tintColor property to red:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var userNameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
if let templateImage = UIImage(named: "Clipboard-20-2")?.imageWithRenderingMode(.AlwaysTemplate) { // it will only use your image silhouette tinted with blue or red color
let imageView = UIImageView(image: templateImage)
imageView.tintColor = UIColor.redColor()
userNameTextField.leftViewMode = .Always
userNameTextField.leftView = imageView
}
userNameTextField.addTarget(self, action: "textChanged:", forControlEvents: .EditingChanged)
}
func textChanged(sender: UITextField) {
sender.leftView?.tintColor = sender.text?.characters.count < 6 ? UIColor.redColor() : nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}