Aas*_*ari 217 iphone cocoa-touch font-size uisegmentedcontrol ios
任何人都可以告诉我如何更改字体类型和大小UISegmentedControl
?
joh*_*ham 482
我遇到了同样的问题.此代码设置整个分段控件的字体大小.类似的东西可能适用于设置字体类型.请注意,这仅适用于iOS5 +
对象C:
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
编辑:UITextAttributeFont
已被弃用 - NSFontAttributeName
改为使用.
编辑#2:对于Swift 4 NSFontAttributeName
已经改为NSAttributedStringKey.font
.
斯威夫特4:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: font],
for: .normal)
Run Code Online (Sandbox Code Playgroud)
Swift 2.2:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
Run Code Online (Sandbox Code Playgroud)
感谢@ audrey-gordeev的Swift实现
Mic*_*son 51
使用iOS 5.0+中的Appearance API:
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
链接: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
And*_*eev 35
这是接受答案的Swift版本:
斯威夫特3:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
Run Code Online (Sandbox Code Playgroud)
Swift 2.2:
let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
小智 13
另一种选择是将变换应用于控件.但是,它会缩小包括控制边框在内的所有内容.
segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
Run Code Online (Sandbox Code Playgroud)
斯威夫特风格:
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
这里我更新了iOS8
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
小智 8
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)
}
}
Run Code Online (Sandbox Code Playgroud)
只需更改数值 (ofSize: 24.0)
归档时间: |
|
查看次数: |
95469 次 |
最近记录: |