フォントの種類とサイズを変更する方法を教えてくださいUISegmentedControl
。
17 に答える
私は同じ問題に遭遇しました。このコードは、セグメント化されたコントロール全体のフォント サイズを設定します。フォントタイプの設定にも同様のことが機能する場合があります。これはiOS5+でのみ利用可能であることに注意してください
オブジェクト 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
。
スイフト5:
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
スウィフト 4 :
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: font],
for: .normal)
スウィフト 3 :
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
スウィフト 2.2 :
let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
forState: UIControlState.Normal)
@audrey-gordeev の Swift 実装に感謝します
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
受け入れられた回答のSwiftバージョンは次のとおりです。
スウィフト 3 :
let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
for: .normal)
スウィフト 2.2 :
let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
forState: UIControlState.Normal)
もう 1 つのオプションは、コントロールに変換を適用することです。ただし、コントロールの境界線を含むすべてを縮小します。
segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
迅速なスタイル:
UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
Here i have updated for iOS8
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
ダニエルは私に正しい道を教えてくれました。こんな感じで使いました~
float scaleFactor = 0.8f;
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]
initWithFrame:CGRectMake(10, 70, 300/scaleFactor,35)];
[segmentedControl insertSegmentWithTitle:@"..." atIndex:0 animated:NO];
[segmentedControl insertSegmentWithTitle:@"..." atIndex:1 animated:NO];
[segmentedControl insertSegmentWithTitle:@"..." atIndex:2 animated:NO];
segmentedControl.transform = CGAffineTransformMakeScale(scaleFactor, 1);
CGPoint segmentedControlCenter = segmentedControl.center;
segmentedControlCenter.x = self.center.x;
segmentedControl.center = segmentedControlCenter;
スイフト4
let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.setTitleTextAttributes([NSFontAttributeName: font], for: .normal)
UISegmentedControl
フォントサイズを設定するための拡張機能。
extension UISegmentedControl {
@available(iOS 8.2, *)
func setFontSize(fontSize: CGFloat) {
let normalTextAttributes: [NSObject : AnyObject]!
if #available(iOS 9.0, *) {
normalTextAttributes = [
NSFontAttributeName: UIFont.monospacedDigitSystemFontOfSize(fontSize, weight: UIFontWeightRegular)
]
} else {
normalTextAttributes = [
NSFontAttributeName: UIFont.systemFontOfSize(fontSize, weight: UIFontWeightRegular)
]
}
self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
}
}
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 のテキストの色を設定しているだけですが、font プロパティを取得または設定することもできると思います。