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

相關問題