1

我希望能够更改 UISegmentedControl 段的颜色和字体大小。我正在为每个段设置标签,然后为每个段设置 tintColor: forTag:。

更改颜色效果很好,直到我平移控件或捏它。在 UIPinchGestureRecognizer 代码中,我将 titleTextAttributes 设置为具有不同的字体大小。当我这样做时,段的颜色恢复为默认的加里颜色。

- (void)createElement {
if (multiStateControl == nil) {

        //Make our new switch
        //multiStateControl = [UIButton  buttonWithType:UIButtonTypeCustom];

    multiStateControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Off State Button", @"On State Button", nil]];

     multiStateControl.segmentedControlStyle = UISegmentedControlStyleBar;

    [multiStateControl setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIFont boldSystemFontOfSize:12.0f], UITextAttributeFont, 

      nil] 
                                     forState:UIControlStateNormal]; 

     [multiStateControl setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];

        // Set up the Contents Frame to the same origin as what we were but set the height/width to the new control.
    [elementViewContents setFrame:CGRectMake(elementViewContents.frame.origin.x, 
                                             elementViewContents.frame.origin.y, 
                                             CGRectGetWidth(multiStateControl.frame), 
                                             CGRectGetHeight(multiStateControl.frame))];


        //Set initial use to disabled
    [multiStateControl setOpaque:NO];
        // Set the default title for the button
            [multiStateControl setTag:kTagOffState forSegmentAtIndex:0];
            [multiStateControl setTag:kTagOnState forSegmentAtIndex:1];
            [multiStateControl setTintColor:onColor forTag:kTagOnState];
        [multiStateControl setTintColor:offColor forTag:kTagOffState];  

        // Lets get it on the screen
    [elementViewContents addSubview:multiStateControl];
   [multiStateControl release];

    [self contentSizeChanged];
}       
}

//捏手势

-(void) pinchElement:(UIPinchGestureRecognizer *)gestureRecognizer  {

    UIFont *existingFont = [[multiStateControl titleTextAttributesForState:UIControlStateNormal] objectForKey:UITextAttributeFont];

    CGFloat existingFontSize = [existingFont pointSize];
    CGFloat newFontSize = existingFontSize * [gestureRecognizer scale] ;

    [multiStateControl setTitleTextAttributes:
          [NSDictionary dictionaryWithObjectsAndKeys:
          [UIFont boldSystemFontOfSize:newFontSize],
          UITextAttributeFont, nil] 
                                     forState:UIControlStateNormal]; 

    [multiStateControl setFrame:CGRectMake(multiStateControl.frame.origin.x, multiStateControl.frame.origin.y, multiStateControl.frame.size.width+20,newFontSize *1.8)];
}
4

2 回答 2

0

然后您必须保留颜色属性,在设置新字体之前检索它们并在设置后再次设置它们。

于 2012-02-28T18:29:58.157 回答
0

当我没有设置 TintColor 然后增加 textAttributes 的字体大小时,这似乎会发生。就像默认颜色使用一些标准图像作为端盖一样。当我增加字体时,控件会增长,然后两端看起来会被拉伸。一位同事提到了像按钮一样的端盖。看起来最终应用程序被拉伸以适应新的控件大小。

我的解决方法是将 TintColor 设置为接近默认颜色的颜色,这样做会创建一个新的动态端盖图像(我猜),并且所有字体缩放效果都很好。

于 2012-03-06T04:56:34.993 回答