I have a UIButton that is gray when the user enters a viewController. There is also a UITextView. If the user enters text into the UITextView, then the button should turn red, but if the text view is blank, the the button is gray. If was thinking of doing something like bellow, which does change the color to red if the user enters text, but if the user deletes the text, it stays red instead of going back to gray. Here is the code I am using:
- (void)textViewDidChange:(UITextView *)textView {
if (self.textView.text.length == 0) {
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
}else{
self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
}
if (self.titleView.text.length == 0) {
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
}else{
self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
}
NSLog(#"Typing has stopped");
}
Use if (self.textView.text.length == 0) instead of checking if the text is null.
For Swift 3:
if (self.textView.text.characters.count == 0)
You could bind the button image to the text view value and then use a value transformer to put in different colored images depending on if there's input.
Code is for OSX but hopefully it's adaptable.
#implementation DBHasTextImageTransformer
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
+ (Class)transformedValueClass
{
return [NSImage class];
}
+ (BOOL)allowsReverseTransformation
{
return NO;
}
- (id)transformedValue:(id)value
{
if ((value == NULL) || [value isEqualToString:#""]) {
return [NSImage imageNamed: #"NoTextImage"];
} else {
return [NSImage imageNamed: #"HasTextImage"];
}
}
#end
Related
I have a problem with my App. I have a View with 25 Buttons with changed cornerRadius. Since iOS 10 it takes about 5-6 seconds till this View loads.
Here is the relevant code:
- (void)setupTiles {
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
}
[self colorTilesWithArray:currentTileColors];
}
When i remove the following lines, is loads the view instantly:
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
The Buttons are grouped in an outlet collection, in case that's necessary to know.
Does anybody have a solution?
Thanks in advance!
Nico
Image 1: This takes about 5-6 seconds
Image 2: This is the View that will be loaded
TileButton.h-File:
#interface TileButton : UIButton
#property (nonatomic) int colorMode;
+ (UIColor *)blue;
+ (UIColor *)red;
+ (UIColor *)green;
+ (UIColor *)yellow;
+(UIColor *)colorForColorCode:(int)colorCode;
-(int)colorMode;
#end
TileButton.m-File:
#implementation TileButton
- (instancetype)init {
self = [super init];
if (self) {
self.layer.cornerRadius = self.frame.size.width*0.3;
}
return self;
}
+(UIColor *)blue {
return [UIColor colorWithRed:41.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1];
}
+(UIColor *)red {
return [UIColor colorWithRed:255.0/255.0 green:71.0/255.0 blue:109.0/255.0 alpha:1];
}
+(UIColor *)green {
return [UIColor colorWithRed:0.0/255.0 green:185.0/255.0 blue:30.0/255.0 alpha:1];
}
+(UIColor *)yellow {
return [UIColor colorWithRed:255.0/255.0 green:198.0/255.0 blue:26.0/255.0 alpha:1];
}
+(UIColor *)colorForColorCode:(int)colorCode {
switch (colorCode) {
case 1:
return [TileButton blue];
break;
case 2:
return [TileButton red];
break;
case 3:
return [TileButton green];
break;
case 4:
return [TileButton yellow];
break;
default:
return [UIColor blackColor];
break;
}
}
-(int)colorMode {
if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton blue].CGColor))
return 1;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton red].CGColor))
return 2;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton green].CGColor))
return 3;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton yellow].CGColor))
return 4;
else
return -1;
}
#end
Since you are creating the buttons in Interface Builder you will need to set the cornerRadius in the awakeFromNib method. It should look something like this:
#implementation TileButton
-(void) awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = self.frame.size.width*0.3;
}
https://developer.apple.com/reference/objectivec/nsobject/1402907-awakefromnib
Why can't you set the corner radius outside the for loop?
UIButton *btn = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; // set your frame here
bin.layer.cornerRadius = btn.frame.size.width*0.3; // width stays the same, set it here
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
// you need to set the button frame here
btn.frame = CGRectMake(0,0,0,0);
[btn layoutIfNeeded];
}
[self colorTilesWithArray:currentTileColors];
Assuming that TileButton is a subclass of UIButton, on initialization, you can set the TileButton to have your custom color, width, and corner radius. That way all of your buttons already have the specified customization on button creation.
I have used two delegate methods to create a place holder for textView.
Code:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if([Description.text isEqualToString:#"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:#"Helvetica" size:18]])
{
Description.text=nil;
}
if([Display.text isEqualToString:#"Display"]&&[Display.font isEqual:[UIFont fontWithName:#"Helvetica" size:18]])
{
Display.text=nil;
}
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if ([Description.text isEqualToString:#""]) // Description is textViewObject
{
Description.text=#"Description of home";
Description.font=[UIFont fontWithName:#"Helvetica" size:18];
}
if ([Display.text isEqualToString:#""])
{
Display.text=#"Display"; // Display is textViewObject
Display.font=[UIFont fontWithName:#"Helvetica" size:18];
}
return YES;
}
#end
This is working correct. But problem is that when I am clicking on any textView, the text is getting nil from all textView. This is because I have connected all textView to delegate.
I want that text should disappear on particular textView only.
you could add conditions in the delegate methods like this :
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if(textView == yourTextView1)
{
//your code for perticular textView1
}
else if(textView == yourTextView2)
{
//your code for perticular textView2
}
return YES;
}
You need to differentiate each of your UITextViews in some way.
If you are to have a small number of UITextViews on the screen, then you should save each of your UItextViews as a property. Check out this youtube video on creating IBOutlets
Then just name your UITextViews in order: textView1, textView2 etc...
Then in your delegate methods, you can check if the textView being passed in the method is equal to the property you want to delete text from.
if (textView == self.textView1) {
//delete text
} else {
//do nothing or something else
}
check if the delegate is operating on the wanted UITextView:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if(textView == Description && [Description.text isEqualToString:#"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:#"Helvetica" size:18]])
{
Description.text=nil;
}
if(textView==Display && [Display.text isEqualToString:#"Display"]&&[Display.font isEqual:[UIFont fontWithName:#"Helvetica" size:18]])
{
Display.text=nil;
}
return YES;
}
Easiest solution by #
CmKndy for this problem is https://stackoverflow.com/a/10201671/3633534
use UITextViewDelegate delegate in your class and add text #"placeholder text here..." in your textView with lightGrayColor to display by default.
Following code is for place holder in textView :
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:#"placeholder text here..."]) {
textView.text = #"";
textView.textColor = [UIColor blackColor]; //optional
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:#""]) {
textView.text = #"placeholder text here...";
textView.textColor = [UIColor lightGrayColor]; //optional
}
[textView resignFirstResponder];
}
For your question[Update]
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if(textView == Description && [Description.text isEqualToString:#"Description of home"])
{
Description.text = #"";
}
else if(textView == Display && [Display.text isEqualToString:#"Display"])
{
Display.text = #"";
}
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if (textView == Description && [Description.text isEqualToString:#""]) // Description is textViewObject
{
Description.text = #"Description of home";
Description.font=[UIFont fontWithName:#"Helvetica" size:18];
}
else if (textView == Display && [Display.text isEqualToString:#""])
{
Display.text = #"Display"; // Display is textViewObject
Display.font=[UIFont fontWithName:#"Helvetica" size:18];
}
return YES;
}
#end
I have written this code to change colour of different labels through different buttons
I am new to app development, just got stuck up in pretty basics:
-(IBAction)ButtonPressed:(id)sender
{
UIButton *btn=sender;
if(btn.tag==1)
{
lbl1.text=#"";
lbl1.backgroundColor=[UIColor redColor];
}
if(btn.tag==2)
{
lbl2.text=#"";
lbl2.backgroundColor=[UIColor greenColor];
}
else if(btn.tag==3)
{
lbl3.text=#"";
lbl3.backgroundColor=[UIColor magentaColor];
}
else if(btn.tag==4)
{
lbl4.text=#"";
lbl4.backgroundColor=[UIColor blueColor];
}
else if(btn.tag==5)
{
lbl5.text=#"";
lbl5.backgroundColor=[UIColor brownColor];
}
}
After writing the code the colour of label one is only getting changed.
I have connected all the elements through storyboard though
Try it like this
-(IBAction)ButtonPressed:(id)sender
{
NSInteger i = [sender tag];
if(i==1)
{
lbl1.text=#"";
lbl1.backgroundColor=[UIColor redColor];
}
if(i==2)
{
lbl2.text=#"";
lbl2.backgroundColor=[UIColor greenColor];
}
else if(i==3)
{
lbl3.text=#"";
lbl3.backgroundColor=[UIColor magentaColor];
}
else if(i==4)
{
lbl4.text=#"";
lbl4.backgroundColor=[UIColor blueColor];
}
else if(i==5)
{
lbl5.text=#"";
lbl5.backgroundColor=[UIColor brownColor];
}
}
Or Simply
-(IBAction)ButtonPressed:(id)sender
{
if([sender tag]==1)
{
lbl1.text=#"";
lbl1.backgroundColor=[UIColor redColor];
}
if([sender tag]==2)
{
lbl2.text=#"";
lbl2.backgroundColor=[UIColor greenColor];
}
else if([sender tag]==3)
{
lbl3.text=#"";
lbl3.backgroundColor=[UIColor magentaColor];
}
else if([sender tag]==4)
{
lbl4.text=#"";
lbl4.backgroundColor=[UIColor blueColor];
}
else if([sender tag]==5)
{
lbl5.text=#"";
lbl5.backgroundColor=[UIColor brownColor];
}
}
Also check the tags of the buttons.
Only replace this line UIButton *btn=sender;
with UIButton *btn = (UIButton *)sender;
I want to create following rating views shown below. How is it possible to do it
I have got 3 conditions.I have got label in top and below that are stars and at the end there is TextField. Good,Amazing,Horrific is set according to number of stars. When there is 1 Star i.e horrific condition we have to present four button Order issues, timing, friendliness,Cleanliness.And UITextField is shift below it
I have implemented following code
#pragma mark - Btn Action on start click
- (IBAction)Btn_FirstHorrific:(id)sender {
[self ReviewHorrific];
[self NumOfStarWorstCase];
str_deliveryBoyRating=#"1";
str_customerComment=#"Horrific";
}
- (IBAction)Btn_SecondNah:(id)sender {
[self ReviewSecondNah];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"2";
str_customerComment=#"nah!";
}
- (IBAction)Btn_ThirdItsOk:(id)sender {
[self ReviewItsOk];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"3";
str_customerComment=#"Its ok";
}
- (IBAction)Btn_FourthGood:(id)sender {
[self ReviewGood];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"4";
str_customerComment=#"good";
}
- (IBAction)Btn_FifthAmazing:(id)sender {
[self ReviewAmazing];
[self NumOfStarOthercase];
str_deliveryBoyRating=#"5";
str_customerComment=#"Amazing";
}
#pragma mark - Change star colour in Number of star changes
- (void)ReviewHorrific{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = [UIColor whiteColor];
Btn_ThirdItsOk.tintColor = [UIColor whiteColor];
Btn_FourthGood.tintColor = [UIColor whiteColor];
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Please report your issue";
lbl_RateType.text=#"Horrific";
self.View_WorstCase.hidden = FALSE;
}
- (void)ReviewSecondNah{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = [UIColor whiteColor];
Btn_FourthGood.tintColor = [UIColor whiteColor];
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Please report your issue";
lbl_RateType.text=#"Nah!!";
}
- (void)ReviewItsOk{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
Btn_FourthGood.tintColor = [UIColor whiteColor];
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Please report your issue";
lbl_RateType.text=#"It's OK";
}
- (void)ReviewGood{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
Btn_FourthGood.tintColor = UIColorFromRGB(0xff8133);
Btn_FifthAmazing.tintColor = [UIColor whiteColor];
lbl_report.text=#"Heap praise if you would like";
lbl_RateType.text=#"Good";
}
- (void)ReviewAmazing{
Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
Btn_FourthGood.tintColor = UIColorFromRGB(0xff8133);
Btn_FifthAmazing.tintColor = UIColorFromRGB(0xff8133);
lbl_report.text=#"Glad to hear it. Any comments or feedback?";
lbl_RateType.text=#"Amazing";
}
Step 1)
Add 5 UIButton to your UI file. Then in your header (.h) code, declare the UIButton like so:
IBOutlet UIButton *starOne;
IBOutlet UIButton *starTwo;
IBOutlet UIButton *starThree;
IBOutlet UIButton *starFour;
IBOutlet UIButton *starFive;
Then back in your UI file, link the star button views to your code. Then add the default (non-selected) star images to your buttons (just drag and drop to do this).
Step 2)
Back in your header code, add 5 IBAction methods. These will detect when the star buttons are pressed:
-(IBAction)press_1;
-(IBAction)press_2;
-(IBAction)press_3;
-(IBAction)press_4;
-(IBAction)press_5;
Then add a method as well. This method will handle the star images.
-(void)set_stars:(int)num :(NSString *)desc
Then declare in integer in your header file. This will tell you what the rating is, like so:
int rating;
Also declare an array (this will contain pointers to your star buttons):
NSArray *_starButtons;
Lastly, declare a UILabel to show the name for each rating (and like it to your UI), like so:
IBOutlet UILabel *rating_name;
Step 3)
In your implementation (.m) code, add in the following:
-(IBAction)press_1 {
[self set_stars:1 :#"Horrific"];
}
-(IBAction)press_2 {
[self set_stars:2 :#"nah!"];
}
-(IBAction)press_3 {
[self set_stars:3 :#"Its ok"];
}
-(IBAction)press_4 {
[self set_stars:4 :#"good"];
}
-(IBAction)press_5 {
[self set_stars:5 :#"Amazing"];
}
-(void)set_stars:(int)num :(NSString *)desc {
rating_name.text = [NSString stringWithFormat:#"%#", desc];
for (int loop = 0; loop < 5; loop++) {
if ((loop + 1) <= num) {
[((UIButton*)_starButtons[loop]) setBackgroundImage:[UIImage imageNamed:#"star_ON.png"] forState:UIControlStateNormal];
}
else {
[((UIButton*)_starButtons[loop]) setBackgroundImage:[UIImage imageNamed:#"star_OFF.png"] forState:UIControlStateNormal];
}
}
rating = num;
}
Finally, dont forget to populate the array with your buttons in the viewDidLoad method like so:
_starButtons = #[starOne, starTwo, starThree, starFour, starFive];
I am creating an app in which I have used user registration.In registration page, I have created many UITextFields.I wants that when a user select any TextField then its background Image is change. I uses the code to do that is:
- (IBAction)touchBegin:(id)sender
{
//UILabel *opt = (UILabel *)[cell viewWithTag:101];
if (_text1.isSelected) {
_text1.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text2.isSelected){
_text2.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text3.isSelected){
_text3.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text4.isSelected){
_text4.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else{
}
}
But I am not getting the result according to my need.
What should I use to get correct output?
be sure whether your textfield border style is UITextBorderStyleNone.
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"image.png"];
I think that it's better and easier to use UITextFieldDelegate methods like this, for example:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"big_star_non_rating"];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"AnImageForNormalStateOrSetNilBackground"];
return YES;
}
Hope it helps.