Oks*_*ana 61 iphone cocoa-touch objective-c uisegmentedcontrol
我尝试将字体颜色从白色更改为黑色UISegmentedControl(适用于iOS 4.*)
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor redColor];
for (id segment in [button subviews]) {
for (id label in [segment subviews]) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *) label;
[titleLabel setTextColor:[UIColor blackColor]];
}
}
}
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
Run Code Online (Sandbox Code Playgroud)
但文字颜色没有改变.我应该做些什么来改变文字颜色UISegmentedControl?
pbi*_*gal 119
在iOS 6.0及更高版本中,它非常简单:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:17], NSFontAttributeName,
[UIColor blackColor], NSForegroundColorAttributeName,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
i--*_*i-- 70
如果您需要更改iOS 7中突出显示的片段的文本颜色,这里有一个解决方案(花了我一段时间才能找到,但感谢这篇文章):
Objective-C的
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
迅速
let titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, forState: .Selected)
Run Code Online (Sandbox Code Playgroud)
Nim*_*ekh 30
下面是将字体设置为粗体和点大小的代码:
UIFont *Boldfont = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont
forKey: NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
我希望它有所帮助
小智 22
Swift 4代码将状态字体颜色设置为黑色
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal)
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
Run Code Online (Sandbox Code Playgroud)
小智 6
for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) {
if ([v isKindOfClass:[UILabel class]]) {
UILabel *label=(UILabel *)[v retain];
lable.textColor=[UIColor blackColor];
}
}
Run Code Online (Sandbox Code Playgroud)
适用于iOS 3.0及更高版本
已针对Swift 4更新-使用此扩展程序(因为扩展程序总是很棒.. !!)
extension UISegmentedControl {
func defaultConfiguration(font: UIFont = UIFont.systemFont(ofSize: 12), color: UIColor = UIColor.gray) {
let defaultAttributes = [
NSAttributedStringKey.font.rawValue: font,
NSAttributedStringKey.foregroundColor.rawValue: color
]
setTitleTextAttributes(defaultAttributes, for: .normal)
}
func selectedConfiguration(font: UIFont = UIFont.boldSystemFont(ofSize: 12), color: UIColor = UIColor.red) {
let selectedAttributes = [
NSAttributedStringKey.font.rawValue: font,
NSAttributedStringKey.foregroundColor.rawValue: color
]
setTitleTextAttributes(selectedAttributes, for: .selected)
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在相应的类中使用这些功能,
@IBOutlet weak var segment: UISegmentedControl!
segment.defaultConfiguration()
// or provide paramater as per your requirement
segment.selectedConfiguration(color: .blue)
Run Code Online (Sandbox Code Playgroud)
以防万一帮助其他人到达那里并且正在使用 swift 工作。
我将提出两种可能的方法。您可以UIAppearance在正在工作的分段中或直接在分段中更改文本属性。
第一个示例在外观中设置属性,这样您将自定义应用程序中的所有分段控件:
let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
UISegmentedControl.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
UISegmentedControl.appearance().setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
Run Code Online (Sandbox Code Playgroud)
第二个例子,直接在segmented中,只会自定义这个segmented:
let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
segmented.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
segmented.setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
Run Code Online (Sandbox Code Playgroud)
在 Swift 5 中,您可以使用此语法更改颜色
斯威夫特 5
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
groupSegment.setTitleTextAttributes(titleTextAttributes, for: .selected)
Run Code Online (Sandbox Code Playgroud)
Swift 5扩展
extension UISegmentedControl {
func setTitleColor(_ color: UIColor, state: UIControl.State = .normal) {
var attributes = self.titleTextAttributes(for: state) ?? [:]
attributes[.foregroundColor] = color
self.setTitleTextAttributes(attributes, for: state)
}
func setTitleFont(_ font: UIFont, state: UIControl.State = .normal) {
var attributes = self.titleTextAttributes(for: state) ?? [:]
attributes[.font] = font
self.setTitleTextAttributes(attributes, for: state)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53815 次 |
| 最近记录: |