【问题标题】:UISegmentedControl - Changing Colors and Size - Not working RightUISegmentedControl - 更改颜色和大小 - 无法正常工作
【发布时间】:2012-03-18 05:54:57
【问题描述】:

我希望能够更改 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)];
}

【问题讨论】:

    标签: objective-c ios uisegmentedcontrol uipinchgesturerecognizer


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这对 TextColor 是有意义的。我不检查那个,我必须检查,谢谢。虽然我遇到问题的颜色是段的 tintColor 。段的 TintColor 是否隐藏在 TextAttributes 内?
      • 看起来 tint 不在属性中,您必须使用 tintColor: 方法来获取它们,设置字体然后使用 setTintColor:
      猜你喜欢
      • 2021-02-16
      • 2014-04-12
      • 2016-04-14
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      相关资源
      最近更新 更多