2012-04-18 68 views

回答

-2

您可以在更多細節上的設計視圖設置字體:

您可以設置這一切在界面生成器本身。除非你有很強的字符串原因才能在代碼中完成。以下是如何在IB中執行此操作 -

打開右側欄&然後單擊「狀態配置」,您會看到按鈕的各種狀態,默認,突出顯示,已選中&禁用。現在您可以爲每種狀態設置不同的圖像,每種狀態的字體顏色都有不同的字體類型&。 Hopr這有助於...

enter image description here

謝謝..!

+0

我想迪內希是想說的是,你可以用不同的字體創建一個圖像,然後設置按鈕的形象。 – 2012-04-18 05:21:21

+0

好吧,我現在更新答案..! – Dinesh 2012-04-18 05:23:15

+0

現在iam更新答案,您可以按照圖像狀態配置更改組合在uibutton中爲uibutton不同狀態變爲字體。 – Dinesh 2012-04-18 05:25:45

2

這是我的工作代碼塊。 IB_DESIGNABLE只是一個微小的改進,使在界面生成的結果可視化的:-)

@interface MyButton : UIButton 
@end 

IB_DESIGNABLE @implementation MyButton 
// Here you can override the look & feel for each state 
// Actually not only fontSize, but any writable properties ^_^ 
- (void)setEnabled:(BOOL)enabled { 
    [super setEnabled:enabled]; 
    self.titleLabel.font = enabled ? [UIFont systemFontOfSize:14] : [UIFont systemFontOfSize:10]; 
} 

- (void)setHighlighted:(BOOL)highlighted { 
    [super setHighlighted:highlighted]; 
    self.titleLabel.font = highlighted ? [UIFont systemFontOfSize:14] : [UIFont systemFontOfSize:12]; 
} 

- (void)setSelected:(BOOL)selected { 
    [super setSelected:selected]; 
    self.titleLabel.font = selected ? [UIFont boldSystemFontOfSize:14] : [UIFont systemFontOfSize:12]; 
} 

@end 

你可以看到設計性MyButton的字體體現在這樣 enter image description here

+0

@inforeqd請讓我知道這個解決方案適用於喲 – Ducky 2015-09-18 05:34:16

2

接口生成器只需創建自定義按鈕。覆蓋佈局子視圖。設置你需要的字體。

// Interface 
@interface EezyButton : UIButton 

@end 
//Implementation 
#import "EezyButton.h" 

@implementation EezyButton 

- (void)layoutSubviews{ 
    if (self.state == UIControlStateNormal) { 
     [self.titleLabel setFont:[UIFont systemFontOfSize:12]]; 
    } 
    else if (self.state == UIControlStateHighlighted){ 
     [self.titleLabel setFont:[UIFont systemFontOfSize:25]]; 

    } 
    else if (self.state == UIControlStateDisabled){ 
     [self.titleLabel setFont:[UIFont systemFontOfSize:12]]; 
    } 
    else if (self.state == UIControlStateSelected){ 
     [self.titleLabel setFont:[UIFont systemFontOfSize:28]]; 
    } 
    [super layoutSubviews]; 
} 

@end 
9

這是一個非常酷的問題,它促使我創建了一個允許設置狀態字體的UIButton的子類!

我還寫了一些示例代碼,顯示如何設置字體。如果您正在使用Interface Builder,請將該按鈕的類設置爲ConfigurableButton。在代碼中,該按鈕還必須聲明爲ConfigurableButton,因爲我已經添加了新屬性和一個setFont:forState:方法。

請對任何可以改進的地方留下評論!

- 視圖 - 控制器實現

#import "ViewController.h" 
#import "ConfigurableButton.h" 

@interface ViewController() 

@property (weak, nonatomic) IBOutlet ConfigurableButton *toggleButton; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //Set the fonts for button's states 
    _toggleButton.normalFont  = [UIFont fontWithName:@"BradleyHandITCTT-Bold" size:14]; 
    _toggleButton.highlightedFont = [UIFont fontWithName:@"Chalkduster" size:14]; 
    _toggleButton.selectedFont  = [UIFont fontWithName:@"Zapfino" size:14]; 
    _toggleButton.disabledFont  = [UIFont fontWithName:@"Arial" size:14]; 
} 

