If I have white text in my UITextField, the selection window (when selecting text) is invisible because the background on the little window is also white.
Any way to fix this?
Sure! The background color of the loupe always matches the backgroundColor property of the text field. (But not the background property.) Example:
textField.textColor = [UIColor whiteColor];
textField.backgroundColor = [UIColor blackColor];
If your text field absolutely requires a transparent background, you'll have to fake it—by using a background image containing the graphics underneath the text field. You may do it manually—by taking a screenshot of your interface and cropping it—or programmatically, like this:
#import <QuartzCore/CALayer.h>
...
// `view` contains the graphics underneath the text field
UIGraphicsBeginImageContext(textField.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint origin = [textField convertPoint:textField.bounds.origin toView:view];
CGContextTranslateCTM(context, -origin.x, -origin.y);
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
textField.background = image;
Since the background is drawn on top of the background color, the text field will appear transparent, and you'll be able to use any background color you want for the loupe.
I'm afraid not. The problem's existed since the earliest days of the iPhone OS—white text appears as white-on-white in the cursor-positioning loupe as well. If this is a serious problem for your app, your only real options are to change the text color or to file a radar feature request with Apple.
Glass takes color from textView.backgroundColor. So I made some dirty hack that works great:
#interface FakeBgTextView : UITextView {
UIColor *_fakeBackgroundColor;
}
- (UIColor *)backgroundColor;
- (void)setFakeBackgroundColor:(UIColor *)color;
#end
#implementation FakeBgTextView
...
- (UIColor *)backgroundColor {
return _fakeBackgroundColor;
}
- (void)setFakeBackgroundColor:(UIColor *)color {
[_fakeBackgroundColor release];
_fakeBackgroundColor = [color retain];
}
...
#end
I know this is a little old, but in iOS5, it appears to be a simulator-only issue. It will render correctly on the device.
One solution that I've employed for this issue is to change the text color to a that will show up in the loupe (but may not look as good in the overall view) while editing and then changing it back to the better display color when finished editing.
It behaves as if you were highlighting the text of the field from a visual standpoint and allows you to use your preferred color for the display of the entered data when not editing.
Set the color to a highlight color that will have enough contrast in the loupe on didBeginEditing, then change it back on didEndEditing.
Just one other possible approach and one I've used in a couple of apps.
eg.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.textColor = [UIColor colorWithRed:116.0/255.0 green:160.0/255.0 blue:246.0/255.0 alpha:1.0];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
textField.textColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0];
}
Related
Its a very basic question - but i could not find the answer to it anywhere.
I would like a back button like the default one that shows up in a navigation bar, but with a image in the background.
Even with customization, how to calculate the size/length of the button as per the title?
Thanks a ton for the help,in advance!
UPDATE:
Thanks guys! but the answer that i finally implemented was this:
#implementation UINavigationBar (UINavigationBarCategory)
-(void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor colorWithRed:(13.0/255.0) green:(183.0/255.0) blue:(255.0/255.0) alpha:1.0];
// use a custom color for the back button which i got using the digital color meter on my nav bar image :P
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
self.tintColor = color;
// use a custom background image for my navigation bar
UIImage *img = [UIImage imageNamed: Navigation_img];
[img drawInRect:CGRectMake(0,0,img.size.width,img.size.height)];
}//worked satisfactorily for me
#end
Create a UIButton using your own UIImage to mimic the shape you want.
The UIImage must be a stretchable type, that is, you would set the leftCapWidth to be the size of your back arrow
Set the UIButton's title to whatever you like
Create a new UIBarButtonItem using your new button as a custom view
Set this to your navigationItems leftBarButtonItem property.
The button will automatically size to fit your title.
Use sizeWithFont: NSString method to calculate the size of the title.
See NSString UIKit Additions Reference
To find out the width of specific text, you may use following methods.
NSString *str=#"Back";
CGSize sizeOfBack = [str sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(30, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
Let me explain above two statements.
the text that you want to have in your string.
sizewithfont method will allow you to calculate the size.
Here, sizeOfBack.width will give me the width acquired by 14System sized string.
Hope it helps to you. let me know by comments, if you have yet doubts regarding this.
There are some application in which validated text fields gets highlighted red, when the user enters wrong information into it.
I want to use this validation and highlighting technique on the iPhone too. How can I do that?
Change the border color of a TextField by using QuartzCore
#import <QuartzCore/QuartzCore.h>
[...]
textField.layer.borderWidth = 1.0f;
textField.layer.borderColor = [[UIColor redColor] CGColor];
with rounded corners
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;
you can validate the text by setting the UITextField's delegate to your controller then do something like :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range {
[self validateInput]; // Your method to check what the user is writting
return YES;
}
And in your "validateInput", change the background image if the validation fails.
Isn't UIButton supposed to become grayish/grayer when enabled=NO ?
I have a simple UIButton on a blackbackground (no custom images, no custom nothing, just dragged it with IB and changed size and title).
And when I set it programatically to become disabled it stays white as hell!
For now I'm using a small stupid workaround: hidden blackbg 0,5 alpha UIView on top of the button that becomes hidden=NO when I need to disable the button... but I would like to set the button properly...
Any thoughts?
There is another way without having to alpha the whole button:
[startButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
Then whenever you set the enabled property to NO, the button's text will automatically gray out.
There is no way to make a UIButton "grayer". But you can use that trick :
UIButton *myButton;
myButton.alpha = 0.4;
myButton.enabled = NO;
So your UIButton looks like unusable ;)
Simply make a UIButton category like the following and import #import "UIButton+StateColors.h" in the classes where you want to use it.
.h
#import <UIKit/UIKit.h>
#interface UIButton (StateColors)
-(void)makeDisabled:(BOOL)flag;
#end
.m
#import "UIButton+StateColors.h"
#define ENABLED_BUTTON_ALPHA 1
#define DISABLED_BUTTON_ALPHA 0.3
#implementation UIButton (StateColors)
-(void)makeDisabled:(BOOL)flag {
self.enabled = !flag;
self.alpha = flag ? DISABLED_BUTTON_ALPHA : ENABLED_BUTTON_ALPHA;
}
#end
And use it like this...
[self.emailBtn makeDisabled:NO];
[self.printBtn makeDisabled:YES];
It's a universal solution I hope...
I happened upon this question and Apple has published a new UIKit User Interface Catalog for working with Buttons in iOS 7.
In response to your question, the UIButton Class now exposes a property called adjustsImageWhenDisabled, which is "a Boolean value that determines whether the image changes when the button is disabled."
If this adjustsImageWhenDisabled property is set to "YES, the image is drawn darker when the button is disabled. The default value is YES."
I face the same problem because I had set background color.
I removed the background color and set it for UIControlStateNormal only and the default behaviour for enable/disable started to appear.
If you are setting background color instead of image try this category for converting UIColor to UIImage:
copied from here:
+ (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
then use this:
[self.loginButton setBackgroundImage:[UIImage imageWithColor:greenColor] forState:UIControlStateNormal];
self.loginButton.enabled = NO;
to set the color as background. Now when you enable/disable, the gray effect should appear.
I used searchbar with my UITableview. When I enter search text, the background color will be changed automatically as white color.
and also I used:
tableView.bounces=FALSE;
When I used searchbar, that time bounces also, won't work.
The output like as follows:
I need to change background color, when I searching content.
I need:
tableView.bounces=FALSE;
will work, when I searching content.
Answer for my question:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
UIImage *patternImage = [UIImage imageNamed:#"1.png"];
[controller.searchResultsTableView setBackgroundColor:[UIColor colorWithPatternImage: patternImage]];
controller.searchResultsTableView.bounces=FALSE;
return YES;
}
Set the background color of the cell to clearcolor and while reloading the table at the time of search set its(tables) background color as black.
My app's main view has a uiwebview. It is white for a split second before it has time to render the HTML the app generates.
Is there a way to make the uiwebview black, or another color, before it renders? The flash of white isn't part of my plan for a smooth visual transition.
Objective-C or MonoTouch answers are fine, I am bi-lingual.
Another thing to try is to make the view transparent:
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
This causes additional compositing work, however, so you can reset these values to something more friendly for that after your web view loads, in webViewDidFinishLoad:.
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
webView.backgroundColor = [UIColor blackColor];
webView.opaque = YES;
}
One thing you can try is put a UIView with the color you want and position it above the UIWebView with the same width and height. Then in the webViewDidFinishLoad method, set the UIView to hidden.
I can't comment yet hence the answer.
#Steve, actually it's possible to do with storyboards.
Place a UIView under the UIWebView and:
// Can be set in the storyboard
bottomView.backgroundColor = [UIColor yellowColor];
webview.alpha = 0;
Keep in mind that a lot of page fetch stuff after being loaded via JS so you still can be left with a white page couple seconds after
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
webView.alpha = 1;
}
To add to Steve Madsen's answer (since I can't comment yet). If you instanced the UIWebView in Interface Builder, you wont be able to set alpha to 0.0, but what you can do is bring up the color picker to set a background color (white, black, gray, it doesnt matter), then set the opacity of that color to zero percent, and that works.