有人能告诉我如何改变UISegmentedControl的字体类型和大小吗?
当前回答
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)
其他回答
这是为objective - c添加你的分段控件名称来代替mysegmentedcontrol
UIFont *font = [UIFont systemFontOfSize:11.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:UITextAttributeFont];
[mySegmentedcontrol setTitleTextAttributes:attributes forState:UIControlStateNormal];
希望能有所帮助
C# / Xamarin:
segment.SetTitleTextAttributes(new UITextAttributes {
Font = UIFont.SystemFontOfSize(font_size) }, UIControlState.Normal);
在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
另一种选择是对控件应用转换。然而,它将缩小一切,包括控制边界。
segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
这里我已经更新了iOS8
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
推荐文章
- 保留HTML字体大小时,iPhone的方向从纵向改变为横向
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?