探索@Harry 的想法,这里有一些想法:
Category on NSAttributedString
、 category onUILabel
或 category on NSDictionary
,也可能是它们的混合,根据哪个最适合您和您的项目。如果您想将自定义用于其他类型的对象(例如 a ) ,则NSAttributedString
在优先级上使用类别可能会更有趣。UILabel
NSAttributedString
UITextView
一个好的开始:
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];
}