How can create a UIButton with Hebrew language text? - ios

Problem:
Text (Hebrew language) which I set in button shows conversely.
For example:
NSString *title = #"עמוד הבית"; //This is right-to-left
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
so:
In my simulator i see:
//This is left-to-right
How can it fix?
How can I show text correctly?
App supported iOS 6 and iOS 7

What worked for me when using an UIButton was this:
if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:button.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft)
{
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
}
Links worth reading:
Supporting Right-to-Left Languages (Apple Reference)
AutoLayout + RTL + UILabel text alignment

if you use "#x200F" that will solve your problem,add this as appended string.There is one issue is after adding it all come with and that's weird,if you know the answer.Please, share

Related

iOS 9 View Not appearing

I have noticed a that a uibutton is not showing in my app when run in iOS 9 (works fine in earlier OS versions). I know several have noticed a similar issue with storyboards (https://forums.developer.apple.com/thread/14003), though I am not using those and am instead building my view programmatically. There is no autolayout or autoresizingmask to worry about.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(SUG_MARGIN, self.mainView.bounds.size.height - 40 - margin, self.mainView.bounds.size.width-margin*2, 40);
[button setTitle:NSLocalizedString(#"...", nil) forState:UIControlStateNormal];
button.titleLabel.font = //...;
button.backgroundColor = //...;
[self.mainView addSubview:button];
NSLog(#"main:%#\nadd:%#\nsuper:%#", NSStringFromCGRect(self.mainView.frame), NSStringFromCGRect(button.frame), button.superview);
That nslog shows everything set up correctly.
I have similar buttons all over the app, but this is the only one that is not appearing, which is why I am scratching my head.
Refactored code to assign only measure frames in viewWillLayoutSubviews and it now works. strange.

Unwanted spaces between characters on UIButton title text in iOS6

Code for UIButton
btnChooseHour.layer.cornerRadius = 4.0;
btnChooseHour.clipsToBounds = YES;
btnChooseHour.layer.borderColor = [CustomUIControls getConstantCustomColor:COLOR_67].CGColor; //Custom Color
btnChooseHour.layer.borderWidth = 0.7;
[btnChooseHour.titleLabel setFont:FONT_AWESOME_(13)]; //Custom Font
[btnChooseHour.titleLabel setMinimumScaleFactor:0.8];
[btnChooseHour setTitleColor:[CustomUIControls getConstantCustomColor:COLOR_153] forState:UIControlStateNormal]; //Custom Color
[btnChooseHour setTitle:[NSString stringWithFormat:#"%#\t%#", strDisplayDate, [NSString fontAwesomeIconStringForEnum:FAIconCaretDown]] forState:UIControlStateNormal];//Setting Title
btnChooseHour.titleLabel.adjustsFontSizeToFitWidth = true;
In iOS 7 and newer version, it is displaying perfectly. But while using in iOS 6, Button's title has some unwanted spacing between every characters.
How to remove spacings between title text of UIButton specially in iOS 6
Thanks in advance.

Localizing buttons in Xcode

This is my first question so far so I would love some understanding!
I am localizing entire application. I have no problem with labels that while pseudolocation translate properly. However, trying to achieve the same with buttons it doesn't work.
The code is
[self.Clear setTitle:NSLocalizedString(#"Clear",nil) forState:UIControlStateNormal];
and the .string file in en.plist content looks like this
"Clear" = "Blah";
The pseudolocation doesn't work and I would really appreciate some help. The simulator keeps showing Clear.
If you have any questions regarding this, I will answer!
Please don't eat me :P
In Interface Builder, you can set 4 strings, one for each of the states in the "State Config" dropdown.
OR, alternatively, in code, you set the button title for each state:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:NSLocalizedString(#"21.title", #"Norm!") forState:UIControlStateNormal];
[button setTitle:NSLocalizedString(#"21.title-highlighted", #"hi btn") forState:UIControlStateHighlighted];
[button setTitle:NSLocalizedString(#"21.title-selected", #"sel btn") forState:UIControlStateSelected];
[button setTitle:NSLocalizedString(#"21.title-disabled", #"dis btn") forState:UIControlStateDisabled];
Courtsey : Localize IOS button label

AutoResizing a button based on title text content

So I have a strange issue which could be a bug or me being stupid.
I have a button in a cell. The cell shows the logged in users facebook friends. If they are already a user of the app it shows the button as Play, else it shows Invite Me...
UIButton *playButton = (UIButton *)[cell.contentView viewWithTag:30];
playButton.backgroundColor = [UIColor clearColor];
[playButton setBackgroundImage:[[UIImage imageNamed:#"fbCell_Invite_30h"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 5, 15, 5)] forState:UIControlStateNormal];
playButton.titleLabel.text = #"Invite me";
if ([self.fbFriendsUsingApp containsObject:[friend valueForKey:#"id"]]) {
playButton.titleLabel.text = #"Play";
}
This all works fine and not an issue. However, when the button text is invite me it shows as this:
In storyboard interface builder the button is setup like this:
What is strange is that if I click the middle arrow <------> to make the width stretch, the title text stops working and they all show 'Play' ???
Try setting the text of the button using
[playButton setTitle:#"Invite Me" forState:UIControlStateNormal];
instead of
playButton.titleLabel.text = #"Invite Me;
I've found that if I don't use the first approach above the changes to the button text don't show up.
You can find the size of the NSString that you are setting as the button label and then resize the button accordingly:
CGSize labelSize = [labelString sizeWithFont:[UIFont systemFontOfSize:12]];
[button setFrame:CGRectMake(10,0,labelSize.width, labelSize.height)];
You will probably want to add a little to the CGSize as padding, but play with it and find the result you desire.

How to Change textLabel property in UIButton programmatically in iOS?

I have a UIButton that should have 2 statuses - "Play" and "Pause"; i.e. - at the first time the user sees it it says "Play" and then whenever the user clicks it it should switch between "Play" and "Pause".
I managed to create the controller itself - the content is played and paused properly - but I cannot seem to change the UIButton Text Label text.
I use:
myButton.titleLabel.text = #"Play";
myButton.titleLabel.text = #"Pause";
It's not working. The text is not changing. I also tried [myButton.titleLabel setText:#"Pause"] and it's not working as well.
How can I set it?
It should be:
[myButton setTitle:#"Play" forState:UIControlStateNormal];
You need to pass the state as well. You can check other state's here.
You can then do something like this:
[myButton setTitle:#"Play" forState:UIControlStateNormal];
[myButton setTitle:#"Stop" forState:UIControlStateSelected];
SWIFT 4
myButton.setTitle("Pause", for: .normal)
I found this to be somewhat out of date since attributed strings were added. Apparently titles of buttons assigned in a storyboard are attributed strings. Attributed titles take precedence over NSString titles, so if you want to use a simple string as a title you have to remove the attributed title first. I did as below, though there may be a better way. You could, of course, make your new title also an attributed string.
[myButton setAttributedTitle: nil forState: 0xffff];
[myButton setTitle: #"Play" forState: UIControlStateNormal];
And if you want to load it on page load, and support localized language:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.flagToChangeLabel){
NSString* newBtnTitle = NSLocalizedString(#"LOCALIZED_KEY", nil);
[self.laterButton setTitle:newBtnTitle forState:UIControlStateNormal];
[self.laterButton setTitle:newBtnTitle forState:UIControlStateSelected];
}
}

Resources