更改UISegmentedControl的文本颜色

时间:2012-10-27 04:38:41

标签: objective-c xcode

我是目标c的新手,需要在UIsegmentControl中更改所选段的文本颜色。 使用以下代码。

 [[UIsegmentControl.subviews objectAtIndex:segment.selectedSegmentIndex] setTintColor:[UIColor redColor]];

它改变了段颜色。请帮助我..

7 个答案:

答案 0 :(得分:39)

无法在UISegmentedControl中设置所选片段标题的自定义颜色。 UIControlState中的forState:用于设置正常和选定状态的段文本属性。

来自您的代码:

[[UIsegmentControl.subviews objectAtIndex:segment.selectedSegmentIndex] setTintColor:[UIColor redColor]];

试用此代码:

[segmnt_cntrl setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:16.0],
                                                          NSForegroundColorAttributeName:[UIColor redColor],
                                                          NSShadowAttributeName:shadow}
                                                         forState:UIControlStateNormal];

将segmnt_cntrl替换为Segment Cotrol的对象。试试这个,它可以帮助你实现你的全部目标。

由于

答案 1 :(得分:18)

如果你需要更改iOS 7中突出显示的片段的文字颜色,这里有一个解决方案(花了我一段时间才找到,但是thanks to this post):

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorBlack]} forState:UIControlStateSelected];

答案 2 :(得分:5)

引用@ i--回答

更新了Swift 4.1

# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH  FALSE)

# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

# the RPATH to be used when installing
SET(CMAKE_INSTALL_RPATH "${FCL_SOURCE_DIR}/libs")

# don't add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)


target_link_libraries (rtcc
    ${Boost_LIBRARIES}
    ${FCL_SOURCE_DIR}/libs/libssl.so
    ${FCL_SOURCE_DIR}/libs/libcrypto.so
)

install(TARGETS rtcc DESTINATION bin
    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)

Swift 3.1

UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .selected)

对于早期的Swift版本:

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red as Any], for: .selected)

答案 3 :(得分:2)

在UISegmentedControl中没有标准API来设置单个段的文本属性。您可以使用未经推荐的方法挖掘分段控件的视图层次结构,找到所需的UILabel(如果有)并设置该标签的属性。更好的方法是查找(或编写)模拟UISegmentedControl的自定义控件,并允许以您需要的方式自定义单个段。

编辑:

实际上,我从错误的角度来看待这个问题。我的答案是基于尝试为特定的段索引设置属性。但事实上,这可以通过设置UIControlStateSelected状态的文本属性来实现。对不起,感到困惑。

答案 4 :(得分:0)

更新了@Babul Prabhakar对Swift 3的回答,因为大约有六个小小的东西发生了变化:

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red as Any], for: .selected)

答案 5 :(得分:0)

除了@Babl Prabhakar的答案

迅速5: UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)

答案 6 :(得分:0)

目标C

设置字体样式,大小和颜色

NSDictionary *attributes = [NSDictionary dictionaryWithObject: [UIFont fontWithName: @"Arial" size:16] forKey:NSFontAttributeName];
[self->mySegmentControl setTitleTextAttributes: attributes forState: normal];
[self->mySegmentControl setTitleTextAttributes: @{NSForegroundColorAttributeName: UIColor.blueColor} forState: UIControlStateSelected];