Changing Size of Text on a UIButton - iphone

Client wants me to do something, i have just checked and i m quite sure its not possible.
He asked to put a text on a Button ( UIButton ). (Default State Configuration)
And when the user clicks it the text should enlarge. (Highligted State Configuration)
I have checked by selecting Highligted State Configuration and then changed the size of text in the Interface Builder but i didnt work . The text changed for Default State Configuration as well. Please tell me if there is any way to do it.
Thanks!
Taimur

You might not be able to do it in IB, but you can certainly do it with a pair of actions. I'm assuming that the highlighted state will only be active while the button is pressed. You can modify this code if that's not the case (to toggle the state back to normal when the highlighted state ends).
- (IBAction)buttonDown:(id)sender {
UIButton *button = (UIButton *)sender;
button.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
}
- (IBAction)buttonUp:(id)sender {
UIButton *button = (UIButton *)sender;
button.titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
}

btn.titleLabel.adjustsFontSizeToFitWidth = TRUE;
so It adjust text size as per button width automatically......

This actually isn't too hard to do. You need to create two IBActions in your class, onTouchDown and onTouchUp. Create an IBOutlet for your button too. Then, go to Interface Builder and wire up the button to the IBOutlet and wire up the action for "Touch Down" to "onTouchDown" and the actions for "Touch Up Inside" and "Touch Up Outside" to "onTouchUp."
onTouchDown is where you'll set your highlighted font. onTouchUp is where you'll reset your button.
Your header file will end up looking something like this:
#interface TestViewController : UIViewController {
UIButton *testButton;
}
#property (nonatomic, retain) IBOutlet UIButton *testButton;
- (IBAction)onTouchDown;
- (IBAction)onTouchUp;
Now, inside the "onTouchDown" function here's what you're gonna do:
1) Store the current dimensions of the button
2) Change the font size
3) Tell the button to auto-resize (THIS IS THE KEY)
4) Center the button based on the old size
You should have a function that ends up looking something like this:
- (IBAction)onTouchDown:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:36];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
Note, the font size is 36.
In your touch up function, it'll look something like this:
- (IBAction)onTouchUp:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:15];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
The font size should be whatever your default font is. In this case, I made it 15.
This should get you a button that resizes from the center instead of just resizing.
Now, the code isn't intended to be perfect. It's just a general IDEA of what you're doing here. I think this is a great candidate for some code reuse by combining these into one function and passing in what size you want the text to be. I just didn't feel like doing it myself ;)

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!

how to place menu button in iphone

