我希望能夠更改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)];
}
這對TextColor有意義。我沒有檢查,我不得不,謝謝。儘管我遇到問題的顏色是細分的tintColor。該段的段TintColor是否隱藏在TextAttributes內部? – scooter133 2012-02-28 18:36:39
看起來像tint不屬於屬性,你將不得不使用tintColor:方法來獲取它們,設置字體然後使用setTintColor: – valexa 2012-02-28 18:40:04