9

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 .

enter image description here

Please help me . Thank you in advance !!

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
Bharat Jagtap
  • 1,692
  • 2
  • 22
  • 35
  • So what happens when you use another font (say one that comes standard with iOS)? And what happens when you don't use a fractional font size? – bilobatum Feb 24 '14 at 06:55
  • @bilobatum If i use the default font then the text alignment is perfect . I also tried changing the font size to different values but no luck , the alignment is not right . – Bharat Jagtap Feb 24 '14 at 07:12
  • Can you try to set a background color to the text and see the amount of space being occupied above and below the text? It might be possible that the font is using some additional whitespace above and/ or below the text. – Akshat Singhal Feb 24 '14 at 08:09
  • 1
    you should be able to adjust the vertical alignment with `-setContentPositionAdjustment:forSegmentType:barMetrics:` – Emmanuel Feb 24 '14 at 12:59
  • I see this as a font issue, and not a segmented control issue. Where did you get that font? Who designed it? – bilobatum Feb 24 '14 at 21:19
  • @Emmanuel your code worked perfectly . Thank you so much . But how do I mark it as an answer because its a comment ? I used below code to pull the text a bit below [self.segmentType setContentPositionAdjustment:UIOffsetMake(0, 2) forSegmentType:UISegmentedControlSegmentAny barMetrics:UIBarMetricsDefault]; – Bharat Jagtap Feb 25 '14 at 06:20
  • @BharatJagtap You're welcome. Post the answer yourself with your corrected code, and accept it, this is OK for me. – Emmanuel Feb 25 '14 at 12:22

3 Answers3

12

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];
Bharat Jagtap
  • 1,692
  • 2
  • 22
  • 35
  • 1
    If you need a global solution for all of the segmented controls in your app, in the `AppDelegate` you can set `[[UISegmentedControl appearance] setContentPositionAdjustment:UIOffsetMake(0, 2) forSegmentType:UISegmentedControlSegmentAny barMetrics:UIBarMetricsDefault];` – Alex Jul 23 '14 at 16:23
  • Or you can place this code inside the drawrect method of a custom subclass of UISegmentedControl, in which case you just say `self` instead of `self.segmentType`. – CommaToast May 05 '15 at 15:44
0

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.

0

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.

CommaToast
  • 11,370
  • 7
  • 54
  • 69