2015-01-14 19 views
31

Làm thế nào để thay đổi kích thước phông chữ và tên phông chữ của uisegmentedcontrol theo lập trình? Tôi đã nhanh chóng sử dụng.làm thế nào để thay đổi kích thước phông chữ và tên phông chữ của uisegmentedcontrol lập trình trên nhanh chóng?

Đây là mã của tôi:

self.mysegmentedControl = UISegmentedControl(items: [ 
     NSLocalizedString("Aaaaaa", comment: ""), 
     NSLocalizedString("Bbbbbb", comment: ""), 
     NSLocalizedString("Cccccc", comment: ""), 
     NSLocalizedString("Dddddd", comment: ""), 
     NSLocalizedString("Eeeeee", comment: ""), 
     ]) 
self.mysegmentedControl.addTarget(self, action: "mysegmentedControlDidChange:", forControlEvents: .ValueChanged) 
self.mysegmentedControl.selectedSegmentIndex = 0 

liên quan.

Trả lời

70

UI có thể sử dụng xuất hiện kiểm soát, nơi tốt nhất để thêm nó là trong ứng dụng của đại biểu, didFinishLaunchingWithOptions phương pháp, sử dụng này nếu bạn muốn thiết lập các thuộc tính giống nhau đến từng UISegmentedControls trong dự án của bạn chỉ một lần:

let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName) 
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal) 

nhưng nếu bạn đang đi để thiết lập các thuộc tính để chỉ một UISegmentedControl hoặc nếu bạn muốn thay đổi nó thường xuyên hơn căn cứ vào một số sử dụng điều kiện này, UISegmentedControl phương pháp:

func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?, 
        forState state: UIControlState) 

Ví dụ:

let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName) 
seg.setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal) 
+0

Tôi có thể chỉnh sửa kích thước UISegmentedControl quá không (i có nghĩa là chiều rộng và chiều cao) theo lập trình trong ủy nhiệm ứng dụng? Nếu có thể, làm thế nào để làm điều đó? – user1858725

+0

Tôi khuyên bạn nên sử dụng bố cục tự động và thêm NSLayoutConstraint vào mỗi SegmentedControl – dulgan

+0

+1 để đề xuất proxy UIAppearance, -1 để được đề xuất chèn vào đại biểu ứng dụng. Thời gian là quan trọng, để làm điều đó trước khi tải bất kỳ chế độ xem nào khác, vì vậy cần thực hiện trước khi kết thúc '-applicationDidFinishLaunching:', nhưng kiến ​​trúc tốt hơn là chuyển tất cả logic trình bày của bạn sang một lớp khác Phong cách. – Tim

13

Câu trả lời này là ngày, nhưng đối với những người đang tìm kiếm một giải pháp trong nhanh chóng bạn có thể muốn thử phương pháp này:

func stylizeFonts(){ 
    let normalFont = UIFont(name: "Helvetica", size: 16.0) 
    let boldFont = UIFont(name: "Helvetica-Bold", size: 16.0) 

    let normalTextAttributes: [NSObject : AnyObject] = [ 
     NSForegroundColorAttributeName: UIColor.blackColor(), 
     NSFontAttributeName: normalFont! 
    ] 

    let boldTextAttributes: [NSObject : AnyObject] = [ 
     NSForegroundColorAttributeName : UIColor.whiteColor(), 
     NSFontAttributeName : boldFont!, 
    ] 

    self.setTitleTextAttributes(normalTextAttributes, forState: .Normal) 
    self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted) 
    self.setTitleTextAttributes(boldTextAttributes, forState: .Selected) 
} 

Hãy chắc chắn để thêm stylizeFonts() trong viewDidLoad của bạn hoặc là một chức năng riêng biệt nếu bạn đang phân lớp.

+0

FYI, bạn có thêm dấu phẩy để cho boldTextAttributes sau boldFont! – Ben

3

Bằng cách mở rộng cách tiếp cận Swift tuyệt vời của mvien. Tôi đã tạo một phần mở rộng SegmentedControl:

extension UISegmentedControl { 

    func setFontSize(fontSize: CGFloat) { 

     let normalTextAttributes: [NSObject : AnyObject] = [ 
      NSForegroundColorAttributeName: UIColor.blackColor(), 
      NSFontAttributeName: UIFont.systemFontOfSize(fontSize, weight: UIFontWeightRegular) 
     ] 

     let boldTextAttributes: [NSObject : AnyObject] = [ 
      NSForegroundColorAttributeName : UIColor.whiteColor(), 
      NSFontAttributeName : UIFont.systemFontOfSize(fontSize, weight: UIFontWeightMedium), 
     ] 

     self.setTitleTextAttributes(normalTextAttributes, forState: .Normal) 
     self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted) 
     self.setTitleTextAttributes(boldTextAttributes, forState: .Selected) 
    } 
} 

Chỉ cần thêm mã mở rộng trên để dự án của bạn sau đó sử dụng như thế này:

yourSegmentedControl.setFontSize(20) 
5

Swift 2.0

override func viewDidLayoutSubviews() { 
     let attributedSegmentFont = NSDictionary(object: UIFont(name: "Roboto-Regular", size: 14.0)!, forKey: NSFontAttributeName) 

    dashBoardSegment.setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal) 
    } 

Thay đổi cho tất cả các điều khiển phân đoạn, sử dụng:

UISegmentedControl.appearance().setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal) 

Sử dụng Swift mở rộng:

extension UISegmentedControl{ 
    func changeTitleFont(newFontName:String?, newFontSize:CGFloat?){ 
     let attributedSegmentFont = NSDictionary(object: UIFont(name: newFontName!, size: newFontSize!)!, forKey: NSFontAttributeName) 
     setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal) 
    } 
} 

mở rộng Thực hiện:

override func viewDidLayoutSubviews() { 
    dashBoardSegment.changeTitleFont("Roboto-Regular", newFontSize: 14.0) 
} 
10

Greg 's câu trả lời được cập nhật cho Swift3:

let attr = NSDictionary(object: UIFont(name: "OpenSans", size: 12.0)!, forKey: NSFontAttributeName as NSCopying) 
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal) 
-2

Đối nhanh chóng 3

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: .selected) 
4

Swift3

override func viewDidLayoutSubviews() { 
    let attr = NSDictionary(object: UIFont(name: "Chalkduster", size: 14.0)!, forKey: NSFontAttributeName as NSCopying) 
    segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal) 
} 
2

Đối Swift 4

let font: [AnyHashable : Any] = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)] 
    segmentedControl.setTitleTextAttributes(font, for: .normal) 
Các vấn đề liên quan