2012-04-18 70 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) 
相关问题