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.
Related
I am adding a circular UIButton like this:
-(void)addInfoButton
{
UIButton *pButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pButton addTarget:self
action:#selector(infoButtonDidTap:)
forControlEvents:UIControlEventTouchUpInside];
pButton.frame = [self getInfoButtonFrame];
pButton.clipsToBounds = NO;
//half of the width
pButton.layer.cornerRadius = BUTTON_HEIGHT/2.0f;
pButton.layer.borderColor = self.tintColor.CGColor;
pButton.layer.borderWidth = 2.0f;
pButton.titleLabel.text = #"F";
pButton.titleLabel.textColor = self.tintColor;
pButton.titleLabel.textAlignment = NSTextAlignmentCenter;
self.filterInfoButton = pButton;
[self addSubview:pButton];
}
The text "F" is not visible when the button gets displayed. What could be the reason?
Edit:
On setting the corner radius as 0, the text is visible. But then, the button is not circular.
You set the title of a UIButton using
- setTitle:forState:
Sets the title to use for the specified state.
Using the attribute titleLabel does not set the text it is used to customize the button's title label.
titleLabel
A view that displays the value of the currentTitle property for a button. (read-only) Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:
button.titleLabel.font = UIFont.systemFontOfSize(12)
When I set the custom font for the segmented control then it changes the vertical text alignment. I am using below code to set the font .
// I dont think these lines are creating any issue but just wanted to paste all the code
self.segmentType.layer.borderColor = navigationTintColor.CGColor;
self.segmentType.layer.cornerRadius = 0.0;
self.segmentType.layer.borderWidth = 1.5;
// These are the lines that are changing the text alignment
UIFont *font = [UIFont fontWithName:ftHelveticaNeueLTPro_Th size:13.5];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:UITextAttributeFont];
[self.segmentType setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Here is the screenshot of whats is happening . If you observer, the text is not vertically centre aligned .
Please help me . Thank you in advance !!
The below code suggested by #Emmanuel works perfectly fine. You can change the vertical offset to align the text vertically at the center .
[self.segmentType setContentPositionAdjustment:UIOffsetMake(0, 2) forSegmentType:UISegmentedControlSegmentAny barMetrics:UIBarMetricsDefault];
Can you please try it using custom UILabel on custom view on it. Change & modify frame value of either titleLabel or customSegmentView as per convenience on actual view. And add this whole view as subview on your segmented control.
UIView *customSegmentView = [[UIView alloc] init];
UILabel *segmentTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 7.0f,180.0f,22.6f)];
segmentTitleLabel.text = #"your-text";
segmentTitleLabel.backgroundColor = [UIColor clearColor];
segmentTitleLabel.textAlignment = UITextAlignmentCenter;
segmentTitleLabel.font = [UIFont fontWithName:#"ftHelveticaNeueLTPro_Th" size:13.5f];
customSegmentView.frame = CGRectMake(60, 20, 180, 35);
[customSegmentView addSubview:segmentTitleLabel];
[self.segmentType setTitleView:customSegmentView];
Hope that will work for your issue. Please check and let me know if we have to go with another solution.
In InterfaceBuilder on XCode 6 there is a Content Offset control for the segments, which affects the baseline of the text. I had this problem because my Content Offset was 2 in the Y dimension instead of 0.
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
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.
I have a UI button that I'd like to put two labels on it, similar to how a cell has a title text and detail text.
I'd like the button to have a larger font for the main text, and have smaller detail text under that.
Is this possible? I've tried to put multiple lines on a button, but I need to have different text sizes for each line, so setting the lineBreakMode and numberOfLines of the titleLabel doesn't really quite work.
Here's the code we finally used. Assistance from John Wang.
Thanks to everyone for the suggestions!
// Formats a label to add to a button. Supports multiline buttons
// Parameters:
// button - the button to add the label to
// height - height of the label. usual value is 44
// offset - the offset from the top of the button
// labelText - the text for the label
// color - color of the text
// formatAsBold - YES = bold NO = normal weight
// tagNumber - tag for the label
- (void) formatLabelForButton: (UIButton *) button withHeight: (double) height andVerticalOffset: (double) offset andText: (NSString *) labelText withFontSize: (double) fontSize withFontColor: (UIColor *) color andBoldFont:(BOOL) formatAsBold withTag: (NSInteger) tagNumber {
// Get width of button
double buttonWidth= button.frame.size.width;
// Initialize buttonLabel
UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, buttonWidth, height)];
// Set font size and weight of label
if (formatAsBold) {
buttonLabel.font = [UIFont boldSystemFontOfSize:fontSize];
}
else {
buttonLabel.font = [UIFont systemFontOfSize:fontSize];
}
// set font color of label
buttonLabel.textColor = color;
// Set background color, text, tag, and font
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.text = labelText;
buttonLabel.tag = tagNumber;
// Center label
buttonLabel.textAlignment = UITextAlignmentCenter;
// Add label to button
[button addSubview:buttonLabel];
[buttonLabel autorelease];
} // End formatLabelForButton
A trick I would recommend is putting a UIButton with a transparent interior on top of UILabels. I've used this trick before and, although it may present some problems in terms of maintenance and i18n, it works like a charm.
Here is a 5 minutes sample using the suggestion above.
Given more time, you can make a better label with round corners.
you should be able to add subviews to it. Since everything is a view, everything can potentially have subviews.
I would subclass it and put the labels on it within the subclass, Then you can extend properties for text and subtext to change their values.
Not saying it can 100% work. But off the top of my head. UIView can have SubViews