UISegmentedControl - 改变颜色和大小 - 不正常工作

时间:2012-02-28 18:10:54

标签: objective-c ios uisegmentedcontrol uipinchgesturerecognizer

我希望能够更改UISegmentedControl的Segments的颜色和字体大小。我正在为每个段设置标记,然后为每个段设置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)];
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

我的工作是将TintColor设置为接近默认颜色的颜色,这样就创建了一个新的飞行端盖图像(我猜)并且所有字体缩放都很有效。