请问有谁能告诉我如何更改 UISegmentedControl
的字体类型和大小?
请问有谁能告诉我如何更改 UISegmentedControl
的字体类型和大小?
我遇到了相同的问题。 这段代码设置整个分段控件的字体大小。 为设置字体类型可能需要类似的方法。 请注意,这仅适用于iOS5+。
Obj C:
->Objective-C:
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
编辑:UITextAttributeFont 已被弃用 - 请改用 NSFontAttributeName。
编辑 #2:对于 Swift 4,NSFontAttributeName 已更改为 NSAttributedStringKey.font。
Swift 5:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
Swift 4:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: font],
for: .normal)
Swift 3:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
Swift 2.2:
let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
forState: UIControlState.Normal)
多亏了 @audrey-gordeev 提供的Swift实现
[mySegmentedControl setTintColor:onColor forTag:kTagOnState];
和 [mySegmentedControl setTintColor:offColor forTag:kTagOffState];
并且再应用 [mySegmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
,那么我刚设置的颜色会消失。 - scooter133NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:12.0f]};
的意思是创建一个字典对象,其中包含了一个键值对,键为NSFontAttributeName
,值为字体大小为12.0且加粗的字体对象。 - Jason MooreboldSystemFontOfSize:
( System 的 S 大写) - Guillaume在iOS 5.0及以上版本中,请使用外观(Appearance)API:
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
UITextAttributeFont
已被弃用 - 请改用NSFontAttributeName
。 - Automate ThisUISegmentedControl
的字体。 - Nat这是接受答案的 Swift 版本:
Swift 3:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
Swift 2.2:
let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
forState: UIControlState.Normal)
另一种选择是对控件应用变换。但是,它会缩小包括控件边框在内的所有内容。
segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
Swift 风格:
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
[NSFontAttributeName: font]
- osrl这里我已经更新了iOS8。
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
XCode 8.1, Swift 3
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 24.0)],
forKeys: [NSFontAttributeName as NSCopying]) as? [AnyHashable : Any],
for: UIControlState.normal)
}
}
只需更改数字值(ofSize: 24.0)
// Set font-size and font-femily the way you want
UIFont *objFont = [UIFont fontWithName:@"DroidSans" size:18.0f];
// Add font object to Dictionary
NSDictionary *dictAttributes = [NSDictionary dictionaryWithObject:objFont forKey:NSFontAttributeName];
// Set dictionary to the titleTextAttributes
[yourSegment setTitleTextAttributes:dictAttributes forState:UIControlStateNormal];
nil
值。 - itzmebibinlet font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
12年前的答案(即使进行了编辑)也已经无法使用。
正确答案仅为:
let _font = UIFont.systemFont(ofSize: 10)
UISegmentedControl.appearance()
.setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)
在进行操作时,您很可能希望正确地更改高度:
import UIKit
class SmallerSegmentedControl: UISegmentedControl {
override init(frame: CGRect) {
super.init(frame: frame)
common()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
common()
}
func common() {
let _font = UIFont.systemFont(ofSize: 10)
UISegmentedControl.appearance()
.setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)
}
override var intrinsicContentSize:CGSize {
var s = super.intrinsicContentSize
s.height = 24
return s
}
}