Lettergrootte van UISegmentedControl wijzigen

Kan iemand mij vertellen hoe ik het lettertype en de grootte van UISegmentedControlkan wijzigen?


Antwoord 1, autoriteit 100%

Ik kwam hetzelfde probleem tegen. Deze code stelt de lettergrootte in voor het gehele gesegmenteerde besturingselement. Iets soortgelijks zou kunnen werken voor het instellen van het lettertype. Merk op dat dit alleen beschikbaar is voor iOS5+

Obj C:

UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                       forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes 
                                forState:UIControlStateNormal];

EDIT: UITextAttributeFontis verouderd – gebruik in plaats daarvan NSFontAttributeName.

EDIT #2: Voor Swift 4 is NSFontAttributeNamegewijzigd in NSAttributedStringKey.font.

Swift 5:

let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)

Swift 4:

let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: font],
                                        for: .normal)

Swift 3:

let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
                                        for: .normal)

Swift 2.2:

let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], 
    forState: UIControlState.Normal)

Dankzij de Swift-implementaties van @audrey-gordeev


Antwoord 2, autoriteit 10%

Gebruik de Appearance API in iOS 5.0+:

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

Links:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40010906

http://www.raywenderlich.com/4344/ gebruikersinterface-aanpassing-in-ios-5


Antwoord 3, autoriteit 7%

Hier is een Swift-versie van het geaccepteerde antwoord:

Swift 3:

let font = UIFont.systemFont(ofSize: 16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
                                        for: .normal)

Swift 2.2:

let font = UIFont.systemFontOfSize(16)
segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], 
    forState: UIControlState.Normal)

Antwoord 4, autoriteit 3%

Een andere optie is om een ​​transformatie op het besturingselement toe te passen. Het zal echter alles verkleinen, inclusief de controlegrenzen.

segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);

Antwoord 5, autoriteit 2%

Snelle stijl:

UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)

Antwoord 6, autoriteit 2%

Hier heb ik geüpdatet voor iOS8

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], NSFontAttributeName, nil] forState:UIControlStateNormal];

Antwoord 7

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)
    }
}

wijzig gewoon de getalwaarde (ofSize: 24.0)

Voorbeeld


Antwoord 8

In swift 5,

let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)

Antwoord 9

// 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];

Neem contact met mij op als u vragen heeft.


Antwoord 10

C# / Xamarin:

segment.SetTitleTextAttributes(new UITextAttributes { 
    Font = UIFont.SystemFontOfSize(font_size) }, UIControlState.Normal);

Antwoord 11

Daniel wees me op de juiste manier. Ik heb het zo gebruikt-

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;

Antwoord 12

UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFont(ofSize: 16.0)],
                                                                        forKeys: [kCTFontAttributeName as! NSCopying]) as? [AnyHashable : Any],
                                                           for: UIControlState.normal)

Antwoord 13

Swift 4

let font = UIFont.systemFont(ofSize: 16)
UISegmentedControl.setTitleTextAttributes([NSFontAttributeName: font], for: .normal)

Antwoord 14

Extensie voor UISegmentedControlvoor het instellen van lettergrootte.

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)
    }
 }

Antwoord 15

Je kunt het daadwerkelijke lettertype voor het UILabel vinden door recursief elk van de weergaven te onderzoeken, beginnend met de UISegmentedControl. Ik weet niet of dit de beste manier is om het te doen, maar het werkt.

@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

In de bovenstaande code stel ik alleen de tekstkleur van het UILabel in, maar je zou ook de font-eigenschap kunnen pakken of instellen, neem ik aan.


Antwoord 16

dit is voor doel c voeg uw gesegmenteerde controlenaam toe in plaats van mysegmentedcontrol

UIFont *font = [UIFont systemFontOfSize:11.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                            forKey:UITextAttributeFont];
[mySegmentedcontrol setTitleTextAttributes:attributes                                    forState:UIControlStateNormal];

hoop dat het helpt


Antwoord 17

Correct antwoord voor 2021. De syntaxis is gewijzigd.

Het 12 jaar oude antwoord (zelfs de bewerkingen) is niet correct.

Het is gewoon:

let _font = UIFont.systemFont(ofSize: 10)
UISegmentedControl.appearance()
 .setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)

Het is zeer waarschijnlijk dat u de hoogte correct wilt wijzigen terwijl u toch bezig bent:

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
    }
}

Other episodes