2

为澄清起见,我将添加 2 个重叠的屏幕截图,一个在 Interface Builder 中,另一个在设备上。较低的 UISegmentedControl 刚从库中出来,没有编辑任何属性,但它在设备上看起来仍然不同(在这种情况下是非 Retina iPad,尽管 Retina-iPhone 的问题是相同的)(对于快速而肮脏的 photoshopping 感到抱歉)

边框样式 UISegmentedControl

有任何想法吗?

编辑:我显然尝试了界面生成器中实用程序选项卡中“控制”下的“对齐”。不幸的是,这些设置都没有改变 UISegment 中的标题。我认为他们不应该这样做,因为他们也没有更改 Interface Builder 中的标题。

EDIT2:以编程方式设置:

eyeSeg.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

也没有什么区别。

4

3 回答 3

2

发现问题“UISegmentedControlStyleBezeled 已弃用。请使用不同的样式。”

另请参阅我应该使用什么而不是不推荐使用的 uisegmentedcontrolstylebezeled-in-io

于 2012-06-08T14:23:34.293 回答
1

嗯……你检查对齐了吗?也许就是这样。

于 2012-06-07T23:47:05.510 回答
0

您可以递归地搜索分段控件UISegmentedControl中每个视图的子视图UILabels,然后更改每个UILabel的属性,包括textAlignment我在代码示例中显示的属性。感谢 Primc 响应更改 UISegmented Control 的字体大小的帖子,他建议使用这种通用方法来自定义UILabels. UISegmentedControl顺便说一句,即使在它被弃用之后,我也一直在使用这种UISegmentedControlStyleBezeled风格的代码,尽管我最近切换到UISegmentedControlStyleBar了调整过的框架高度。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Adjust the segment widths to fit the text.  (Will need to calculate widths if localized text is ever used.)
    [aspirationControl setWidth:66 forSegmentAtIndex:0]; // Navel Lint Collector
    [aspirationControl setWidth:48 forSegmentAtIndex:1]; // Deep Thinker
    [aspirationControl setWidth:49 forSegmentAtIndex:2]; // Mental Wizard
    [aspirationControl setWidth:64 forSegmentAtIndex:3]; // Brilliant Professor
    [aspirationControl setWidth:58 forSegmentAtIndex:4]; // Nobel Laureate

    // Reduce the font size of the segmented aspiration control
    [self adjustSegmentText:aspirationControl];
}

- (void)adjustSegmentText:(UIView*)view {
    // A recursively called method for finding the subviews containing the segment text and adjusting frame size, text justification, word wrap and font size
    NSArray *views = [view subviews];
    int numSubviews = views.count;

    for (int i=0; i<numSubviews; i++) {
        UIView *thisView = [views objectAtIndex:i];
        // Typecast thisView to see if it is a UILabel from one of the segment controls
        UILabel *tmpLabel = (UILabel *) thisView;
        if ([tmpLabel respondsToSelector:@selector(text)]) {
            // Enlarge frame.  Segments are set wider and narrower to accomodate the text.
            CGRect segmentFrame = [tmpLabel frame];
            // The following origin values were necessary to avoid text movement upon making an initial selection but became unnecessary after switching to a bar style segmented control
            // segmentFrame.origin.x = 1;
            // segmentFrame.origin.y = -1;
            segmentFrame.size.height = 40;
            // Frame widths are set equal to 2 points less than segment widths set in viewDidLoad
            if ([[tmpLabel text] isEqualToString:@"Navel Lint Collector"]) {
                segmentFrame.size.width = 64;
            }
            else if([[tmpLabel text] isEqualToString:@"Deep Thinker"]) {
                segmentFrame.size.width = 46;
            }
            else if([[tmpLabel text] isEqualToString:@"Mental Wizard"]) {
                segmentFrame.size.width = 47;
            }
            else if([[tmpLabel text] isEqualToString:@"Brilliant Professor"]) {
                segmentFrame.size.width = 62;
            }
            else {
                // @"Nobel Laureate"
                segmentFrame.size.width = 56;
            }

            [tmpLabel setFrame:segmentFrame];
            [tmpLabel setNumberOfLines:0];  // Change from the default of 1 line to 0 meaning use as many lines as needed
            [tmpLabel setTextAlignment:UITextAlignmentCenter];
            [tmpLabel setFont:[UIFont boldSystemFontOfSize:12]];
            [tmpLabel setLineBreakMode:UILineBreakModeWordWrap];

        }

        if (thisView.subviews.count) {
            [self adjustSegmentText:thisView];
        }
    }
}

分段控制标签文本在 IB 中外观难看,但使用上述代码在设备和模拟器中完美居中并包裹在 2 行中。

于 2012-07-17T01:25:23.203 回答