please, how can I change the color of the text field background and the colour of place holder?
I saw this answer
here in stack, change bkgrnd
but Is not clear where to call this
[UIColor colorWithRed:0.2f green:0.3f blue:0.4f alpha:0.50001f];
shall I override the UITextField.h? where?
or where shall I change the colour?
and for the changing of the place holder, but it doesn't work!
stack, change holder.
You can overwrite UITextField and do this in the init method. But I would highly recommend to do simply after you created your UITextField
textField.backgroundColor = [UIColor colorWithRed:0.2f green:0.3f blue:0.4f alpha:0.50001f];
To change the background of your textfield :
yourTextField.backgroundColor = [UIColor yellowColor];
To change the placeholder text color :
[yourTextField setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
Related
I am looking into theming my app by setting window.appearance.
In my app, I draw some stuff inside layers. I also use Core Plot, which renders its charts in layers.
For the default aqua appearance, I just use the system colors (such as NSColor.textColor and NSColor.gridColor) and they are drawn in the correct color in CALayer. But changing the window's appearance to vibrant dark causes colors to be drawn incorrectly.
Is there any way to obtain the correct color for a givenNSAppearance? Private API is acceptable too.
If the question is not clear, here is a very simple example to show the problem.
I set up a CATextLayer that is added as a sublayer of the main view's sublayers and an NSTextFied that is added as a subview:
CATextLayer* textLayer = [CATextLayer new];
textLayer.string = #"Is this colored correctly? (Layer)";
textLayer.foregroundColor = NSColor.textColor.CGColor;
textLayer.contentsScale = 2.0;
textLayer.frame = (CGRect){0,0, [textLayer preferredFrameSize]};
NSTextField* textField = [NSTextField new];
textField.stringValue = #"Is this colored correctly? (View)";
textField.textColor = NSColor.textColor;
textField.font = (__bridge id)textLayer.font;
textField.editable = NO;
textField.selectable = NO;
textField.bezeled = NO;
textField.backgroundColor = nil;
[textField sizeToFit];
textField.frame = (CGRect){0, 60, textField.bounds.size};
[self.view.layer addSublayer:textLayer];
[self.view addSubview:textField];
On an Aqua window, both appear correctly:
However, on a dark vibrant window, the layer does not, while the text field does:
I'd like to know how to get the correct color for a given NSAppearance.
So I had an incorrect approach.
The right way to do it, is to implement -updateLayer in the view and take the colors' CGColor snapshot there. -updateLayer is called when the appearance changes, so the view can update it with the correct color values.
Instead of having a normal text view, I want it to be another shape, for instance like the Text Field. The reason to why I am not using Text Field is because I want the text view to be uneditable.
Suggestions on how to change the shape?
Thanks.
If what you really want to do is use a UITextField you have a few options:
textField.enabled = NO; // create an outlet to your text field and put this in ViewDidLoad to prevent editing but also selecting and copying
implement the delegate method: // this will allow copy/paste, etc. but no writing
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO; }
If you want to use a UITextView, the only shape items you can change are the width and height (you can't make it star shaped or anything). If you want rounded corners you could use QuartzCore as follows:
[textView.layer setBorderWidth:2.0]; // not needed but put here in case
//Round the corners via a radius value`enter code here` (play with number to get what you want)
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
Note: UITextView gives you the added benefit of multi-lines and scrolling.
If you just want to display a value that can be read and copied then use a UILabel
I am trying to add an image as a background to a UILabel, but my UILabel's title cannot be seen, even though I tried to send the background image to the back. My code is below and any advice on how to help with this would be great, thanks!
UIImageView *labelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
[myLabel addSubview:labelBackground];
[myLabel sendSubviewToBack:labelBackground];
myLabel.backgroundColor = [UIColor clearColor];
[myLabel setText:title];
When you add a view (your image view) as a subview to another view (your label), the subview will always be in front of its superview. They would either need to be siblings:
[myContainer addSubview:labelBackground];
[myContainer addSubview:myLabel];
or better yet, the label should be a subview of the image view:
[labelBackground addSubview:myLabel];
[myContainer addSubView:labelBackground];
Another solution might be to use your image as a background color for your label:
[myLabel setBackgroundColor:[UIColor colorWithPatternImage:myUIImage]];
But note that the image will be repeated instead of centered or stretched.
Try adding the UILabel as a subview of the UIImageView, rather than the other way around.
This will result in the UIImageView being the "container" of the label, and thus the label being on top of the UIImageView.
I have an NSButton (Push Button) with some temporary title text built in Interface Builder / Xcode. Elsewhere, the title text inside the button is changed programmatically to a string of unknown length (actually, many times to many different lengths).
I'd like the button to automatically be resized (with a fixed right position--so it grows out to the left) to fit whatever length of string is programmatically inserted as button text. But I can't figure it out. Any suggestions? Thanks in advance!
If you can't use Auto Layout as suggested by #jtbandes (it's only available in Lion), then you can call [button sizeToFit] after setting its string value, which will make the button resize to fit its string. You would then need to adjust its frame based on the new width.
You can't do this automatically, but it would be easy to do in a subclass of NSButton.
#implementation RKSizeToFitButton
- (void)setStringValue:(NSString*)aString
{
//get the current frame
NSRect frame = [self frame];
//button label
[super setStringValue:aString];
//resize to fit the new string
[self sizeToFit];
//calculate the difference between the two frame widths
NSSize newSize = self.frame.size;
CGFloat widthDelta = newSize.width - NSWidth(frame);
//set the frame origin
[self setFrameOrigin:NSMakePoint(NSMinX(self.frame) - widthDelta, NSMinY(self.frame))];
}
#end
This way you can just set your button's class to RKSizeToFitButton in Interface Builder and then calling setStringValue: on the button to change its label will "just work" with no additional code.
Sure! Just use Auto Layout! :)
I want to know how to center a UIImage to UIButton. I am using the setImage method of UIButton to place the UIImage..
You can use the contentMode Property:
button.imageView.contentMode = UIViewContentModeCenter;
Remove the text from the UIButton before setting the content mode
Sometimes you might have the wrong content alignment. Make sure they are both in the center with :
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;