uibutton font subclass - cocoa

I have the following method in my UIButton class:
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.titleLabel.font = [UIFont fontWithName:#"Chalkduster" size:16];
self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.titleLabel.textColor = [UIColor greenColor];
}
return self;
}
Works great except I can't see the border around the button: any ideas why? What do I have to do extra to make the button border show?
Thank you

Do not subclass button do everything in IB it would be more better. This is how You can achieve what you want:
Select button -> go to Attributes Inspector -> Type -> select Rounded Rect. That will give You default button look.
Then select font (Press on T Letter) like this:
Also select text color behind Font.
Result:
EDIT:
Create button programmatically:
//create the button with RoundedRect type
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 150, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
//set the button's font
button.titleLabel.font = [UIFont fontWithName:#"Chalkduster" size:16];
//set the button's line break mode
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
//set the button's text color
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//button.titleLabel.textColor = [UIColor greenColor]; // don't use this because button's text color after clicking it will be blue (default).
//add the button to the view
[self.view addSubview:button];
Result:
Note: Do not use textColor because Your text color after clicking the button will be default blue.

Related

Change tint color of UITextFields clear Button

i tried to change the color of uitextfields clear button like:
for (UIView *subView in self.urlTextField.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subView;
[button setImage:[[button imageForState:UIControlStateNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];
}
}
I'm able to add a custom image this way, but the tint color won't change.
As i could´nt find a solution to change the tint color of the original picture, i´ve implemented this workaround
-(void)setClearButtonColor: (UIColor *) color forTextfield: (UITextField *) textField{
UIButton *clearButton = [textField valueForKey:#"_clearButton"];
if (clearButton) {
UIImage *image = [[UIImage imageNamed:#"textfieldClearButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[clearButton setImage:image forState:UIControlStateNormal];
[clearButton setTintColor: color];
}
}
i have created this clear button image (57x57)(#3x)
feel free to use it.

How to set button selection and unselection image?

when i am touch the button to show selected image and again touch that button unselection image shown.
I am try this coding:
UIButton *btn=(UIButton*) sender;
if (btn.selected=YES)
{
[btn setImage: [UIImage imageNamed:#"reportsel.png"] forState:UIControlStateSelected];
}else{
[btn setImage:[UIImage imageNamed:#"reports.png"] forState:UIControlStateNormal];
}
Anytime click this button not went to else condition.
For Highlighted state, use:
[btnObj setImage:[UIImage imageNamed:#"imgName.png"] forState:UIControlStateHighlighted];
In Normal State, use:
[btnObj setBackgroundImage:[UIImage imageNamed:#"imgName1.png"] forState:UIControlStateNormal];

How to update a bar button item in a toolbar

I have a single playback controller in my app on the navigation bar and is easily found in its implementation file. So I've tried to add a toolbar and move the playback controls to that toolbar, but I don't know how to reference it programmatically and therefore cannot update the image used to indicate if music can be paused or played.
You can do this a couple of ways, one is by setting the image of the UIBarButtonItem not to be confused with the backgroundImage of a button. You can add a bar button like so:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"camera.png"] style:UIBarButtonItemStylePlain target:self action: #selector(pressButton1:)];
The image should be a grayscale image somewhere around 30 x 30 in size much like a tab bar icon. Declare the bar button in the interface file and you can set the image like this:
[barButton1 setImage:[UIImage imageNamed:#"Chats.png"]];
The other way you can do this is by using a custom UIButton like so:
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 0, 64, 30);
[button1 setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState: UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"Chats.png"] forState: UIControlStateSelected];
[button1 setTitle:#"Camera" forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[button1 addTarget:self action:#selector(pressButton1:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
Declare button1 in the interface file and then you can change the image like so:
[button1 setSelected:TRUE];

NSTextField transparent background

I create transparent NSTextField
self.myTextField = [[NSTextField alloc] initWithFrame:CGRectMake(backgroundView.frame.origin.x + backgroundView.frame.size.width + 20, self.projectTitle.frame.origin.y - 30.0, 100, 20)];
self.myTextField.editable = NO;
self.myTextField.bezeled = NO;
self.myTextField.drawsBackground = YES;
self.myTextField.backgroundColor = [NSColor clearColor];
self.myTextField.selectable = NO;
self.myTextField.font = [NSFont fontWithName:#"Helvetica Neue" size:16];
[self addSubview:self.compressingTime];
And as a result text look bad.
If I set background color
self.myTextField.backgroundColor = [NSColor colorWithCalibratedRed:0.85 green:0.85 blue:0.85 alpha:1.0];
everything looks ok
I have also tried with drawsBackground = NO; Do you guys know how to fix this?
The secret is setting ALL THREE of these properties on the NSTextField...
myTextField.bezeled = NO;
myTextField.editable = NO;
myTextField.drawsBackground = NO;
There is a property in the .xib file, on the interface builder window for the text field, under attribute inspector
Check the Display Draws Background
Select a background color. Select clear color for transparent background.
As of 10.12 you can just do:
let label = NSTextField(labelWithString: "HELLO")
Came here looking for this too, and have got the background to give me a transparent grey. Key is to not have a bezel. My code below:
NSTextField *yourLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, width , height * 1.0/3.0)];
yourLabel.editable = false;
yourLabel.bezeled = false;
[yourLabel setTextColor:[NSColor blackColor]];
[yourLabel setBackgroundColor:[NSColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1]];
For completeness I had got the width and height earlier because they get used many times for layout:
height = self.window.frame.size.height;
width = self.window.frame.size.width;
I ended up using CATextLayer instead NSTextField.
I have same problem. Default appearance is empty. I try set dark mode and it work.
self.nameTextField.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark];
I had this problem just now. I fixed it by removing a property named backgroundColor from the NSTextField's superview.
I was using backgroundColor just as a convenience getter/setter for the CALayer properties on an NSView subclass. Although this property isn't documented on NSView, it looks like I had accidentally overridden a property on NSView.
Yay for subclassing! 😒
The clear color will make the current view (ie)NSTextView's background as transparent hence the color of NSView which holds the NSTextView is visible.

UITextField doesn't work

In a TabBar-Application I have on the view a small one, in which I can draw. Works perfect.
With that code:
UITextField * textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(10, 45.0, 260.0, 50)];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor]; //text color
textFieldRounded.font = [UIFont systemFontOfSize:17.0]; //font size
textFieldRounded.placeholder = #"<enter text>"; //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textFieldRounded.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textFieldRounded.returnKeyType = UIReturnKeyDone; // type of the return key
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
textFieldRounded.userInteractionEnabled = YES;
[skizzenFeldOutlet addSubview:textFieldRounded];
textFieldRounded.delegate = self;
I create a textfield. It works fine, but I can't put text on the textfield. The textfield is visible but I can draw in the view under the textfield!
Any tips?
Best regards
Andreas
because of this.
[skizzenFeldOutlet addSubview:textFieldRounded];
your textField is been flat added in your view.

Resources