2014-01-23 43 views
0

我有我試圖定義一個UISegmentedControl運作良好,我有兩個問題:UISegmentedControl setTitleTextAttributes:forState:不使用字體

這是我的代碼:

NSDictionary *attributes = @{UITextAttributeFont: [UIFont fontWithName:@"Gotham-Book" size:14.f], 
           UITextAttributeTextColor: [UIColor whiteColor], 
           UITextAttributeTextShadowColor: [UIColor clearColor]}; 
    [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 

    NSDictionary *selectedAttributes = @{UITextAttributeFont: [UIFont fontWithName:@"Gotham-Medium" size:14.f], 
            UITextAttributeTextColor: [UIColor redColor]}; 
    [segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected]; 

1 )UISegmentedControl對UIControlStateNormal反應良好,但對於forState:UIControlStateSelected,顏色有效,但不是字體。僅供參考,字體存在,因爲如果我爲UIControlStateNormal切換「Gotham-Book」和「Gotham-Medium」,我會看到不同之處。

2)在爲UIControlStateNormal設置字體之前,文本是垂直對齊的,但是現在它不再是了。我怎樣才能改變textInsets?

感謝您的幫助!

回答

-1

在IOS 7使用NSFontAttributeName,NSForegroundColorAttributeName和NSShadowAttributeName(如果需要),

爲了向後兼容我使用兩種變體,如:

NSDictionary *attributes = @{UITextAttributeFont: [UIFont fontWithName:@"Gotham-Book" size:14.f], 
          UITextAttributeTextColor: [UIColor whiteColor], 
          UITextAttributeTextShadowColor: [UIColor clearColor], 
          NSFontAttributeName: [UIFont fontWithName:@"Gotham-Book" size:14.f], 
          NSForegroundColorAttributeName: [UIColor whiteColor], 
}; 
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
相關問題