Clear the attributed text of textfield from the IB clear button - ios

I am not able to clear the attributed text of UITextField when the Interface Button clear button is triggered.
I have tried _textfield.attributedText = nil; but it doesn't work. What happens next is that the UI freezes and I am not able to interact with the UI anymore.
Moreover, the attributedText is not removed from the UI.
Note:
I can't use text property of textfield because I have an icon and a string inside my attributedText. Hence, I must use this.
Any help is much appreciated.
Ronak

Why don't you try setting an empty attributed string:
let emptyAttributedString = NSMutableAttributedString()
textView.attributedText = emptyAttributedString
Perhaps you could place this within the IB Action hooked to your clear button?

Related

Why is UIButton.titleLabel?.text always switching back after I changed it in Swift 5 (UIKit)?

I ran into an issue with my UIButton.
After the User presses the button, it activates an IBAction which is supposed to change the titleLabel.text of my UIButton.
self.btnAppleHealthKit.titleLabel?.text = "Already pressed."
Whenever I press the button, the text changes for about a quarter second to "Already pressed", but switches back immediately to the previous text.
Important Note: The whole thing is inside a completion handler but I wrapped it into DispatchQueue.main.async { }
Can you guys think of an idea why this error appears?
Thanks for your help in advance.
It seems, UIButton does not allow to configure text or color of titleLabel.
From apple doc.s
Do not use the label object to set the text color or the shadow color.
Instead, use the setTitleColor(:for:) and setTitleShadowColor(:for:)
methods of this class to make those changes. To set the actual text of
the label, use setTitle(_:for:) (button.titleLabel.text does not let
you set the text).

How to make a custom UITextField Placeholder in Swift 3.0

I'm building an app who has to obtain the address and some information of the user. I have one UIViewController with three UITextFields and I want to display the placeholders of each TextField like this picture.
Mockup Design of the UIViewController
How can I do that?
UITextField has built-in placeholder functionality. Just set the placeholder property of your text field:
textField.placeholder = "Casa 9 manzana E"
You can style the text field as needed. Take a look at the UITextField documentation for more details: https://developer.apple.com/reference/uikit/uitextfield
For multi-line, styled placeholders consider using attributedPlaceholder.
NSAttributedString allows to modify/style parts of the string differently.
You need To try This
Txtpwd.attributedPlaceholder = NSAttributedString(string: "Your Text",attributes: [ NSForegroundColorAttributeName=UIColor.darkGray])
Where TXTPwd in Outlet of Yore TextField

UITextField Alignment : Placeholder right aligned & Actual text gets left aligned

I have a UITextField set in xib. Text Alignment is set to right. When app is running, I see textfield which shows placeholder with right aligned text. When I start typing it still aligned to right. As soon as I move the cursor from that textfield to some other UI component, typed text gets left aligned.
EDIT :
I have resolved it partially in one scenario while editing in textField. But direct text assignment from local data source having same problem.
Editing Text Field Solution
- (void)textFieldDidEndEditing:(UITextField *)textField{
textField.text = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
Still facing alignment issue if I say,
textField.text = #"my Test";
Applied same logic before assigning text to textField as did in editing mode but still it gets left aligned automatically :(
Actual Output Problem
XIB Setting for UITextField
I have been using a UILabel category class for vertical alignment adjustment. In that there was a method +(void)loadand inside that method i was using following line
method_exchangeImplementations(class_getInstanceMethod(self, #selector(textRectForBounds:limitedToNumberOfLines:)), class_getInstanceMethod(self, #selector(verticalAlignTextRectForBounds:limitedToNumberOfLines:)));
method_exchangeImplementations(class_getInstanceMethod(self, #selector(drawTextInRect:)), class_getInstanceMethod(self, #selector(verticalAlignDrawTextInRect:)));
This particular lines of code caused the problem for me. It was overriding the existing default behaviour of the UITexfield. Commenting this worked for me.
So something similar would be there in your code also.
Hope this will help you :)
You can do the following:
1- set the textAligment to the right
2- add UITextFieldDelegate to your.h
3- yourTextField.delegate = self
4- Add the delegate method 'willBeginEditing' and set the textfield aligment to the left inside it
5- Add the delegate method 'didEndEditing' and set the textfield aligment to the right inside it IF the textfield.text is = #""
Happy Coding!
I have tried as same as you performed and it's working fine.
I think You have to delete that textfield and put it again with new identity.
Then Clean and Rebuild your project.
Make sure You are not giving any values or parameters to textfield programatically.

Swift disable copy, cut, paste in UISearchBar

I know how to disable the standard copy, paste, cut... options in Swift when editing a UITextField's / UITextView's text but I don't know how to do this with a UISearchBar. With a text field I would subclass UITextField and override the canPerformAction() method. But when subclassing UISearchBar this does not work and I don't know how to address the searchBar's textField. Furthermore I would really like to know, how you could change the color of the little 'X' on the right site of the search bar's text field, which is responsible for deleting all the text in the text field.
Would be great if someone could help me with these two things. Thanks in advance.
Your best bet to change the clear button color is to use a custom image by calling
setImage(_ iconImage: UIImage?, forSearchBarIcon icon: UISearchBarIcon, state state: UIControlState)
on the UISearchBar.
As to disabling copy/paste on the UISearchBar's text field, I don't think there is a way to do that. Even if you get access to the UITextField instance contained in it, there's no way you'd be able to change the behaviour for that particular instance.

Calling setAttributedText on UITextView resets the font size, and I can't figure out why

I've read about this annoying bug on Stackoverflow, where if you don't make your UITextView selectable, setting the text will cause the font to reset, which is super annoying.
This seems to only work for when setting the normal text, not attributedText, because when I set attributedText it still resets the font, even if I have my the text view set to selectable.
This is really easy to replicate. Create a text view in a storyboard for instance (how I'm doing it), set a font, then set the attributed text to a plain string with myTextView.attributedText = NSAttributedString(string: "Lorem ipsum dolor.") and it gets reset.
How do I stop this? It's super annoying.
I had the same issue and solved it with pragmatism as I'm sure it's a bug in iOS. So, my take might be not the best solution but one that'll work. Write a category for UITextView where with a method like -(void)setAttributedTextWithoutResettingFont:(NSAttributedString *)attributedText; and implement it such that your first save the current font in a temporary variable, then call the actual setter setAttributedText: on the UITextView and finally update the font again with the one saved in your temporary variable.

Resources