UISegmentedControl截断段标题

时间:2013-09-19 09:04:12

标签: iphone uisegmentedcontrol ios7

我的iPhone应用程序中有一个分段控件在ios6上效果很好但是在ios7上,段瓦片被截断(文本有足够的空间,但无论如何它都会截断它们)

    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSDictionary *defaultTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:defaultFont,
                                       UITextAttributeFont,
                                       defaultFontColor,
                                       UITextAttributeTextColor,
                                       nil];
[self.segmentedControl setTitleTextAttributes:defaultTextAttributes forState:UIControlStateNormal];

NSDictionary *selectedTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:selectedFont,
                                        UITextAttributeFont,
                                        selectedFontColor,
                                        UITextAttributeTextColor,
                                        nil];
[self.segmentedControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateHighlighted];

有关如何解决这个问题的任何消息?为什么它会在ios6上正常工作,但在ios7上截断标题......

这是导致问题的代码......

      UIImage *unselectedBackgroundImage = [[UIImage imageNamed:@"product_details_segmentBckg"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    /* Selected background */
    UIImage *selectedBackgroundImage = [[UIImage imageNamed:@"product_details_segmentBckg"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    /* Image between two unselected segments */
    UIImage *bothUnselectedImage = [[UIImage imageNamed:@"product_details_segmentBckg"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 1, 0)];
     /* Image between segment selected on the left and unselected on the right */
    UIImage *leftSelectedImage = [[UIImage imageNamed:@"product_details_segmentBckg"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 1, 0)];
    /* Image between segment selected on the right and unselected on the right */
    UIImage *rightSelectedImage = [[UIImage imageNamed:@"product_details_segmentBckg"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 1, 0)];

    if ([UIImage instancesRespondToSelector:@selector(imageWithRenderingMode:)]) {
#ifdef __IPHONE_7_0
        unselectedBackgroundImage = [unselectedBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        selectedBackgroundImage = [selectedBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        bothUnselectedImage = [bothUnselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        leftSelectedImage = [leftSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        rightSelectedImage = [rightSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
#endif
    }


    [[UISegmentedControl appearance] setBackgroundImage:unselectedBackgroundImage
                                               forState:UIControlStateNormal
                                             barMetrics:UIBarMetricsDefault];

    [[UISegmentedControl appearance] setBackgroundImage:selectedBackgroundImage
                                               forState:UIControlStateSelected
                                             barMetrics:UIBarMetricsDefault];

    [[UISegmentedControl appearance] setDividerImage:bothUnselectedImage
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

    [[UISegmentedControl appearance] setDividerImage:leftSelectedImage
                                 forLeftSegmentState:UIControlStateSelected
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];

    [[UISegmentedControl appearance] setDividerImage:rightSelectedImage
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateSelected
                                          barMetrics:UIBarMetricsDefault];

如果我发表评论,标签显示正常......但我需要背景图片...... 那么如何保持ios6的兼容性,同时在ios7上具有自定义的分段控件

5 个答案:

答案 0 :(得分:2)

我通过为所选状态设置与正常状态相同的标题文本属性来解决这个问题。 在iOS 6中,这已经足够了:

NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   someUIFont, UITextAttributeFont,
                                   nil];

[self.segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];

现在我必须添加它才能使其适用于iOS 7:

[self.segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];

答案 1 :(得分:1)

经过一些研究,这是我对该问题的解决方案(Swift 3,iOS 10)

class CustomSegmentedControl: UISegmentedControl {

override func draw(_ rect: CGRect) {

    for segment in subviews {
        for subview in segment.subviews {
            if let segmentLabel = subview as? UILabel {
                segmentLabel.numberOfLines = 0
                segmentLabel.translatesAutoresizingMaskIntoConstraints = false
                segmentLabel.leadingAnchor.constraint(equalTo: segment.leadingAnchor).isActive = true
                segmentLabel.trailingAnchor.constraint(equalTo: segment.trailingAnchor).isActive = true
                segmentLabel.topAnchor.constraint(equalTo: segment.topAnchor).isActive = true
                segmentLabel.bottomAnchor.constraint(equalTo: segment.bottomAnchor).isActive = true
            }
        }
    }
}

override func didMoveToSuperview() {
    self.layer.cornerRadius = 5
    self.clipsToBounds = true
    self.backgroundColor = .white
    self.tintColor = UIColor.orange

    let font = UIFont(name: "Helvetica", size: 12.0)
    self.setTitleTextAttributes([NSFontAttributeName: font!], for: .normal)

    let fontBold = UIFont(name: "Helvetica-Bold", size: 12.0)
    self.setTitleTextAttributes([NSFontAttributeName: fontBold!], for: .selected)
}
}

答案 2 :(得分:0)

Change width of a segmented control based on titles in each segment?  该问题的公认答案也是这个问题的解决方案

答案 3 :(得分:0)

我尝试了一些解决方案,以避免重新计算所有分段大小 为解决我的问题,我将此代码放在分段控件的valueChange操作上:

for(int i=0;i<setControl.subviews.count; i++){
  NSString *title=[segControl titleForSegmentAtIndex:i];
  [segControl setTitle:title forSegmentAtIndex:i];
}

在iOS7上运行的iOS6(使用Xcode 4.6.3)编译的App会出现问题。应用程序从暂停状态重新激活但未在所有视图中重新激活后,可以看到故障。

答案 4 :(得分:0)

我正在更新标题,由于原始标题的长度小于新标题的长度,因此该标题被截断了。 因此,我在原始标题的文本中添加了永久的不间断空格\u{00a0},以匹配标题前后的长度。