i need to place menu button in iphone.
i have some touch with android.
In android i display like this by pressing menu.
Is it possible to handle iphone menu button.
if it is not where can i place these.
Thank u in advance.
use the round rect button and change the property into custom place the image as the background image of the button do this for more menu item in your home page.
this is the way i do the menu item.......
I think with Xcode and Interface Builder (iPhone's development platform), getting this task done would be even simpler.
As a brief introduction to iPhone app's UI design, to place this tab bar (as you put it, the menu) on a view, all you need to do is pick up a tab bar from the library and place it on a nib file's view. You can also connect such a tab bar with some codes responsible for internal logic by simply clicking and dragging. It's simple.
btw, a nib file is just some file format Xcode and Interface Builder use to hold the UI data.
iPhones don't have a dedicated slide out menu - everything is on the screen at all times, and there are no purpose built buttons (like back and home).
You should either always show your actions on the screen, or if they are unimportant or infrequent, hide them behind another action button (like a + or wrench icon).
You should use images same as menu items and put custom button over the images.This is the only way to show menu as you want.
Iphone sdk provides tabBar and toolBar, you also can use these in customize forms.This is the correct way to making menu.Iphone design pattern having lot of strengths over android so you can make every thing in Iphone easily but you cant make every thing in android same as iPhone.Iphone having international standards so must use iPhone's controls.
.h
IBOutlet UIScrollView *scrollView;
#property ( nonatomic , retain ) IBOutlet UIScrollView *scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender;
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons;
.m
#synthesize scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender{
NSLog(#"AppleVijayAtFacebookDotCom called");
UIButton *button=(UIButton *)sender;
if (button.tag == 0) {
NSLog(#"hey have clicked first button, this is my tag : %i \n\n",button.tag);
}
else if (button.tag == 1) {
NSLog(#"hey have clicked second button, this is my tag : %i \n\n",button.tag);
}
// ......like this
NSLog(#"button clicked is : %iBut \n\n",button.tag);
}
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(AppleVijayAtFacebookDotCom:) forControlEvents:UIControlEventTouchUpInside];
//[button1 setImage:[UIImage imageNamed:#"Button.png"] forState:UIControlStateNormal];//with image
//OR
[button setTitle:[NSString stringWithFormat:#"%iBut",i] forState:UIControlStateNormal];//with title
button.frame = CGRectMake(i*(offset+buttonSize.width), 8.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 10;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.backgroundColor=[UIColor blackColor].CGColor;
button.layer.borderWidth=2.0f;
button.tag=i;
[self.scrollView addSubview:button];
}
self.scrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
//self.navigationItem.titleView=self.scrollView;//if u have navigationcontroller then enable this line
}
Dont forget to connect the scrollView in interface builder
while creating the scrollview in IB make sure ur scrollView height is 44.which is default to navigation bar.so it will look nice.
in viewDidLoad call
[self createMenuWithButtonSize:CGSizeMake(70.0, 30.0) withOffset:20.0f noOfButtons:30];
OUTPUT

iphone problem making UILabel

I'm having kind of a dumb problem.
I am using the below code to create/modify a UILabel via code. The reason I am creating it via code is because I need it to be rotated 90 degrees and Im not aware of a way to do that in IB.
What's happening - A user hits a button that makes the text they selected to appear in the UILabel. Then when they select the button again, with different text, the new text appears in place of the old text.
The first time I hit the button, it works perfectly, but the second time I hit the button, the new label appears over the old label and the old label never disappears. I have tried removing the first label, making it nil, just removing the text, but I cannot access any part of the label once it has been created.
ViewController.h
...
UIView *viewForLabels;
UILabel *tab1Label;
}
#property (nonatomic, retain) IBOutlet UIView *viewForLabels;
#property (nonatomic, retain) IBOutlet UILabel *tab1Label
...
#end
ViewController.m
...
#synthesize tab1Label;
...
UILabel *tab1Label = [[UILabel alloc]init];
tab1Label.text = [theText];
tab1Label.backgroundColor = [UIColor clearColor];
tab1Label.textColor = [UIColor blackColor];
tab1Label.opaque = NO;
tab1Label.font = [UIFont systemFontOfSize:14];
tab1Label.numberOfLines = 2;
tab1Label.adjustsFontSizeToFitWidth=YES;
tab1Label.transform = CGAffineTransformMakeRotation (90*3.1459565) / 180);
tab1Label.frame = CGRectMake(2,87,45,119);
[viewForLabels: addSubview: tab1Label];
...
First in your code example you alloc tabLabel1 and then run a bunch of property updates against a different named object tab1Label.
Sorry if I am misunderstanding the question but why are you creating a second label? Per this part of your description:
What's happening - A user hits a
button that makes the text they
selected to appear in the UILabel.
Then when they select the button
again, with different text, the new
text appears in place of the old text.
Just update the .text property and any sizing needed why use a whole separate object?

UIButton only responds in a small area

I'm trying to add a UIButton to a UIView, but am having some trouble with getting it to respond to touches.
I have a method which returns UIButtons after I provide it with a tag:
- (UIButton*)niceSizeButtonWithTag:(int)tag {
UIButton * aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:tag];
[aButton addTarget:self action:#selector(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
CGRect newFrame = aButton.frame;
newFrame.size.width = 44;
newFrame.size.height = 44;
[aButton setFrame:newFrame];
return aButton;
}
As you can see I'm creating a new button and increasing the size.
I use this in the following way:
UIButton * anotherButton = [self niceSizeButtonWithTag:1];
[anotherButton setImage:[UIImage imageNamed:#"image" withExtension:#"png"] forState:UIControlStateNormal];
[anotherButton setCenter:CGPointMake(middleOfView)];
[aView addSubview:anotherButton];
I create a lot of buttons like this, hence the reason for the method.
The buttons are always created and added to the subview perfectly. I can see the image and they're in the correct position. The problem is that they only respond to touches in a tiny strip.
In this attached image,
alt text http://web1.twitpic.com/img/107755085-5bffdcb66beb6674d01bb951094b0e55.4c017948-full.png
The yellow shows the whole frame of the button,
The red shows the area that will respond to touches.
The grey shows a section of the view the button is added to.
If anyone could shed some light on why this is happening, it would be really useful.
UPDATE:
I should probably mention I'm trying to add this button to a UIView which is a subview of a UITableViewCell.
UPDATE 2:
Adding it directly to the UITableViewCell works, adding it to the UIView on the UITableViewCell is causing the error.
FINAL UPDATE
The problem turned out to be that I was sending the view containing the button to the back of the subviews on the UITableViewCell. Getting rid of the sendSubviewToBack: call fixed it.
Do you have any other views added to this containing view after you add the button? try setting some of your view's background color to blueColor, redColor and other colors to better see what the stack of views in your app is like. Then you should be easily able to see if there is some sort of view blocking the button.

iPhone app: SDK 3.0 : Button Selected State: Can't get working!

Trying to do simple button functionality.
Button A. It has 3 states: default, highlighted, selected. Should UNSELECT when clicked again.
For the life of me, I can't even get a simple 3 state functionality established.
Highlight (on press) appears built in (goes to "stock" blue).
I"ve used Button Attributes to load in image for selected state...PLayed with Control/Content to click Highlight and Selected on and off, trying to find the right combo...
I thought it'd simply be selecting in dropdown the state I want to edit....and it would register my edits for that state...images loaded/ colors changed/ etc....
NO!!!!
What am I missing..?
What are you trying to do set the images or titles for the different states? For that you can just use methods such as
-(void)setImage:(UIImage *)image forState:(UIControlState)state;
OR
-(void)setTitle:(NSString *)title forState:(UIControlState)state;
is that what you are asking. May be i am misunderstanding your question because its not clear enough.
I couldn't find a straight answer about this anywhere. So I brewed my own solution that seems to work great.
Dont forget to wire up your buttons in IB you will need
to bind both
touch up inside to setButtonPressed
file owner to button ex. 1stButton
Psuedo Code
Clear all buttons selected
Set sender to selected
CODE
//Declare your buttons in .h
UIButton *1stButton;
UIButton *2ndButton;
UIButton *3rdButton;
#property(nonatomic, retain) IBOutlet UIButton *1stButton;
#property(nonatomic, retain) IBOutlet UIButton *2ndButton;
#property(nonatomic, retain) IBOutlet UIButton *3rdButton;
//In .m file - write 2 methods
-(void)clearButtons
{
[1stButton setSelected:FALSE];
[2ndButton setSelected:FALSE];
[3rdButton setSelected:FALSE];
}
//attach to touch up inside event in IB for each button
-(void) setButtonPressed:(UIButton *)sender
{
self.clearButtons;
[sender setSelected:TRUE];
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
}
You really should post some code (or an IB screenshot) as it's hard to fully parse what you are saying.
The default behavior of a UIButton is to show the Selected state on press, then revert back to the normal state. If you want it to "lock down" a press, you need to do something like this in the IBAction hooked to the UIButton:
BOOL pressed;
UIButton *button
- (IBAction) toggleButton:(id)sender
{
button.selected = ! pressed;
pressed = ! pressed;
}
AH! Well, not being a programmer, I was hoping for simple "stock" solution within Interface Builder.
To be clear of the "want/wish:
Button A-E. Each button should have 3 states: Default, highlight, selected.
Default: resting state.
Highlight: some treatment using interface builder features or import a graphic that will appear on Highlight. This state appears while button is being pressed.
Selected: when button A-E is released, it shows that it has been selected by displaying a new state, again by using tweaks within interface builder (font color, shadow) or import a graphic. When you click the SELECTED button again, it returns to Default state.
I guess I just thought it'd be "easy" because SDK 3.0 Interface Builder has dropdowns for the 3 states. I thought code would "magically" be assigned to the button allowing it to function as desired along with it's aesthetic shift.
I TOTALLY appreciate any and all feedback. I can pass along any coding advice to partner on this App.
Try this:
UIImage *img1 = [UIImage imageNamed:#"image1.png"];
UIImage *img2 = [UIImage imageNamed:#"image2.png"];
UIImage *img3 = [UIImage imageNamed:#"image3.png"];
[button setImage:img1 forState:UIControlStateNormal];
[button setImage:img2 forState:UIControlStateHighlighted];
[button setImage:img3 forState:UIControlStateSelected];
[button setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];
[img1 release];
[img2 release];
[img3 release];
This is because the state variable is actually a bit flag.