ios ui object check for text property - ios

I need to check if some object has text property. For example it can be an UIButton or an UILabel. But there is an issue between properties that display text for an UIButton and for an UILabel.
This is the difference:
For an UIButton we set text using setTitle: method, but for an UILabel we using another one named setText:
What I need to do with this:
I need to get all subviews and if these subviews have text property I need to change this property value with some text.
So of course I can check class of object and setup needed property, but maybe we have another approach to do it without checking class of object.

for (id item in [self.view subviews])
{
if ([item respondsToSelector:#selector(text)])
[item setText:#"text"]; // change text for UILabel
else if ( [item respondsToSelector:#selector(setTitle:forState:)])
[item setTitle:#"text" forState:UIControlStateNormal]; // change text for UIButton
}

Related

setImage:forState does not work for subclass of UIButton

I create a class RadioButton that inherited from UIButton ,and rewrite the method layoutSubViews where reset the frame of self.imageView and self.titleLabel ,when I call the method setImage:forState: of RadioButton instance,it does not work, but the method radioButton.imageView.image = [UIImage imageNamed:#""] work. It is an interesting problem ,I appreciate so much if anyone know why and share the reason .
When we set title,image for button we need to use below code
For Button
[button setTitle:#"Your Title" forState:UIControlStateNormal];
For Image
[button setImage:[UIImage imageNamed:#"Your Image Name"] forState:UIControlStateNormal];
why should not we use 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.
why should we use 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.
For Image

iOS Access dynamically created label from other method

I have created a TableView that contains UIViews, which hold a few other elements. These UIViews are created dynamically, as the data is called from a server. Inside each UIView there is a UILabel and a UIButtton. Once the button is clicked, I would like to have the corresponding label updated with some value. I was able to modify the UIButton and the view itself but unable to modify the UILabel. Here is an example of the method that is called when a UIButton is called. Right now it will change the background color of the corresponding UIView, but the label element does not work as intended. How can accomplish modifying this label element which is a subelement of the UIView?
- (void) heartPlus:(id)sender {
UIButton *button = (UIButton*) sender;
NSInteger id_num = button.tag;
UIView * view = (UIView *)[self.view.superview viewWithTag:id_num];
UILabel * label = (UILabel *)[view viewWithTag:id_num];
label.backgroundColor = [UIColor blueColor];
}
Creating the UIView along with adding the corresponding elements.
UIView * msgView = [[[UIView alloc] initWithFrame:CGRectMake(0,offSet,320,120)] init];
[msgView setTag:someID];
// Add button
UIButton * buttonUpdate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonUpdate.tag = someID;
[buttonUpdate addTarget:self action:#selector(heartPlus:) forControlEvents:UIControlEventTouchUpInside];
UILabel * labelHeart = [[[UILabel alloc] initWithFrame:CGRectMake(280,100,20,10)] init];
labelHeart.tag = someID;
// Add each element to the msgView
[msgView addSubview:buttonUpdate];
[msgView addSubview:labelHeart];
There is still some missing code, but as far as I can see you have the error in heartPlus method and viewWithTag does not return the correct view. Therefore the assignment of backgroundColor fails. The explanation is below:
You are first getting the parent view of view that contains UIButton and UILabel. I assume your view hierarchy looks something like this:
UITableViewCell
UIView (tag: someID)
UILabel (tag: someID)
UIButton (tag: someID)
So, if I assume there is no error in the line below and that it returns correct UITableViewCell (or whatever other view is the parent of both UILabel and UIButton):
UIView * view = (UIView *)[self.view.superview viewWithTag:id_num];
We can clearly see the problem with both UILabel and UIButton having the same tags. So if you ask the UITableViewCell for the view with tag, it will simply fail - returns nil.
There are multiple solutions to this problem, but the problem remains the same.
Do not give the UILabel the same tag as UIButton.
I assume you are a beginner with Objective-C so I would suggest you first look into some tutorials of how UIViews work. But to make it easier for you, here are few options:
Create a custom subclass of the UIView with UIView properties.
Use unique tags for UILabel and for UIButton. To find the correct id_num on click, use introspection and sender's superview, which is passed to you in a method.
You could easily loop through UIView's subviews property (which is an array) and find the UILabel manually - this works, if there is only one UILabel or you are looking for all of them.
But in either case, you need to rethink of how UITableView works. It is not a good practice to ask self.view.superview for viewWithTag and your method could already fail at this point.

Add dynamic text on uilabel programmatically

-(void)subImagetap:(UITapGestureRecognizer *)subimagetap
{
UIView *myV = (UIView *)subimagetap.view ;
NSArray *lbarray = subimagetap.view.subviews;
for (UIView *textV in lbarray) {
UILabel *textlb = (UILabel *)textV;
if([textlb isKindOfClass:[UILabel class]]) {
NSLog(#"%#", textlb.text);
textlb.text = #"Tapped";
}
}
}
The above mentioned code is my UITapGestureRecognizer code through with I tap on a label and changed his text to "Tapped" but my requirement is when I tap on the label the textpad of iphone will open and i could able to put text in that label.
You should use UITextFields if you want the user to be able to directly manipulate their content. Using a text field, you can still do what you're doing by modifying the text property, but UITextField supports editing with the keyboard as well.
You can even dress the text field up to look just like a UILabel through use of its borderStyle property. (Set it to UITextBorderStyleNone)

titleLable.text not working

I create an UIButton and use titleLable.text to set text ,but the text is not seen in the button so I replace it with setText method, and it show the text, I just want to know why the titleLable.text is not working
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.text=#"text";
Then used like below
[button setTitle:#"text" forState:UIControlStateNormal];
Because titleLabel is a readonly property.
titleLabel
A view that displays the value of the currentTitle property for a
button. (read-only)
#property(nonatomic, readonly, retain) UILabel *titleLabel >
Discussion
Although this property is read-only, its own properties are
read/write. Use these properties primarily to configure the text of
the button. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
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.
Availability
Available in iOS 3.0 and later.
Declared In UIButton.h
Please refer UIButton Class for more details.
Midhun's answer, while completely true, doesn't answer the question. As he says, the label property is read-only but its properties are not, including the text property.
The reason setting the text does not work is that the button itself will be setting the text internally, depending on the control state. This will override anything you've set as the text.
This is why you use setTitle:forControlState: instead. The button will use the value for the normal state for all other states, unless you tell it otherwise, but if you never set a title for any control state, it will not show the label.

Clear button on UITextView

How can I add a clear button (cross inside a circle) for UITextView like UITextField has?
Based on the answer from GhostRider a more accurate and up to date implementation:
int kClearButtonWidth = 15;
int kClearButtonHeight = kClearButtonWidth;
//add the clear button
self.clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.clearButton setImage:[UIImage imageNamed:#"UITextFieldClearButton.png"] forState:UIControlStateNormal];
[self.clearButton setImage:[UIImage imageNamed:#"UITextFieldClearButtonPressed.png"] forState:UIControlStateHighlighted];
self.clearButton.frame = CGRectMake(0, 0, kClearButtonWidth, kClearButtonHeight);
self.clearButton.center = CGPointMake(self.textView.frame.size.width - kClearButtonWidth , kClearButtonHeight);
[self.clearButton addTarget:self action:#selector(clearTextView:) forControlEvents:UIControlEventTouchUpInside];
[self.textView addSubview:self.clearButton];
And the method
- (void)clearTextView:(id)sender{
self.textView.text = #"";
}
You can use this images for the two states of the button:
just make a uibutton and put it on uitextview and set its action for clear text view;
uitextview.frame = (0,0,320,416);
uibutton.frame = (310,0,10,10);
[uibutton setimage:#"cross.png" forcontrolstate:uicontrolstatenoraml];
[uibutton addTarget:self action:#selector(clearButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
-(void)clearButtonSelected{
uitextview=#"";
}
hope you want to clear the text view text when you click on cross button above is help
if not understand then i can send you proper program for that
From product perspective, if you're going to have a clear button, you probably want to use a UITextField instead of a UITextView and UITextField supports a clear button natively - set the clearButtonMode property as such:
UITextField *textfield = ...;
textfield.clearButtonMode = UITextFieldViewModeAlways;
See screenshot:
You could use UITextFieldViewModeWhileEditing to only present the clear button while the user is actively updating the content.
There's nothing built in like there is for the UITextField. You'd have to add the view yourself (probably a UIButton) and place it correctly and also somehow get the text to wrap around it correctly. (And I don't think the latter is really possible.)
Maybe instead you should display a toolbar above the keyboard (or an inputAccessoryView if you're targeting 3.2 and later) that provides a clear button.
For me changing the .frame or the .contentInset properties did not work.
For me the best result came from:
1) adding a UIView to the controller, give it round corners and a border to mimic a UITextView.
self.viewTextBackground.layer.borderColor = [UIColor colorWithRed:171/255.0 green:171/255.0 blue:171/255.0 alpha:1.0].CGColor;
self.viewTextBackground.layer.borderWidth = 1.0f;
self.viewTextBackground.layer.cornerRadius = 9.0f;
2) place UITextView on top of this UIView. Place it so that the borders of the underlying UIView stay visible.
3) give the UITextView round corners:
self.textNote.layer.cornerRadius = 9.0f;
3) Make its width fe. 30pixels less compared to the underlying UIView. You now have space for a clear-button.
4) simply add a UIButton to your controller and place it in the top-right corner of the underlying UIView.
5) change the buttons properties: set its type to 'custom' and set its image to the image of a grey cross.
6) bind an action to the button te clear the UITextView
You can add a clear button like the one in the attached screenshot with minimal coding. Just follow these steps:
Select the storyboard and drag a UIButton into your UITextView
Set the buttons constraints
Assign a title or a background image
Create the button's IBOutlet reference and the action (see onClearPressed(_:) below) for "Touch Up Inside" in the ViewController
Implement the textViewDidChange(_:) UITextViewDelegate delegate method, and make sure to set the button's isEnabled property based on the textfield content, e.g.:
func textViewDidChange(_ textView: UITextView) {
clearButton.isEnabled = !textView.text.isEmpty
}
Implement the onClearPressed(_:) action:
#IBAction func onClearPressed(_ sender: Any) {
textView.text = ""
clearButton.isEnabled = false
}
That's it.

Resources