iPhone: How to remove glow (light) from UIBarButtonItem when pressed? - ios5

I'm trying to remove the glow from a UIBarButton item so that my text appears to be a label instead of a button. I've seen various posts talking about how to do this through interface builder or by setting a boolean variable "showsTouchWhenHighlighted", but neither of these options are available to me it appears. I've tried setting the showsTouchWhenHighlighted in the .m viewDidLoad where I change the font and font-size but the UIBarButtonItem doesn't appear to have that property. I also only have the options in the following image to change in InterfaceBuilder.

There is a way to do this (a bit of a hack but it works). Just drag a UIButton into your toolbar (instead of a UIBarButtonItem). Then a UIBarButtonItem will be automatically be created for you as a superview for your UIButton. Then you just set it like this:
UIBarButtonItem
Style: Plain
Title: (empty)
UIButton
Type: Custom
Title: (your actual label title here)
Text Color: White
Shows Touch On Highlight: (Unchecked)
Here is a screenshot to use as reference:
Note: Just remember that from now on any updates on the text must be made on the UIButton

try this:
`
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(140 , 0, 50, 250)];
[label setBackgroundColor:[UIColor clearColor]];
label.text = #"TEXT";
UIView *view = (UIView *) label;
[self.barItem setCustomView:view];
`
note: self.barItem is a UIBarButtonItem added from the object library and placed between two flexible spaces.
another way is to remove the [self.barItem setCustom:view] line and change the parameters of the label (width) so that it fills the entire toolbar and set the alignment to middle and the font by yourself in code,

Related

How to use custom emoji view from iPhone default keyboard?

