UISegmentedControl 改变选中字体的颜色
时间: 2024-01-21 22:04:32 浏览: 16
你可以通过设置 UISegmentedControl 的标题文本属性来改变选中字体的颜色。具体方法如下:
1. 创建一个字典,设置选中状态下的字体颜色:
```
NSDictionary *selectedAttributes = @{NSForegroundColorAttributeName: [UIColor redColor]};
```
2. 设置 UISegmentedControl 的标题文本属性:
```
[segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
```
这样,当选中某个选项时,它的字体颜色就会变成红色。
相关问题
uni-segmented-control 修改字体颜色
A: 您可以使用NSAttributedString来设置每个选项的字体和颜色。以下是一个示例代码:
```
let segmentedControl = UISegmentedControl(items: ["Option 1", "Option 2"])
segmentedControl.tintColor = .white // 设置背景颜色
segmentedControl.backgroundColor = .black // 设置前景颜色
let normalAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.white,
.font: UIFont.systemFont(ofSize: 16)
]
// 设置普通状态下的字体颜色和字体
segmentedControl.setTitleTextAttributes(normalAttributes, for: .normal)
let selectedAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black,
.font: UIFont.systemFont(ofSize: 16, weight: .bold)
]
// 设置选中状态下的字体颜色和字体
segmentedControl.setTitleTextAttributes(selectedAttributes, for: .selected)
```
在这个示例中,我们首先设置了控件的背景和前景颜色。然后,我们使用NSAttributedString来设置普通状态和选中状态下的字体颜色和字体。在普通状态下,字体颜色为白色,字体为16号系统字体。在选中状态下,字体颜色为黑色,字体为16号粗体系统字体。您可以根据自己的需要修改这些属性。
uisegmentedcontrol
UISegmentedControl是iOS开发中的一个控件,它通常用于在多个选项之间进行选择。它由一组分段控件组成,每个分段控件都代表一个选项。用户可以通过点击分段控件来选择不同的选项。UISegmentedControl通常用于导航栏、工具栏或表格视图中。