Four digit Pin Entry/ OTP screen in iOS [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can anyone suggest me how to create a 4 digit pin entry screen like One time password, User will enter and one circle will be checked/darken.
I know I can create this by using images and hidden text field. But I am unable to decide best way for the same.
Any help will be appreciated.

Imagine you have a UITextField. Getting the user's input to show up as black dots (and not expose their information) is as easy as doing this (assuming the UITextField is called field, and that you're using Swift).
field.secureTextEntry = true
Honestly I'm not totally sure what you're asking but this is my best guess.

My best guess to achieve this using image. You can simply add image on your UITextField's subview.
func addImageToTextField(txtPin : UITextField)
{
let imageView = UIImageView();
let image = UIImage(named: "darkImage.png");
imageView.image = image;
imageView.frame = CGRect(x: 0, y: 0, width: txtPin.frame.width, height: txtPin.frame.height)
txtPin.addSubview(imageView)
}
And call this method from textFieldshouldChangeCharactersInRange
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
textField.textColor = UIColor.clearColor()
if (!string.isEmpty) {
self.addImageToTextField(textField)
}
Similarly you can do the same if user hits back button to clear the textfield
I have done this and works like a charm for me. Cheers !! happy coding.

Related

Scroll view is scrolling when content is fully visible [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a problem: my scroll view scrollable, when content is fully visible. What should i do, to enable scrolling, when some part of content is hidden?
This is the solution. I faced the same issue few months back.
public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
ScrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
ScrollView.isScrollEnabled = true
ScrollView.showsVerticalScrollIndicator = false
}
Enjoy!

How to Customize price button text in swift, iOS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have 2 UIButton as shown in below image. Here I need to customize exactly the same for both, mean texts, how could I do this.
Suggest how could I do this either in storyboard (using label/view ..) or programmatically. Guide me with some piece of code for NSAttributtedstring in case of label or button text as shown in image.
You can use this below func :
extension UILabel {
func setAttributes(price : String) {
let font:UIFont? = UIFont.boldSystemFont(ofSize: 22)
let fontSuper:UIFont? = UIFont.boldSystemFont(ofSize: 15)
let aDotRange = (price as NSString).range(of: ".")
let attString:NSMutableAttributedString = NSMutableAttributedString(string: price, attributes: [.font:font!])
attString.setAttributes([.font:fontSuper!,.baselineOffset:5], range: NSRange(location:0,length:1))
attString.setAttributes([.font:fontSuper!,.baselineOffset:5],
range: NSRange(location:aDotRange.location,length:4))
attString.setAttributes([.font:font!],
range: NSRange(location:1,length:aDotRange.location - 1 ))
attString.setAttributes([.font:fontSuper!],
range: NSRange(location:aDotRange.location + 4,
length: (price.count) - (aDotRange.location + 4) ))
self.attributedText = attString
}
}
Dummy code :
lblPrice.setAttributes(price: "$249.99 / mon")
lblPrice.layer.cornerRadius = 4
lblPrice.layer.borderWidth = 1.0
lblPrice.layer.borderColor = UIColor.blue.cgColor
lblPrice2.setAttributes(price: "$999.99 / 6 mo")
lblPrice2.layer.cornerRadius = 4
Output :
You can use UIViews and UILabels for designing the buttons and add tapGestures to the UIViews for User interactions.

Changing custom made attribute inspector value with swift [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have made a custom attribute inspector for my text field:
#IBInspectable var bottomColor: UIColor = UIColor.clear {
didSet {
if bottomColor == UIColor.clear {
self.borderStyle = .roundedRect
} else {
self.borderStyle = .bezel
}
self.setNeedsDisplay()
}
}
Now I want to set bottomColor to red when the form is submitted with an empty text field. Which would be the best way to do this?
I'm putting my logic here
For do that you need to add bottom border of UITextField
UITextField border for bottom side only in swift OR How to only show bottom border of UITextField in Swift
Then you need to set color of this bottom border that you want
After remove same you need to set 0 width of bottom border of UITextField clear color of border.

IOS setting width of textfield programmatically [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to display a picker next to a textfield, when the user starts editig. After editing, the picker disappears.
Therefore I changed the width of a textfield like this in textFieldDidBeginEditing:
func textFieldDidBeginEditing(_ textField: UITextField)
{ print ("textFieldDidBeginEditing")
let newSize = CGSize(width: 90.0, height: textfield.frame.height)
let newFrame = CGRect(origin: textfield.frame.origin, size: newSize)
textfield.frame = newFrame
}
In textFieldDidEndEditing the width is reset to the old value.
This works fine the time (beginEditing, endEditing)
In following attempts the with is not changed again (the print statement is reached)
What´s going wrong.
Everything works fine in my test project, try insert textfield.setNeedsDisplay() after set new frame.

Disable "..." truncating for a UITextField

I've been reviewing the documentation for UITextField and its options are much more limited than the UITextView.
I've run into a situation where I need to handle truncating the text by forcing the bounding box to be bigger so no truncation exists.
I cannot just use .adjustsFontSizeToFitWidth because this event is only allowed when the text box is at screen width. The truncation is happening when it's not full screen length.
Right now when the user types, I log each keystroke and make sure the UITextFields box expands to fit the text. However if I use a large font, the text is getting cut off still:
"THIS IS LARGE TEXT GETTING CUT O..."
Currently I log each keystroke and run this code to size it:
func adjustFrameWidthToFitText()
{
var size = sizeThatFits(CGSizeMake(CGFloat.max,height))
frame = CGRectMake(frame.origin.x, frame.origin.y, size.width + 7, frame.height)
}
However I still get the ... cut off in some places. Is there anyway to tell if the text is being truncated and override the behaviour causing said truncation?
(Solutions in Swift & Obj-c welcome!)
Based on the answer below I tried:
func adjustFrameWidthToFitText()
{
var fontSize = font.pointSize
var atr = [NSFontAttributeName:font]
var textSize = NSString(string: text).sizeWithAttributes(atr)
frame = CGRectMake(frame.origin.x, frame.origin.y, textSize.width, frame.height)
}
But there is still truncation
extension String {
func sizeWithAttributes(atr: NSDictionary) -> CGSize {
return NSString(string: self).sizeWithAttributes(atr)
}
}
let size = textView.text.sizeWithAttributes([NSFontAttribute:textView.font])
This will return the exact size for the string including '\n' characters.
The you can use the size however you want.
ADDED
Also when I was using CATextLayer I had to add this to the attributes to get the rows, havent tested on UITextView or field though:
let style = NSMutableParagraphStyle()
style.lineHeightMultiple = 1.05
..., NSParagraphStyleAttributeName:style])
After much digging I found the root cause, and a shortcut.
Shortcut first:
func adjustFrameWidthToFitText()
{
var size = intrinsicContentSize()
frame = CGRectMake(frame.origin.x, frame.origin.y, size.width, frame.height)
}
This gives me the size of the textfield.
The problem with my code was I was calculating the resize event BEFORE the character was added to the text. So my bounds were always being calculated before the latest keystroke was added to the string.
When I changed my logic
public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
adjustFrameWidthToFitText()
return true
}
To
textFieldDidEndEditing(_:)
{
adjustFrameWidthToFitText()
}
It works.
Voila!

Resources