can't get word wrap to work on UIButton - cocoa-touch

I have a simple UIButton and try to get word wrap but it always shows the text in one line exceeding the button size.
NSString * text = NSLocalizedString(#"Start Loading",#"Start Loading");
_continueBtn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
_continueBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
[_continueBtn setTitle:text forState:UIControlStateNormal];
Also in UI builder when I set word wrap there the text wraps - only when I run the app the text appears in one line.
What have I missed here?

Try:
_continueBtn.titleLabel.numberOfLines = 0;
I've tried next code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 70, 50);
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle:#"start loading"
forState:UIControlStateNormal];
[self.view addSubview:button];
It's working good for me:

Related

How to make a custom UIBarButton for Left Bar Button for BackBarButton

I am trying to customize UI of an application for iOS 7. I want exactly BackBarButton of iOS 7 but with different color for both arrow and title and different font for title. This is iOS 7 back button.
I tried to customize it with following code
UIImage *backButtonImage = [UIImage imageNamed:#"back.png"];
UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customBackButton addTarget:self action:#selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[customBackButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[customBackButton setFrame:CGRectMake(0, 0, 30, 30)];
backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
[backButton setAction:#selector(backButtonPressed)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
It seemed like this:
There is two problem with above Image.
First it is not aligned to left(iOS back button sticked to the left).
Second it doesn't have title of previous page.
I just added title attribute to custom button.
[customBackButton setTitle:#"title" forState:UIControlStateNormal];
But it was like this:
How can I fix the problems(stick to left and title)?
For this, you need to have a custom image with arrow & title.
if you want to customise the title, you can just use below code>
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"My Title" style:UIBarButtonItemStylePlain target:nil action:nil];
But if you want it to set for an image, you need an image with having the back button to the left of the image.
Image:
2x Image:
See the images in editor, you can notice that image size is 70x32 and the arrow is more towards left end. You can have image with text also & use it.
Hope that helps.
I just found out how It is possible
I wrote following code and it perfectly worked!
UIBarButtonItem * backButton = [UIBarButtonItem new];
UIView * backButtonView = [UIView new];
[backButtonView setFrame:CGRectMake(0, 0, 90, 32)];
UIImageView *backButtonImage = [UIImageView new];
[backButtonImage setImage:[UIImage imageNamed:#"Setting/back.png"]];
[backButtonImage setFrame:CGRectMake(-15, 0, 30, 30)];
UILabel * backButtonLabel = [UILabel new];
[backButtonLabel setFrame:CGRectMake(8, 0, backButtonView.frame.size.width, 30)];
[backButtonLabel setBackgroundColor:[UIColor clearColor]];
[backButtonLabel setTextColor:[UIColor whiteColor]];
[backButtonLabel setTextAlignment:NSTextAlignmentLeft];
[backButtonLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
[backButtonLabel setText:title];
UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customBackButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[customBackButton setFrame:CGRectMake(0, 0, 90, 30)];
[backButtonView addSubview:customBackButton];
[backButtonView addSubview:backButtonImage];
[backButtonView addSubview:backButtonLabel];
backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
[backButton setAction:action];
[self.navigationItem setLeftBarButtonItem:backButton animated:YES];

UIButton title doesn't show up

I'm having a little problem with a UIButton I try to create programmatically. The problem is that the button shows up (i.e. if I set a background color, I can clearly see the button) and is clickable, but the title label isn't visible.
Here's what I'm doing :
valueButton = [UIButton buttonWithType:UIButtonTypeCustom];
[valueButton addTarget:self action:#selector(openList:) forControlEvents:UIControlEventTouchUpInside];
valueButton.frame = CGRectMake(160.0, decalageLabel+6.0, 120.0, 20.0);
[valueButton.titleLabel setTextAlignment:UITextAlignmentRight];
[valueButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Neue-Light" size:17.0]];
UIColor * colorLabelValue = [[UIColor alloc] initWithRed:131.0/255.0 green:159.0/255.0 blue:86.0/255.0 alpha:1.0] ;
[valueButton setTitleColor:colorLabelValue forState:UIControlStateNormal];
if(txtValueField == NULL){
txtValueField = #"";
}
valueButton.titleLabel.text = [NSString stringWithFormat:#"%#", txtValueField];
NSLog(#"button : %# ", valueButton.titleLabel.text);
[self.contentView addSubview:valueButton];
Oh and I've made sure the title had an actual value, by NSLogging its text value, which works fine. So I don't know what's happening. Any idea?
Don't use [[button titleLabel] setTitle:] for this. Use [button setTitle:#"blah" forControlState:UIControlStateNormal].
This allows you to set a different title for highlighted, selected, up, etc.

Multiple Lines in UIButton

Hi I have problem for set multiple lines to my Button which is declared like that:
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.numberOfLines = 0;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
[button addTarget:self
action:#selector(myButtonClick)
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(0.0, 100.0, 317.0, 100.0);
[button setTitle:string forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.text = #"ahoj";
NSMutableString *ObratString = [[NSMutableString alloc] initWithString:button.titleLabel.text];
[ObratString appendString:#"\n"];
[ObratString appendString:#"caw"];
[ObratString appendString:#"\n"];
[ObratString appendString:#"helllo"];
button.titleLabel.text = ObratString;
[ObratString release];
[self.view addSubview:button];
But in the end I just see the first line.
Is there any way to make it work?
The UIButton displays it's text with a contained UILabel. The default for the contained label is to display one line of text only. This label is accessible through the titleLabel property, and anything you can do to a normal label can be done to it.
For example making it multi-lines broken by words:
ObjC:
myButton.titleLabel.numberOfLines = 0; // Dynamic number of lines
myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
Swift:
myButton.titleLabel?.numberOfLines = 0; // Dynamic number of lines
myButton.titleLabel?.lineBreakMode = .byWordWrapping;
Swift version for the checked response:
myButton.titleLabel?.numberOfLines = 0
myButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

Help with iphone button pressed

I am trying to see which button was clicked on so I can preform the correct logic.
This is the code for the buttons:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 423, 60, 60)];
[button addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[[UIImage imageNamed:#"refreshicon.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button.tag = 1;
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 423, 60, 60)];
[button2 addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[[UIImage imageNamed:#"login.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button2.tag = 2;
[self.navigationController.view addSubview:button];
[self.navigationController.view addSubview:button2];
And this is how I call the buttonPressedAction:
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
if(button.tag == 1)
{
NSLog(#"1");
}else
{
NSLog(#"2");
}
}
But when I use NSLog to see what the sender value is, it crashes.
Any advice for what's happening and how to correct it?
Now corrected :o) THANKS!
As other have pointed out, UIButton does not have a value property. I guess you are trying to detect which button was clicked. Here are two ways to do this:
Use the tag property one each button. I.e. button1.tag = 1, button2.tag = 2. You can then test which button was clicked with if(sender.tag == 1) etc. You could introduce constants for the numbers to make the code more readable.
If you keep a reference to the button, you can check if the reference is equal. Example: if(sender == self.button1)
Probably the best approach would be to give buttons that do different things different actions, but failing that, you could distinguish them by tag.
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
// do something
}
If you were saving the pointers to the button objects you are creating, you could compare the pointers. Or you can just set the tag property for the button, and use that to switch behaviour.

iPhone/iPad UIButton TitleLabel text not appearing

I created a grid of buttons. The following code creates the buttons and displays them, but there is no text on the button. Is there a setting I am missing? (Obj-C replies are fine, I'm bi-lingual)
RectangleF frame = new RectangleF (X + 3, Y + 3, cellWidth - 2, cellHeight - 2);
UIButton button = new UIButton (frame);
button.TitleLabel.Text = "Baha'i";
button.TitleLabel.Font = UIFont.FromName ("Helvetica-Bold", 15);
button.TitleLabel.TextColor = UIColor.Black;
button.TitleLabel.Frame = frame;
button.BackgroundColor = UIColor.FromWhiteAlpha(.5f,.5f);
this.AddSubview (button);
I think you want setTitle: forState:
[button setTitle:#"Baha'i" forState:UIControlStateNormal]
A UIButton inherits from UIView, so another way to add text and images, is to add subview. Example:
// My Image
UIImage *buttonImage = [UIImage imageNamed:#"myImage.png"];
UIImageView *buttonImageView = [[[UIImageView alloc]
initWithFrame:CGRectMake(0, 0,
buttonImage.size.width,buttonImage.size.height)] autorelease];
[buttonImageView setImage:buttonImage];
// My Label
UILabel* titleLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(0, 0,
buttonImage.size.width, buttonImage.size.height)] autorelease];
titleLabel.text = #"My Text";
titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 11.0];
titleLabel.textColor = [UIColor blackColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
// Create Button with Image and Label
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addSubview:buttonImageView];
[button addSubview:titleLabel];
UIButton * selectCountryBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[selectCountryBtn setTitle:#"My Title" forState:UIControlStateNormal];
Here, I have create sample programmatically button, and then set the title for its Normal Control state, You can use your own button object for set the title string, additionally you can also set title text for another control states by change UIControlStateNormal to another required states.
In my case (Swift 4) it was the following:
btnLogin.setTitle("Welcome, " + var_name, for: [])
For swift Developers-
button.setTitle("Your button Name", for: .normal)
self.ButtonName.setTitle("Here Put Your variable model", for: .normal)
self.Button_name.titleLabel.text = #"Your title name";