1

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

更改颜色效果很好,直到我平移控件或捏。在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)]; 
} 

回答

0

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

我的工作是将TintColor设置为接近默认颜色的颜色,这样做创建了一个新的动态端帽图像(我猜),所有的字体缩放都很好。

0

然后,你必须保持颜色属性,设置新的字体之前进行检索,并将其设置后重新设定。

+0

这对TextColor有意义。我没有检查,我不得不,谢谢。尽管我遇到问题的颜色是细分的tintColor。该段的段TintColor是否隐藏在TextAttributes内部? – scooter133 2012-02-28 18:36:39

+0

看起来像tint不属于属性,你将不得不使用tintColor:方法来获取它们,设置字体然后使用setTintColor: – valexa 2012-02-28 18:40:04

相关问题