I am developing an iPhone application that allows user to draw emoticons and use it while comments with text (like iPhone allows user to use their emoji by enabling emoji keyboard from the settings). I want to use my own made emoticons. I store all emoticons in collection view. How can I enable that view from iPhone default keyboard, so that I can use my own custom emoticons with text?
When using a custom keyboard in your app, just use the inputView attribute of a UITextField.
You can do this in the following fashion:
#interface SCAViewController ()
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property ( nonatomic) UIView *labelView;
#end
#implementation SCAViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// here we set the keyboard to become the first responder
// this is optional...
//[_textField becomeFirstResponder];
// creating the custom label keyboard
_labelView = [[UIView alloc] initWithFrame:CGRectMake(0, 568, 320, 568/3)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
label.text = #"Label View!";
[_labelView addSubview:label];
_labelView.backgroundColor = [UIColor blueColor];
_textField.inputView = _labelView;
}
Now for the explanation:
The reason that the _labelView is set to those current dimensions is because i wanted a close match to the default keyboard. You can always change them to whatever you're needing.
I did a label being added onto the view, but since you're doing emojis, i'd create a method that will load the image into the textField by using buttons.
It would be list like the following:
create action method for button
create button
set background image of button
add button onto custom view
set inputView to the custom view
you can create the action method like so
// create the method for the button
- (IBAction) loadOntoKeyboard {
// load the image/text/whatever else into the textfield
}
you can create a button to add onto your custom view like
// creating custom button
UIButton *emoji1 = [UIButton buttonWithType:UIButtonTypeCustom];
// creating frame for button.
// x - the location of the button in x coordinate plane
// y - same as x, but on y plane
// width - width of button
// height - height of button
emoji1.frame = CGRectMake(x, y, width, height);
// just setting background image here
[emoji1 setBackgroundImage:[UIImage imageNamed:#"emoji_imageā€¦"] forState:UIControlStateNormal];
// don't forget to add target of button
[emoji1 addTarget:self action:#selector(loadOntoKeyboard) forControlEvents:UIControlEventTouchUpInside];
// adding button onto view
[customView addSubview:emoji1];
// setting the inputView to the custom view where you added the buttons
textFieldVariable.inputView = customView;
Hopefully this all makes sense to you, and I hope that I helped you understand in a clear manner on how to implement the custom keyboard actions :)
Hope this helps!

Displaying notification badge like counter in UINavigationbar

I have a requirement to display number of pending notifications in iPhone navigation bar. The appearance should be like that of notification badge - but these are not APNS notifications. They are the ones sent from private server with similar purpose.
I tried adding a right/left button (UIBarButtonItem) in my UINavigationbar but it seems like it is very rigid in appearance. I can't set its width, fonts etc. See my code:
self.notifButton = [[UIBarButtonItem alloc] initWithTitle:#"0" style:
UIBarButtonItemStyleBordered target:self action:#selector(TouchNotif)];
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:self.notifButton];
self.navigationItem.rightBarButtonItems = items;
Because of other 2 items also added to items array, navbar is cluttered. Their fonts, width etc I cannot play with, or maybe I don't know how should I create them.
My questions:
1) What is proper way to accommodate at least 3 items in navbar right area? I am asking this because I don't find a way to play with width and font of the UIButtons I use.
2) If I want to have custom appearance for my notification button (just like notification badge) - are there any pointers how do I make it? Which control to use, how to set its frame and font which will be allowed within UINavigationBar?
Please help.
You need to create a UIBarButtonItem that contains a custom view using initWithCustomView.
The custom view could be a custom UIButton with a number badge as subview. With this custom view you can also control the width of the buttons.
There is no public API to create a notification badge directly. In case of a tab bar item you could set a badge using the property badgeValue - but not with UIBarButtonItem.
Here you need to use this open source control: MKNumberBadgeView.
Note that the property rightBarButtonItems is available since iOS 5.
If you only need one item set the rightBarButtonItem instead.
UIButton * buttton = [UIButton buttonWithType:UIButtonTypeCustom];
[buttton setFrame:CGRectMake(285, 20, 20, 20)];
[buttton.layer setCornerRadius:10];
[buttton setTitle:#"23" forState:UIControlStateNormal];
[buttton.titleLabel setFont:[UIFont systemFontOfSize:12]];
[buttton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[buttton setBackgroundColor:[UIColor whiteColor]];
[self.navigationController.view addSubview:buttton];
First get the respective barbuttonitem in navigationController by
let baritem = navigationItem.right/leftBarButtonItem
baritem.badgeValue = "\(correspondingValues)"

How to achieve UIPickerView as shown in image, with titles for components?

How to achieve picker view like this? I have implemented all necessary delegate and dataSource methods to populate the data, but the thing I am not able to understand is how to add this titles adults, children and infants?
They are static and does not spin with the component!
Add the 3 labels to your view as subviews when you showing the picker view and then hiding them when the picker is dismissed.
You will have to position the labels on the band.
I got this done just using Interface Builder.
I created a container view and then put picker view inside it.
To be sure that my container size is the same as picker view I set space constraints: leading, trailing, top and bottom.
Then I put 3 labels above picker view (but they're still the subviews of the container) and set their frames to center it.
Also to achieve the same label visual effect as on the screenshot (it seems to be under selection bar) decrease label's alpha to about 0.7.
You could put the labels on a particular frame position and then make the labels background color as clearColor.
You need to add labels as subviews of the picker view. There is no functionality built into UIPickerView to make this easy.
Create your picker, create a label with a shadow, and push it to a picker's subview below the selectionIndicator view.
It would look something like this
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(135, 93, 80, 30)] autorelease];
label.text = #"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[picker insertSubview:label aboveSubview:[picker.subviews objectAtIndex:5]];
//When you have multiple components (sections)...
//you will need to find which subview you need to actually get under
//so experiment with that 'objectAtIndex:5'
//
//you can do something like the following to find the view to get on top of
// define #class UIPickerTable;
// NSMutableArray *tables = [[NSMutableArray alloc] init];
// for (id i in picker.subviews) if([i isKindOfClass:[UIPickerTable class]]) [tables addObject:i];
// etc...

Best way to add a large chunk of text to a UIScrollView?

What is the best way to display a large chunk of text (taken from a .txt file in the app) in a UIScrollView so a user can scroll about it? Length of the text is variable.
On Interface Builder open the Attributes Inspector (if not already open - command-1) and uncheck "Editable".
Also notice there's a Scroll View section below. Make sure "Scrolling" is checked.
Hope this helps somebody (the post is a year old so I guess by now the one who posted it doesn't need this info).
I came here looking for an answer and found that all answers are bad - or flat out wrong.
The proper way to do this is using UITextView by itself. Since it is a descendant of UIScrollView, it has scrolling built-in and lots of features for adjusting formatting such as the insets etc.
If you intend to only show text, you need to explicitly disable editing. You do this by setting the "editable" property to false.
And if you want to disable the text selection mechanism, set the "selectable" property to false.
In newer versions of iOS, UITextView has added support for NSTextContainer which gives you even greater control over formatting.
One way I had working for me is to create UILabel, set text and then set content size of scrollview by it size.
Here is an example
Quote:
// alocate and initialize scroll
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// alocate and initialize label
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// add long text to label
myLabel.text = #"Lorem ipsum... long text here";
// set line break mode to word wrap
myLabel.lineBreakMode = UILineBreakModeWordWrap;
// set number of lines to zero
myLabel.numberOfLines = 0;
// resize label
[myLabel sizeToFit];
// set scroll view size
myScroll.contentSize = CGSizeMake(myScroll.contentSize.width, myLabel.frame.size.height);
// add myLabel
[myScroll addSubview:myLabel];
// add scroll view to main view
[self.view addSubview:myScroll];
Usage of the UITextView into the UIScrollView. I could not recommend this because UITextView is the subclass of UIScrollView. Apple is also recommending the same.
Use UILabel in this case as a sub-view,
Put the UITextView into the UIScrollView.

UIButton title alignment and multiline support

How do I set the title of a UIButton to be left-aligned, and how can I show multiple lines of text in a UIButton?
Here's the code to do the UIButton alignment in code too: -
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
To set the alignment of a UIButton, look in the Attributes Inspector in Interface Builder for the button. There's a section called Alignment. Set it to left. Or right, or whatever you want.
To do it in code, use the contentHorizontalAlignment property of UIControl. You can set it to any of these properties:
UIControlContentHorizontalAlignmentCenter
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
None of these options look particularly good (as you can see above), and you might have might have more luck using the contentEdgeInsets property of UIButton to reposition the content.
To set a multiline title on a UIButton, check this forum post which describes changing the button label.
Or you can use this code to quickly make a 2 line button:
myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[myButton setTitle:#"Siegfried\nRoy" forState:UIControlStateNormal];
Raju, don't forget to mark questions as accepted.
Actually, you can set the lineBreakMode to UILineBreakModeWordWrap or UILineBreakModeCharacterWrap. This breaks the text into multiple lines if necessary.
Try this
myButton.titleLabel.textAlignment = UITextAlignmentLeft;
myButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
myButton.titleLabel.numberOfLines = 0;
To add a Paragraph effect you can change the size of title.