如何为不同的UIbutton状态设置不同的字体?感谢您的帮助。为UIButton的不同状态设置不同的字体
回答
您可以在更多细节上的设计视图设置字体:
您可以设置这一切在界面生成器本身。除非你有很强的字符串原因才能在代码中完成。以下是如何在IB中执行此操作 -
打开右侧栏&然后单击“状态配置”,您会看到按钮的各种状态,默认,突出显示,已选中&禁用。现在您可以为每种状态设置不同的图像,每种状态的字体颜色都有不同的字体类型&。 Hopr这有助于...
谢谢..!
这是我的工作代码块。 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
@inforeqd请让我知道这个解决方案适用于哟 – Ducky 2015-09-18 05:34:16
接口生成器只需创建自定义按钮。覆盖布局子视图。设置你需要的字体。
// 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
这是一个非常酷的问题,它促使我创建了一个允许设置状态字体的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
最简单的办法是设置一个属性标题为每个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)
- 1. 为不同的设备设置不同的字体大小到UIButton
- 2. setContentEdgeInsets为UIButton的不同状态?
- 3. 如何在UIButton上动态设置不同的字体?
- 4. 不同字体的UIButton textLabel
- 5. 如何为不同的textareas设置不同的字体颜色
- 6. 针对不同按钮状态的不同字体
- 7. 如何为后备字体设置不同的字体重量?
- 8. 为不同状态更改UIButton的渐变
- 9. 如何设置图像按钮backgroundimage为不同的状态?
- 10. 设置为true状态复选框在不同的页面(javascript)
- 11. 如何为WKInterfaceButton的不同状态设置背景图片?
- 12. vim:为分割窗口设置不同的状态行
- 13. 为文本框中的不同行设置不同的字体大小
- 14. 如何为不同的绑定元素设置不同的字体颜色
- 15. 不同身体背景不同的UI,路由器状态
- 16. 如何使用不同形状的UIButton
- 17. 如何为不同的蜘蛛设置不同的scrapy设置?
- 18. 设置的UIButton字体
- 19. 如何在UITableViewCell中为不同的ViewController设置UIButton的IBAction?
- 20. 设置状态同步
- 21. 标题和不同字体大小为的UIButton
- 22. 将多个不同的字体设置为ListView中的TextView
- 23. 不同的图像高亮显示的UIButton状态
- 24. 从不同状态的颜色的UIButton背景图像
- 25. 设置不同状态的控件的属性? - Flex的4
- 26. 如何为Ionic Framework中动态更新的不同屏幕尺寸设置不同的字体大小?
- 27. 设置不同的形状在cytoscape.js不同节点
- 28. 处理不同的状态
- 29. 为iPhone 5和6设置不同的字体大小
- 30. 如何为两列ListView设置两种不同的字体?
我想迪内希是想说的是,你可以用不同的字体创建一个图像,然后设置按钮的形象。 – 2012-04-18 05:21:21
好吧,我现在更新答案..! – Dinesh 2012-04-18 05:23:15
现在iam更新答案,您可以按照图像状态配置更改组合在uibutton中为uibutton不同状态变为字体。 – Dinesh 2012-04-18 05:25:45