我正在考虑将 UILabel
属性作为 subview 添加到 UIButton
中,以匹配当前存在的 titleLabel
属性的功能,并且为它提供按钮的所有不同状态(UIControlStateNormal
、UIControlStateHighlighted
等)。
我开始尝试将状态存储在 NSDictionary
中,使用状态(包装在 NSNumber
中)作为字典的键,值是文本,文字颜色和阴影颜色。但我不确定这是否会根据各种不同的按钮状态组合正常工作。
到目前为止,我有以下方法:
-(void)setSelectedBOOL)selected
{
super.selected = selected;
[self stateUpdated];
}
-(void)setHighlightedBOOL)highlighted
{
super.highlighted = highlighted;
[self stateUpdated];
}
-(void)setEnabledBOOL)enabled
{
super.enabled = enabled;
[self stateUpdated];
}
-(void)stateUpdated
{
[self updateValueLabel];
}
-(void)setValueTextNSString *)text forStateUIControlState)state
{
NSMutableDictionary *stateValues = self.customStates[@state];
// If we haven't set the state before create a new place to store them
if (!stateValues) {
stateValue = [NSMutableDictionary dictionary];
self.customStates[@state] = stateValues;
}
if (text) {
stateValues[@"text"] = text;
} else {
[stateValues removeObjectForKey"text"];
}
[self updateValueLabel];
}
-(void)setValueTextColorUIColor *)textColor forStateUIControlState)state
{
NSMutableDictionary *stateValues = self.customStates[@state];
// If we haven't set the state before create a new place to store them
if (!stateValues) {
stateValues = [NSMutableDictionary dictionary];
self.customStates[@state] = stateValues;
}
if (text) {
stateValues[@"textColor"] = textColor;
} else {
[stateValues removeObjectForKey"textColor"];
}
[self updateValueLabel];
}
-(void)updateValueLabel
{
NSMutableDictionary *currentStateValues = self.customStates[@self.state];
// Set the new values from the current state
self.valueLabel.text = currentStateValues[@"text"];
self.valueLabel.textColor = currentStateValues[@"textColor"];
}
我将 valueLabel
作为 UIButton
子类的属性。
为不同状态设置的不同属性似乎没有生效。如果我使用不同的位掩码手动设置它们就可以了,但我想模拟默认的回退状态,就像 UIButton
对其属性所做的那样。
所以不必手动设置每个:
[button setValueText"Normal" forState:UIControlStateNormal];
[button setValueText"Normal" forState:UIControlStateHighighted];
我希望能够只设置一次标题:
[button setValueText"Normal" forState:UIControlStateNormal];
它显示所有州。
提前致谢!
创建一个 getter valueTextForState:
,就像 Apple 对 titleForState:
所做的那样.该方法返回值的文档如下:
The title for the specified state. If no title has been set for the specific state, this method returns the title associated with the UIControlStateNormal state.
所以,让我们为您的属性(property)也这样做:
- (NSString *)valueTextForStateUIControlState)state
{
NSString *result;
NSDictionary *currentStateValues;
currentStateValues = self.customStates[@(state)];
result = currentStateValues[@"text"];
if (!result && state != UIControlStateNormal) {
// Fall back to normal state.
currentStateValues = self.customStates[@(UIControlStateNormal)];
result = currentStateValues[@"text"];
}
return result;
}
你会为你的颜色做同样的事情。那么您的 updateValueLabel
将如下所示:
-(void)updateValueLabel
{
// Set the new values from the current state
self.valueLabel.text = [self valueTextForState:self.state];
self.valueLabel.textColor = [self valueColorForState:self.state];
}
关于ios - UIButton 如何存储不同的 UI 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864588/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |