更改 UIControl 的 NSAttributedString 文本值
Changing NSAttributedString text value for a UIControl
目前我所有的按钮和文本字段都有 attributedText
值定义为属性字符串。
考虑一个简单的案例UILabel
。每当我必须更改此 UILabel
的文本(基于某些用户操作)时,我必须重新定义 NSAttributedString
上的属性。一种方法是简单地创建一个子例程,在我需要时生成这些属性,但这是一个问题,因为可能有许多不同的标签(或属性字符串)需要这种方便的方法。
另一种可能是简单地更改 text
字段并让观察者添加这些属性,但这是相同的工作量,现在可能更复杂。
有没有不用重新定义属性就能实现上述目标的简单方法?
探索@Harry 的想法,这里有一些想法:
NSAttributedString
类别、UILabel
类别或 NSDictionary
类别,也可能是它们的混合,具体取决于哪一个最适合您和您的项目。
如果您想将自定义 NSAttributedString
用于其他类型的对象(如 UITextView
),则在 UILabel
之前优先使用 NSAttributedString
上的类别可能会更有趣。 =24=]
一个好的开始:
typedef enum : NSUInteger {
AttributeStyle1,
AttributeStyle2,
} AttributeStyles;
NSDictionary
上可能的分类方法:
-(NSDictionary *)attributesForStyle:(AttributeStyles)style
{
NSDictionary *attributes;
switch(style)
{
case AttributeStyle1:
attributes = @{}//Set it
break;
case AttributeStyle2:
attributes = @{}//Set it
break;
default:
attributes = @{}//Set it
break;
}
return attributes;
}
UILabel
可能的类别:
-(void)setString:(NSString *)string withAttributes:(NSDictionary *)attributes
{
[self setAttributedText:[[NSAttributedString alloc] initWithString:string attributes:attributes];
}
NSAttributedString
可能的类别:
-(NSAttributedString *)initWithString:(NSString *)string withStyle:(AttributedStyles)style
{
//Here, a mix is possible using the first method, or doing here the switch case
//Ex: return [[NSAttributedString alloc] initWithString:string attributes:[NSDictionary attributesForStyle:style];
//And to use like this: [yourLabel setAttributedText:[[NSAttributedString alloc] initWithString:string withStyle:AttributeStyle1];
}
目前我所有的按钮和文本字段都有 attributedText
值定义为属性字符串。
考虑一个简单的案例UILabel
。每当我必须更改此 UILabel
的文本(基于某些用户操作)时,我必须重新定义 NSAttributedString
上的属性。一种方法是简单地创建一个子例程,在我需要时生成这些属性,但这是一个问题,因为可能有许多不同的标签(或属性字符串)需要这种方便的方法。
另一种可能是简单地更改 text
字段并让观察者添加这些属性,但这是相同的工作量,现在可能更复杂。
有没有不用重新定义属性就能实现上述目标的简单方法?
探索@Harry 的想法,这里有一些想法:
NSAttributedString
类别、UILabel
类别或 NSDictionary
类别,也可能是它们的混合,具体取决于哪一个最适合您和您的项目。
如果您想将自定义 NSAttributedString
用于其他类型的对象(如 UITextView
),则在 UILabel
之前优先使用 NSAttributedString
上的类别可能会更有趣。 =24=]
一个好的开始:
typedef enum : NSUInteger {
AttributeStyle1,
AttributeStyle2,
} AttributeStyles;
NSDictionary
上可能的分类方法:
-(NSDictionary *)attributesForStyle:(AttributeStyles)style
{
NSDictionary *attributes;
switch(style)
{
case AttributeStyle1:
attributes = @{}//Set it
break;
case AttributeStyle2:
attributes = @{}//Set it
break;
default:
attributes = @{}//Set it
break;
}
return attributes;
}
UILabel
可能的类别:
-(void)setString:(NSString *)string withAttributes:(NSDictionary *)attributes
{
[self setAttributedText:[[NSAttributedString alloc] initWithString:string attributes:attributes];
}
NSAttributedString
可能的类别:
-(NSAttributedString *)initWithString:(NSString *)string withStyle:(AttributedStyles)style
{
//Here, a mix is possible using the first method, or doing here the switch case
//Ex: return [[NSAttributedString alloc] initWithString:string attributes:[NSDictionary attributesForStyle:style];
//And to use like this: [yourLabel setAttributedText:[[NSAttributedString alloc] initWithString:string withStyle:AttributeStyle1];
}