Text Alignment issue when I set the custom font

我是研究僧i 提交于 2019-12-05 18:22:40

问题


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 !!


回答1:


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];



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/21980424/text-alignment-issue-when-i-set-the-custom-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!