@end 

ConfigurableButton.h

#import <UIKit/UIKit.h> 

IB_DESIGNABLE 

/** 
* A button that allows fonts to be assigned to each of the button's states. 
* 
* A state font can be specified using setFont:forState, or through one of the 
* four state Font properties. 
* 
* If a font is not specified for a given state, then 
* the System font will be displayed with a font size of 15. 
*/ 
@interface ConfigurableButton : UIButton 

@property (strong, nonatomic) UIFont *normalFont; 
@property (strong, nonatomic) UIFont *highlightedFont; 
@property (strong, nonatomic) UIFont *selectedFont; 
@property (strong, nonatomic) UIFont *disabledFont; 

/** 
* Set a font for a button state. 
* 
* @param font the font 
* @param state a control state -- can be 
*  UIControlStateNormal 
*  UIControlStateHighlighted 
*  UIControlStateDisabled 
*  UIControlStateSelected 
*/ 
- (void) setFont:(UIFont *)font forState:(NSUInteger)state; 

@end 

ConfigurableButton.m

#import "ConfigurableButton.h" 

@implementation ConfigurableButton 

//Sets one of the font properties, depending on which state was passed 
- (void) setFont:(UIFont *)font forState:(NSUInteger)state 
{ 
    switch (state) { 
     case UIControlStateNormal: 
     { 
      self.normalFont = font; 
      break; 
     } 

     case UIControlStateHighlighted: 
     { 
      self.highlightedFont = font; 
      break; 
     } 

     case UIControlStateDisabled: 
     { 
      self.disabledFont = font; 
      break; 
     } 

     case UIControlStateSelected: 
     { 
      self.selectedFont = font; 
      break; 
     } 

     default: 
     { 
      self.normalFont = font; 
      break; 
     } 
    } 
} 

/** 
* Overrides layoutSubviews in UIView, to set the font for the button's state, 
* before calling [super layoutSubviews]. 
*/ 
- (void) layoutSubviews 
{ 
    NSUInteger state = self.state; 

    switch (state) { 
     case UIControlStateNormal: 
     { 
      [self setTitleFont:_normalFont]; 
      break; 
     } 

     case UIControlStateHighlighted: 
     { 
      [self setTitleFont:_highlightedFont]; 
      break; 
     } 

     case UIControlStateDisabled: 
     { 
      [self setTitleFont:_disabledFont]; 
      break; 
     } 

     case UIControlStateSelected: 
     { 
      [self setTitleFont:_selectedFont]; 
      break; 
     } 

     default: 
     { 
      [self setTitleFont:_normalFont]; 
      break; 
     } 

    } 

    [super layoutSubviews]; 
} 

/** 
* Private 
* 
* Convenience method that falls back to the System font, 
* if no font is configured. 
*/ 
- (void) setTitleFont:(UIFont *)font 
{ 
    if (!font) { 
     font = [UIFont systemFontOfSize:15]; 
    } 

    self.titleLabel.font = font; 
} 

@end 
4

最簡單的辦法是設置一個屬性標題爲每個UIControl狀態:

var attributes = [String : AnyObject]() 
attributes[NSForegroundColorAttributeName] = UIColor.redColor() 
attributes[NSFontAttributeName] = UIFont.systemFontOfSize(15) 

let normal = NSAttributedString(string: "normal", attributes: attributes) 
button.setAttributedTitle(normal, forState: .Normal) 

attributes[NSForegroundColorAttributeName] = UIColor.redColor() 
attributes[NSFontAttributeName] = UIFont.boldSystemFontOfSize(15) 

let selected = NSAttributedString(string: "selected", attributes: attributes) 
button.setAttributedTitle(selected, forState: .Selected) 
相關問題