我希望能够更改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