ios 更改 UISegmentedControl 的字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2280391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change font size of UISegmentedControl
提问by Aashutosh Tiwari
Can anyone please tell me how can I change the font type and size of UISegmentedControl
?
谁能告诉我如何更改字体类型和大小UISegmentedControl
?
回答by johngraham
I ran into the same issue. This code sets the font size for the entire segmented control. Something similar might work for setting the font type. Note that this is only available for iOS5+
我遇到了同样的问题。此代码设置整个分段控件的字体大小。类似的东西可能适用于设置字体类型。请注意,这仅适用于 iOS5+
Obj C:
对象 C:
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
EDIT: UITextAttributeFont
has been deprecated - use NSFontAttributeName
instead.
编辑:UITextAttributeFont
已被弃用 -NSFontAttributeName
改用。
EDIT #2: For Swift 4 NSFontAttributeName
has been changed to NSAttributedStringKey.font
.
编辑 #2:对于 Swift 4NSFontAttributeName
已更改为NSAttributedStringKey.font
.
Swift 5:
斯威夫特5:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
Swift 4:
斯威夫特 4:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: font],
for: .normal)
Swift 3:
斯威夫特 3:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
Swift 2.2:
斯威夫特 2.2:
let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
forState: UIControlState.Normal)
Thanks to the Swift implementations from @audrey-gordeev
感谢@audrey-gordeev 的 Swift 实现
回答by Michael Peterson
Use the Appearance API in iOS 5.0+:
在 iOS 5.0+ 中使用 Appearance API:
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
链接:http: //developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40010906
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
回答by Andrey Gordeev
Here is a Swift version of the accepted answer:
这是已接受答案的 Swift 版本:
Swift 3:
斯威夫特 3:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
Swift 2.2:
斯威夫特 2.2:
let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
forState: UIControlState.Normal)
回答by Daniel
Another option is to apply a transform to the control. However, it will scale down everything including the control borders.
另一种选择是对控件应用转换。但是,它将缩小包括控件边界在内的所有内容。
segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
回答by Peter Kreinz
Swift Style:
迅捷风格:
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
回答by karthikeyan
Here i have updated for iOS8
这里我已经更新了 iOS8
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
回答by H?sh H?sh
XCode 8.1, Swift 3
XCode 8.1,斯威夫特 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)
}
}
just change the number value (ofSize: 24.0)
只需更改数字值 (ofSize: 24.0)
回答by H?sh H?sh
// 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];
If you have any query, Contact me.
如果您有任何疑问,请联系我。
回答by t9mike
C# / Xamarin:
C#/Xamarin:
segment.SetTitleTextAttributes(new UITextAttributes {
Font = UIFont.SystemFontOfSize(font_size) }, UIControlState.Normal);
回答by drew..
Swift 4
斯威夫特 4
let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.setTitleTextAttributes([NSFontAttributeName: font], for: .normal)