Button title text not showing fully - iphone

I am using Xcode 4.2 iOS 5.0 and also using storyboard. I have simple button and I connected that button with outlet.Now I am changing the button text from code.Problem is that if my string of button title is that "hey how are you?" then it is showing in button title "hey....u?" .Can anyone suggest me where I am doing wrong or missing something else?
![enter image description here][1]This is IB Settings.I just wrote btw.textLabel.text=textstr; and frame is (0,0,290,42) and text str is time as for e.g [NSDate date]

Your button width is smaller than the title string's length.that's why
it truncates the middle part of the string. You can set the content
mode to characterWrap in IB to make the title in two lines or you can
change the font size to a smaller one to fit in your button width or
you can change the button width to occupy the whole string. Prefer
what is fine for you.

Try [myButton sizeToFit]; //I need to type 9 more characters to answer this question due to 30 char minimum.

I'm not sure if this already worked in XCode 4.2, but now it does ;)
You need to use the UIButton method setTitle:forState:
[self.myButton setTitle:#"Correct New Title" forState:UIControlStateNormal];
as this will correctly update the size and position of the buttons label. Where as just setting the title with self.myButton.titleLabel.text = #"Wrong New Title"; will not.
Credits to James Beith (see his Answer in UIButton - text truncated)

myButton.textLabel.text = #"some very long title";
[myButton.textLabel sizeToFit];

FOR SWIFT 4,5
You need to use "setTitle" instead of "textLabel.text"
Button.setTitle("this is maximium line text come", for: .normal)

Related

When I create a table in iOS7, the label in the cell appears to be much smaller than the cell

I have an iPhone app which i'm currently testing with iOS7. I've noticed a few changes such as iOS7 not taking into account nav bars and tab bars when giving the screen size and I've accounted for that. However, one thing I haven't been able to account for is that when I create a table view, the text label appears to be. I've tried running it in iOS6.1 and it runs fine there.
To set the text, I do this:
label_p.text = sText_p;
label_p.font = [UIFont fontWithName: [font_p returnFontName] size: [font_p getFontSizeValue]];
label_p.numberOfLines = 0;
label_p.lineBreakMode = UILineBreakModeWordWrap;
I'm using the smallest font I can but it's still not helping the issue. When I run the app with the numberOfLines variable set to 1, it shows the text correctly but then the UILineBreakModeWordWrap setting for lineBreakMode stops working and text runs off the end of the label without wrapping. Is this a problem that anyone else has come across when migrating to iOS7?
I managed to figure it out. For some reason in iOS 7, you need to call [label sizeToFit] but not in previous versions of iOS.

Get Width of last line of multiline UILabel

I have a dynamic multi line UILabel and need to know the end of the text (X Coordinate) of the visible text (not the Label) so I can show something after the text.
Is this possible?
thank you
You'll be able to have more control over the text layout with the CoreText framework. Checkout the documentation:
There are also some nice open source things that already do a lot of the hard work for you, like: https://github.com/Cocoanetics/DTCoreText
As far as I know , the best solution for this is to use a UIWebView instead of a UILabel.
You just have to format the HTML for it to load and then add whatever you want to add after.
Example:
[webView loadHTMLString:[NSString stringWithFormat:#"<html><body><font face=\"arial\" size=\"2\">%#</font><font face=\"arial\" size=\"1\"> %#</font></body></html>",text1 , text2] baseURL:nil];
If you want to keep trying with UILabel / UITextView /any UIView for that matter , I only know of a way to figure out the height properly : [myView sizeToFit]; And then get it's frame.
Hope this helps.
Regards,
George

How to set UIButton title to null or an empty string

TL;DR - My question really is: How to I set a UIButton's title to an empty string, or null after it has already been assigned a previous title.
I have a UIButton that accepts a string generated based on the text from an NSString variable as it's title using the following:
NSString *MyStringVariable = #"HELLO";
...
[myButton setTitle:MyStringVariable forState:UIControlStateNormal];
For the sake of this question, let's just assume I have "HELLO" set on my NSString variable. This then makes my UIButton have the title "HELLO", obviously...
I then have another button which removes this title from the UIbutton, like so:
[myButton setTitle:#"" forState:UIControlStateNormal];
Which does hide the UIButton's title (i.e. it now just looks like an empty UIButton), but upon further inspection, the title does infact still exist. If I add the following to my UIViewController:
- (void)viewWillAppear:(BOOL)animated{
NSLog(#"Title: %#", myButton.titleLabel.text);
}
and navigate to and fro from this view, the title continues to get logged in the console, even though I've set the text to #"".
I have tried the following methods to make the title truly go away:
[myButton setTitle:#"" forState:UIControlStateNormal];
[myButton setTitle:NULL forState:UIControlStateNormal];
[myButton setTitle:nil forState:UIControlStateNormal];
myButton.titleLabel.text = #"";
The weird thing is, if I set the title to #" " (with a space):
[myButton setTitle:#" " forState:UIControlStateNormal];
the title does update correctly, and the space is then logged to the console, rather than the old title.
My question really is: How to I set a UIButton's title to an empty string, or null after it has already been assigned a previous title.
I too faced the same issue. I was setting the title of the button to #"" for all the states, and at a later point when I check the titlelabel.text it gives me the previous title which is not #"". So I printed out all the possible values of a button as suggested by PengOne and found that the properties titleForState and currentTitle give the updated/correct values.
So instead of checking the titleLabel.text, try checking the currentTitle or titleForState.
I still haven't figured out why the titleLabel is not updated properly.
I believe this has to do with how the UIButton updates its titleLabel property when setTitle:forState: is called.
For example, if you try to set the title of a button by directly accessing the titleLabel.text property, it does not work (check out the myriad questions on SO about this).
If you truly want to understand what's going on, then you should look at both the titleLabel.text and the current state of the button. One way to do this is to log titleLabel.text, currentTitle, and titleForState: in viewWillAppear, viewDidAppear, viewWillDisappear, and viewDidDisappear. The first two (titleLabel.text, currentTitle,) should coincide, and the third should be what you expect (titleForState:).
This sounds like a bug in the SDK, create a bugreport.
If you really need a button without a title, you'll probably have to create one yourself. A UIView with it's layer set to have a cornerRadius (and hence be rounded), which responds to user interaction should do it. Also, check the three20 toolkit, they may have a custom button or two in there you could use to save some time.
I have seen the same issue but in a different context. I have been using NSLocalizableString to change the text in a button.
It appears that setting the UIButton.title changes its UIButton.currentTitle but not its UIButton.titleLabel.text.
Though it may not make much of a difference for your application, the fact that titleLabel.txt and currentTitle are different turns to be more of a "be aware" than an implementation challenge.

UISwitch - change from on/off to yes/no

does anyone know of a way I can change the text label for on and off to yes and no.
I did it with
((UILabel *)[[[[[[switchControl subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = #"Yes";
((UILabel *)[[[[[[switchControl subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = #"No";
However, with the release of iOS 4.2, this is no longer supported (this probably wasn't recommended by Apple anyway)
My client is insisting on yes/no switches. I'd appreciate any advice!
many thanks
Hurrah! From iOS 6, it's possible to specify an image to be used for the on / off states, respectively. So, this can be used to display a YES / NO image (or whatever image representing the text you would prefer to use instead of the previously limited ON / OFF).
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
{
[mySwitch setOnImage: [UIImage imageNamed:#"UISwitch-Yes"]];
[mySwitch setOffImage:[UIImage imageNamed:#"UISwitch-No"]];
}
The images should be 77 px wide, 27 px high, and the text (one image for each state) should be horizontally centred within that 77 px width. I use transparent backgrounds for the text, so I can still make use of the tint for the background, which still works with this.
Of course, it would seem easier to just supply text, rather than having to use an image of text, but I'm certainly grateful for this new option, at least.
You need to implement your custom UISwitch for that. Or use one of already implemented :) (check this SO question and this post)
Vladimir answer is great, but in my humble opinion there is an even better implementation here: https://github.com/domesticcatsoftware/DCRoundSwitch.
Besides setting a custom text, it is easier to change the size and color of the UISwitch and you get a sharper result.
It is released under an MIT license. Have a look!
It turns out that you can create a custom UISwitch with the following items:
A UIScrollView
A UIButton
Two UILabels
A background image
A Boolean value
First you will need to add QuartzCore.framework to your project and #import <QuartzCore/QuartzCore.h> to your view controller.
Next add the UIScrollView to your view using Interface Builder. The ScrollView will be your custom UISwitch.
Next add the button and the two labels to your ScrollView. One label will be for "yes" the other for "no".
Add the image to the button and set its type to custom. This is the image I use:
Position the labels over the blue and white area of the image. Adjust the ScrollView so it is just big enough to show the blue part of the image and the thumb nob.
Add the following line to viewDidLoad:
self.mySwitch.layer.cornerRadius = 13.5;
mySwitch is the name of the ScrollView and 13.5 is half the height of the ScrollView. The above statement changes the ScrollView to have rounded ends like the UISwitch.
To make the custom switch active you will need to tie the buttons "Touch Up Inside" event to an IBAction. Here is the code I use in the event handler:
-(IBAction)mySwitchButton:(id)sender {
self.myValue = !self.myValue;
CGPoint scrollPoint = CGPointMake((self.myValue)? 43.0: 0, 0.0);
[mySwitch setContentOffset:scrollPoint animated:YES];
}
Where myValue is the boolean variable that contains the state of your switch and 43.0 is the number of points you will have to move the image over to put the switch in the off position.
That is all there is to it!
From iOS 6, it's possible to specify an image to be used for the UISwitch on / off states, but NOT the text.
This will lead trouble when internationalization is required because translators
have to provide an image text for each language, not text only.
Moreover, the size of the UISwitch image is fixed, limiting the text length.
Because of the above reasons, I like the JSWilson's answer: simple and flexible.
To relieve developers of the need to manually add the required controls, I coded a custom CRDScrollSwitch class that you can find at my GitHub repository:
https://github.com/corerd/CRDScrollSwitch

UIToolbar tint on iOS 4

just switched to iOS 4 on my iPhone 3GS and some of my apps broke.
One issue I had is that I had a UIToolbar with some buttons, tinted to pink, that worked well on the 3.1.3 OS. After upgrading to iOS 4, the toolbar was still tinted, but the buttons it contained were no longer affected by the tint. The toolbar was pink while the buttons were regular-blue.
Looked around for it on the net, but found no reference of such a thing.
Anyone knows what broke in the process?
(must be frank here - I knew the answer before posting, just didn't know how to load this data to StackOverflow. Thought the solution I found was valuable for others, so wanted to post it here. I'm new here, so please no harsh critics :) )
So eventually the problem resulted from, AFAICT, a change in behavior in the OS.
As stated the tint code worked before the upgrade and was written like this:
// Toolbar content
NSArray *items=[NSArray arrayWithObjects: ... ]; // PSEUDO CODE HERE
[toolbar setItems:items];
// Add tint
toolbar.tintColor = [UIColor colorWithRed:0.83 green:0.43 blue:0.57 alpha:0.5];
What I needed to do, was just reverse the order of things:
// Add tint
toolbar.tintColor = [UIColor colorWithRed:0.83 green:0.43 blue:0.57 alpha:0.5];
// Toolbar content
NSArray *items=[NSArray arrayWithObjects: ... ]; // PSEUDO CODE HERE
[toolbar setItems:items];
(If you created UIToolbar in Interface Builder, you can change it's tint there, and that applies for the buttons as well).
I guess the tint updated all buttons before iOS 4, while in iOS 4 it doesn't and when adding buttons, they check for existing tint. But this is just a guess. The solution works anyhow..
Hope this helps someone, and that I didn't violate any sacred SO rules...
Cheers!
Well, it seems more like an OS bug than a feature, since navigation bars do change their item's color when you set their tintColor.
We've found that if you change the item's style, it refreshes their color as a side effect. Doing the following worked in our case. The original buttons are bordered, so we change them to plain and set them to bordered again. You may do a more complicated and generic code that saves the current style, sets another one and then switchs back. I am just to lazy to do that. :D Anyway, you get the idea.
toolbar.tintColor = //<some dynamically obtained UIColor>
// Workaround to properly set the UIBarButtonItem's tint color in iOS 4
for (UIBarButtonItem * item in toolbar.items)
{
item.style = UIBarButtonItemStylePlain;
item.style = UIBarButtonItemStyleBordered;
}
Regards,
Rula.