有人能告诉我如何改变UISegmentedControl的字体类型和大小吗?
当前回答
另一种选择是对控件应用转换。然而,它将缩小一切,包括控制边界。
segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
其他回答
另一种选择是对控件应用转换。然而,它将缩小一切,包括控制边界。
segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
在swift 5中,
let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
// 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];
如果您有任何疑问,请联系我。
你可以通过递归检查从UISegmentedControl开始的每个视图来获得UILabel的实际字体。我不知道这是不是最好的方法,但它确实有效。
@interface tmpSegmentedControlTextViewController : UIViewController {
}
@property (nonatomic, assign) IBOutlet UISegmentedControl * theControl;
@end
@implementation tmpSegmentedControlTextViewController
@synthesize theControl; // UISegmentedControl
- (void)viewDidLoad {
[self printControl:[self theControl]];
[super viewDidLoad];
}
- (void) printControl:(UIView *) view {
NSArray * views = [view subviews];
NSInteger idx,idxMax;
for (idx = 0, idxMax = views.count; idx < idxMax; idx++) {
UIView * thisView = [views objectAtIndex:idx];
UILabel * tmpLabel = (UILabel *) thisView;
if ([tmpLabel respondsToSelector:@selector(text)]) {
NSLog(@"TEXT for view %d: %@",idx,tmpLabel.text);
[tmpLabel setTextColor:[UIColor blackColor]];
}
if (thisView.subviews.count) {
NSLog(@"View has subviews");
[self printControl:thisView];
}
}
}
@end
在上面的代码中,我只是设置了UILabel的文本颜色,但你也可以获取或设置字体属性。
2021年的正确答案。语法改变了。
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
}
}
推荐文章
- 保留HTML字体大小时,iPhone的方向从纵向改变为横向
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?