如何爲不同的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