see above attached image.
It's programatically written in Objective-C. UISegmentController is cutting title text.
Weird thing is, it shows 'Privacy Policy' full text which is way longer than 'About' and 'Terms', which is cutting.
Can anybody please help in this?
code is given below:
UISegmentedControl *unitsPicker = [[UISegmentedControl alloc] initWithItems:#[#"About", #"Terms", #"Privacy Policy"]];
unitsPicker.frame = CGRectMake(0, CGRectGetMaxY(unitsLabel.frame) + kBottomPaddingConstant, CGRectGetWidth(_unitsContainer.frame), segmentHeightConstant);
unitsPicker.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
unitsPicker.selectedSegmentIndex = ![TemperatureUtility unitIsCelsius];
[unitsPicker addTarget:self action:#selector(onUnitPickerChanged:) forControlEvents:UIControlEventValueChanged];
unitsPicker.layer.cornerRadius = 5.0f;
unitsPicker.layer.borderColor = [UIColorFromRGB(0xE9E9E9) CGColor];
unitsPicker.layer.borderWidth = 1.0f;
unitsPicker.layer.masksToBounds = YES;
unitsPicker.clipsToBounds = YES;
[_unitsContainer addSubview:unitsPicker];
Try setting:
segmentedControl.apportionsSegmentWidthsByContent = YES;
From the docs:
If the value of this property is true, for segments whose width value is 0, the control attempts to adjust segment widths based on their content widths.
The default is false.
Swift 5 and 5+ Solution
Add
mySegmentController.apportionsSegmentWidthsByContent = true
inside viewDidLoad() {}
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)
below are some other properties of the label. obviously nstextalignmentleft is not what i'm going for. having trouble understanding where to enter coordinates.
self.lblTimer = [[UILabel alloc] init];
self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
self.lblTimer.textAlignment = NSTextAlignmentLeft;
self.lblTimer.font = [UIFont systemFontOfSize:10];
self.lblTimer.textColor = [UIColor redColor];`self.lblTimer = [[UILabel alloc] init];
I think you're misunderstanding what textAlignment does. This simply controls the alignment within the label, not on the screen. If you want to position the label on screen you must change its frame property:
self.lblTimer.frame = CGRectMake(x,y,width,height);
As you've set self.lblTimer.translatesAutoresizingMaskIntoConstraints to NO I am guessing you want to use Auto Layout and if so, I highly recommend taking a look at Masonry.
An alternative is to set the frame property on the label with requires a CGRect where you give it the x and y coordinates as well as the width and height.
I'm working on example from "iOS Programming Cookbook", based on iOS 7. I need to create UITextField and add it to ViewController.
In ViewController.m I have:
- (void) viewDidLoad {
[super viewDidLoad];
[self createField];
}
- (void) createField {
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 35.0f, 280.0f, 30.0f)];
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = #"Enter text to share";
self.textField.delegate = self;
[self.view addSubview:self.textField];
}
On screenshot from book textField appears in the middle of screen's width under the status bar, but when I run it textField appears on the top left corner of screen behind the status bar.
Maybe the problem is, that I run app on iPhone 6 simulator with iOS 8. But I don't know how to solve it.
Thanks!
Using self.textField.translatesAutoresizingMaskIntoConstraints = NO; is pretty much telling the object that it doesn't really care about the frame but relies more on the constraints that you give it. Setting it to YES takes the frame and automatically applies constraints to it to mimic the frame that you give it.
For example it would apply the constraints to have the frame appear at: CGRectMake(20.0f, 35.0f, 280.0f, 30.0f). When setting it to NO use NSLayoutConstraint and create the constraints programatically.
But in your case just remove the line
self.textField.translatesAutoresizingMaskIntoConstraints = NO;
because it is set to YES by default.
I have a button in my ios project which displays edit profile if the user is the current user and follow if the user is not the current user.
I am setting the titleLabel text programmatically dependant on the above condition. I am also programatically setting the button text to align center.
The problem I'm having is it works perfectly for the edit profile button which appears as it should center aligned but when showing the follow button the word follow is slightly off center to the left and I can't figure out where I'm going wrong.
Here is my code:
if([userID isEqualToString:credentialID]){
self.followEditBtn.layer.cornerRadius = 2;
self.followEditBtn.layer.borderColor = UIColorFromRGB(0xA6B9C1).CGColor;
self.followEditBtn.layer.borderWidth = 1.0f;
self.followEditBtn.layer.backgroundColor = UIColorFromRGB(0xF8FBFC).CGColor;
self.followEditBtn.titleLabel.textColor = UIColorFromRGB(0xA1B1C3);
self.followEditBtn.titleLabel.text = #"Edit Profile";
self.followEditBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
} else {
self.followEditBtn.layer.cornerRadius = 2;
self.followEditBtn.layer.borderColor = UIColorFromRGB(0x19A548).CGColor;
self.followEditBtn.layer.borderWidth = 1.0f;
self.followEditBtn.layer.backgroundColor = [UIColor whiteColor].CGColor;
self.followEditBtn.titleLabel.textColor = UIColorFromRGB(0x19A548);
self.followEditBtn.titleLabel.text = #"Follow";
self.followEditBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
And some images of the buttons as you can see the follow text is slightly off center to the left yet the edit profile button is correctly aligned:
I couldn't reproduce your problem (iOS 6.1 to 7.1). Although I had to make a small change in order to make it work, since it wasn't showing any text.
Instead of using:
self.followEditBtn.titleLabel.text = #"Edit Profile";
you should use:
[self.followEditBtn setTitle:#"Follow" forState:UIControlStateNormal];
I'm assuming you're using a UIButtonTypeCustom type UIButton, and having a fixed frame size.
I'd like you to tell me where are you putting this code, and how is this button being initialized.
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.