iOS, UIBarButtonItem: Custom label bound to UIBarButtonItem - ios

I have an issue with attaching a custom label bound to my UIBarButtonItem. My code:
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0.00, 0.00, 10.0, 10.0)];
lbl.font=[UIFont fontWithName:#"Verdana" size:7];
lbl.textAlignment=NSTextAlignmentCenter;
lbl.textColor=[UIColor darkGrayColor];
lbl.text=#"Subtypes available:";
lbl.backgroundColor=[UIColor lightGrayColor];
_btnSubtypes.enabled=YES; // UIBarButtonItem
_btnSubtypes.title=[NSString stringWithFormat:#"%lu / %lu",[_ucqc returnQuantityOfListedSubtypes:_coin],[_ucqc returnPureQuantityOfSubtypesInType:_coin]];
[_btnSubtypes setAction:#selector(navigateToSubtypes)];
[_btnSubtypes.customView addSubview:lbl];
This code is executed in viewWillAppear method. So, it doesn't set label. What's wrong with it?
Thank you in advance.

Hi I think is better in order to custom use UIButton than UIBarButtonItem. In your code you don't have any customView. If you insert a NSLog statements you can see than is nil.
[_btnSubtypes.customView addSubview:lbl];
NSLog(#"have I go customView? %#",[_btnSubtypes.customView description]);
You should change this:
_btnSubtypes.customView=lbl;
But It´s posible the result not match with you expect.

Related

Objective c - new UILabel text is 'merged' with the existing

I am refactoring an 'old' Objective-C iOS-program. All layout is done programmatically without storyboard.
I am experiencing some odd behavior when I try to update the text on a UILabel. The text is NOT updated but written 'on top' of the existing text.
The context is:
The UILabel is defined in a 'top-bar' that is managed by a sub-ViewController. It is assigned as 'default'/placeholder value when initialized by the sub-ViewController.
lblArea = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
//self.lblArea.text = #"giraf";
lblArea.textColor = BLACK_COLOR;
lblArea.backgroundColor = [UIColor clearColor];
lblArea.textAlignment = NSTextAlignmentCenter;
lblArea.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14];
//self.lblArea.text=SEARCH_FOR_ADDRESS;
NSAttributedString *placeholderText = [[NSMutableAttributedString alloc] initWithString:SEARCH_FOR_ADDRESS attributes : #{
NSForegroundColorAttributeName : GRAY_COLOR,
}];
lblArea.attributedText=placeholderText;
[self.view addSubview:lblArea];
The setter-method is
-(void)setLabelAreaText:(NSString *)labelText{
lblArea.text=labelText;
}
In the parent controller the sub-controller is added as follows:
[self addChildViewController:topBarViewController];
[topBarViewController didMoveToParentViewController:self];
[self.view addSubview:[topBarViewController view]];
When the label is assigned another value from the parent-ViewController (via a setter method) the text is not replaced but written 'on top' of the existing.
The UILabel is defined as a instance variable and is made accesiable via a custom setter-method. I have tried to change it to a public property but it doesn't change anything.
Can anyone please help with an explanation :-)

Set rounded corners globally on UIButtons in iOS application

Is there a way to set UIButtons with rounded corners globally like with color below?
[[UIButton appearance] setBackgroundColor:[UIColor purpleColor]];
The list of properties that you can set using UIAppearance is found here:
What properties can I set via an UIAppearance proxy?
Unfortunately rounded corners are not something that is possible.
You could use something like beautify (https://github.com/beautify/beautify-ios) which enhances the UIKit controls to allow you to specify rounded buttons.
With beautify, the following would give you rounded buttons globally:
BYTheme *theme = [BYTheme new];
theme.buttonStyle.border = [[BYBorder alloc] initWithColor:[UIColor blackColor]
width:2.0f
radius:5.0f];
[[BYThemeManager instance] applyTheme:theme];
I've found this link. Please see if it could help.
Taming UIButton
It is using this
[[basicButton layer] setCornerRadius:18.0f];
As i mentioned in previous answer. You have to subclass UiButton for it.. :)
#import <QuartzCore/QuartzCore.h>
Add this in your header file.
then in implementation,
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
cornerRadius will do the trick for you.. Let me know if more info needed.. :)
Edit
This cannot be achieved globally. As you used appearence, here is the list to see what you can customize with UIAppearance. what you can do is you can create a subclass of your UIButton, & there you can write implementation of setCornerRadius in initWithCoder Method.
Migrating advice for a workable solution to Swift (it's also possible using an equivalent ObjC category on UIView):
1. Add this extension https://gist.github.com/d3ce2e216884541217d0
2. Code:
let a = UIButton.appearance()
a.layerCornerRadius = 20.0
a.layerBorderColor = UIColor.redColor().CGColor
a.layerBorderWidth = 2.0
This sort of hack works because of how properties are copied. All appearance changes to things like a.layer and a.titleLabel are not propagated, but extension properties are copied.

How to implement tag functionality like tumblr

I want to implement tagging functionality like tumblr. I tried with UITextfield and added UIButtons for tags_name in textfield.
How to select tag on first back and delete it on second back?
How tags should be added in multiple lines in UITextfield?
please suggest any solution for this functionality.
here is the code how I added buttons in UItextfield with array.
for (count_value = 1; count_value<=[textfieldarray count]; count_value++)
{
UIButton *button_dish = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button_dish setTag:count_value];
[button_dish.layer setOpacity:0.5];
[button_dish.layer setCornerRadius:4.0f];
[button_dish setUserInteractionEnabled:YES];
[button_dish setShowsTouchWhenHighlighted:YES];
[button_dish setContentMode:UIViewContentModeCenter];
[button_dish setTitleEdgeInsets:UIEdgeInsetsMake(8, 8, 4, 8)];
[button_dish.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[button_dish.titleLabel setFont:[UIFont fontWithName:#"Eau-SansBold" size:12]];
[button_dish setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[button_dish setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button_dish setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button_dish setContentMode:UIViewContentModeScaleAspectFit];
[button_dish setTitle:textfieldarray[count_value-1] forState:UIControlStateNormal];
CGSize expected_Size1 = [textfieldarray[count_value-1] sizeWithFont:button_dish.titleLabel.font constrainedToSize:button_dish.frame.size lineBreakMode:NSLineBreakByWordWrapping];
[button_dish addTarget:self action:#selector(buttonactionmethod:) forControlEvents:UIControlEventTouchUpInside];
left_view_length1 = left_view_length1+expected_Size1.width+16+12;
[iconview1 setFrame:CGRectMake(0, 10, left_view_length1, 24)];
[button_dish setFrame:CGRectMake(x_cordinate1, 2, expected_Size1.width+16, 20)];
x_cordinate1 = x_cordinate1+expected_Size1.width+16+12;
[iconview1 addSubview:button_dish];
[textfield1 setLeftView:iconview1];
}
Here is the sample reference image for tumblr tag,
1st image shows selection of button on first back.
2nd image shows multiple line tags.
Please give me some reference or hint.
That'd be called a "token field." They first appeared in Mail.app, and eventually became part of the AppKit framework. Documentation here.
It should be pretty easy to reproduce that look in Cocoa Touch. In Cocoa Touch, every view is backed by a Core Animation layer, and layers have a cornerRadius attribute according to your UI. Set that attribute to half the height of the field to create rounded ends. You can set a background color if you're okay with a flat appearance, or you can draw a subtle gradient to give a more convex appearance.
Of course, you only need to do all that if you want a reusable view where you can set the text and so forth. If you only need a few of these for custom buttons or something, it might be easier to just draw them in your favorite drawing program.
Another possibility is to use a third party control, such as any of:
TITokenField
JSTokenField
RMStokenview
COTokenField (part of COPeoplePickerViewController)
Reference from this SO Answer.

How do I make a UISwitch under iOS 7 not take the background colour of the view behind it?

It looks like this whenever off:
While I'd prefer more of a grey background. Do I really have to use a UIImageView?
Here is how I changed the fill color of my iOS7 UISwitch.
First you need to import QuartzCore.
#import <QuartzCore/QuartzCore.h>
Then set the background color and round the UISwitch's corners.
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
[self addSubview:mySwitch];
This will give you a UISwitch with a custom off (background) color.
Hope this helps someone:)
You can set the setOnTintColor property of your UISwitch to the color you desire.
You can also set this for the switch in Interface Builder. Just set the background colour of the UISwitch to whatever colour you want (white, in the example below), then set a User Defined Runtime Attribute of layer.cornerRadius = 16:
There's no API support for changing the off fill color of a UISwitch.
Adjusting the tintColor will only affect the outline, and adjusting the backgroundColor will affect the whole frame, including the parts outside the rounded bounds.
You either have to place a properly shaped opaque UIView behind it or - easier - use a custom open source implementation, such as MBSwitch, which allows you to set the off fill color.
You can also use an image as background, using the [UIColor colorWithPatternImage];
mySwitch.onTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-on"]];
mySwitch.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-off"]];
Adding to Barry Wyckoff solution : set tint color also
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
mySwitch.tintColor = [UIColor redColor];
[self addSubview:mySwitch];

using a single UIslider as a color picker dont work

I want the user to pick a color using a single uislider to change a background on a label and I know i need to use the colorWithHue :saturation: brightness: alpha function and that the hue is meant to be the slider value and that the other are seposed to stay 1.0.
but it just don't work!!!
Can somebody help me with writing the function write?
[slider addTarget:self action:#selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
-(void)sliderValueChanged:(UISlider *)sender {
NSLog(#"slider value changed:%f", sender.value);
label.backgroundColor = [UIColor colorWithHue:sender.value saturation:1.0 brightness:1.0]
}
If you're using Interface Builder make sure you connect the method to the IBOutlet.

Resources