How to use UITextField UIAppearance based on user Interaction enabled trait - ios

I am trying to find a way to have UITextField appearance is distinguishable when user interaction is disabled. I have to do this application wide. Thought using UIAppearance could be an easy way to do. But couldn't figure out how to use that with user interaction enabled trait of text field.
Can you help? Or Is there an alternate way to do the same thing centrally.
Note: Text field user interaction enabled state on a given instance can get toggled based on user action.
Thanks

I don't know of an app wide way to do it if you're using text fields directly, but if you were to subclass UITextField and override setEnabled:, you could make a change to the background there.
#implementation MYTextField
- (void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
self.backgroundColor = [UIColor lightGrayColor];
}
#end
There is already a faint grey background for UITextFields which are disabled, but if this is not enough you can do more as above. Alternatively, you could use setDisabledBackground: on each UITextField to set an image for display when disabled - I accept that this isn't a great solution with the current design paradigms.

Related

iOS SDK: Difference between UIButton setTitleForState and UIButton titleLabel.text

I have this issue with a custom UIView where I have a UIButton subview, I want to set the button's text on initialization based on some condition like this:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
self.testButton.titleLabel.text=#"Some Title";
}
Nothing happens and the button's text is the same as defined in the nib file, however if I change the implementation to:
- (void)awakeFromNib {
[super awakeFromNib];
//check for some conditions
[self.testButton setTitle:#"Some Title" forState:UIControlStateNormal];
}
It works as expected.
Can somebody please explain to me the difference between the two approaches? and when to use each?
EDIT:
the suggested answer doesn't explain my situation, I tested changing the button's text from another button's action like this:
- (IBAction)otherButtonClicked:(id)sender {
self.testButton.titleLabel.text=#"Some Title";
}
and the button's text changed. I just want to understand that behaviour.
Exact answer is
titleLabel
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.
The titleLabel property returns a value even if the button has not
been displayed yet. The value of the property is nil for system
buttons.
setTitle
Use this method to set the title for the button. The title you specify
derives its formatting from the button’s associated label object. If
you set both a title and an attributed title for the button, the
button prefers the use of the attributed title over this one.
At a minimum, you should set the value for the normal state. If a
title is not specified for a state, the default behavior is to use the
title associated with the UIControlStateNormal state. If the value for
UIControlStateNormal is not set, then the property defaults to a
system value.
Title label access is given to adjust other properties of the label such as font but setting titleLabel text does not work.
It is because UIButton class has inner implementation to set the text based on different states of the button like selected/highlighted etc which overrides label text.
The accepted answer is correct, I just add this here to elaborate a bit (comments are too short)
There's not much to explain here. The title is simply not meant to set the text at all. My guess is that the internal workings of UIButton make it save the text somewhere else as well (for different states). It uses a normal UILabel to eventually display that, because that's convenient and easy to understand. Setting the text of that does not change the button in all cases, which probably ultimately depends on the drawing cycle. I assume when it's drawn, laid out, or the like it "replaces" the label's text with its other saved variant at some point.
Now you might wonder why Apple did then expose the UILabel and thus seemed to make the text editable then.
Legacy is probably one aspect of this decision (IIRC you could once set the button's title that way). Although old code doesn't result in the desired behavior, it at least didn't crash immediately. Also, a lot of code simply wants to get the text and expects a label, that works perfectly fine as ever.
Second, designing it totally different seems overkill. To avoid that, they would have to use a subclass of UILabel which prevents you from setting the text or something and use that. Or skip it (and thus legacy support) completely and only offer the setTitle:forState: method. That seems like a bit much for a simple text container like a Label.
Ultimately it's a design choice made by Apple. You can't set the title text directly and there's no case in which you should do it any way other than by using setTitle:forState:

Styling Buttons in IOS Xcode

I am new to IOS development. In windows phone development we use to define a style(appearance e.g) of the button control in app Global place and just use that style anywhere on any button in the app by assigning the style property.
Is there anything similar in IOS XCode? I know I can customize the button in the property and appearance windows in XCode. But in this way I have to give style to each and every similar button in the app. and if there is any change I have to change it everywhere. What IOS developers do in these kind situations?
If you want to style all buttons in your app in a standard way, then you would style the appearance proxy for UIButton. You get one by calling the class method UIButton.appearance(), and then styling the returned value the way you want your buttons to look. You can also do a more fine-grained appearance by calling methods like UIButton.appearanceWhenContainedIn().
Create a UIButton subclass.
For example for creating a UIButton with rounded corners and a border:
Header file
#import <Foundation/Foundation.h>
#interface XYRoundedCornerButton : UIButton
#end
Implementation file
...import headers etc...
#implementation XYRoundedCornerButton
- (void)awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = YES;
self.layer.borderRadius = 1.0f;
self.layer.borderColor = UIColor.redColor;
}
#end
If you are using storyboard, You don't need to add any code in class.
Just drag one button and set all the properties you want, whether background colour, corner radius etc, whatever you want. And then either copy it and paste wherever you want or select the button, ALT+DRAG it .

UITextField customization for how text is selected or inserted

I'm planning to work on a small customization of UITextField to change a certain UI/UX design features.
I know UITextField has a certain inbuilt methods to change the border style, color, etc. But if I wish to make a lot of customization, can someone guide me how I should begin?
Do I need to refer to the UITextField parent class to modify it? Or I will have to make a custom UI object right from the scratch?
EDIT:
The customization I plan on working is how we select/de-select or choose an insertion point for the text in the UITextField.
It sounds like you're going to want to take advantage of the fact that UITextField adopts the UITextInput protocol. You can do that by subclassing UITextField. Alternatively, it might be sufficient for your needs to set up some other class as a delegate of this UITextField - that depends on whether the UITextFieldDelegate notifies you with a sufficiently fine grain that the user is up to something in the text field.

change the background custom keyboard with switch- or buttons To be presented in ViewController

How To add Image To background custom keyboard?
I want to set the number of images they have the background and the user can select the image that suits him
I use the language of "swift"
This have been asked before.
Anyhow:
You need to change the keyboardAppearance for a certain textfield you want the keyboard to be changed for. You can do this via the differnet UIKeyboardAppearances - more about the different types of appearances you can find in apple's UITextInputTraits API.
i.e:
textField.keyboardAppearance = UIKeyboardAppearanceDark;
Would look like:
textField.keyboardAppearance = UIKeyboardAppearanceLight;
Would look like:

Use UISlider without user interaction

I've got a customized UISlider that I want to use to display information to the user with and I don't want the user to be able to interact with the slider. I've tried
mySlider.enabled = NO;
but the slider becomes greyed out, which does not look the way I want it to look.
So, how do I set a UIControl to disabled without "greying" it out.
mySlider.userInteractionEnabled = NO;
Don't you think it's going to confuse users to present an enabled slider that doesn't respond to touches? A UISlider doesn't just display information, it also tells the user that the information is user-adjustable.
You should come up with your own information display that doesn't look user-adjustable.
userInteractionEnabled probably doesn't works for the UISlider (and why it exists??) but it works for its superview. So, try attaching the UISlider to another auxiliary (transparent?) NSView and then set userInteractionEnabled = NO to this auxiliary view.

Resources