Directly call the system to write it up a bit more trouble, encapsulate it
Because to be simple, write the class method
Wjattributestyle base class
#import<Foundation/Foundation.h>
#import<UIKit/UIKit.h>
/**
* Base class Rich Text
*/
@interfaceWjattributestyle:nsobject
@property (Nonatomic,strong) NSString *attributename;
@property (Nonatomic,strong)IDValue
@property (nonatomic,assign) Nsrange range;
+ (Wjattributestyle *) AttributeName: (NSString *) AttributeName Value: (ID) Value range: (nsrange) range;
@end#import"WJAttributeStyle.h"
@implementationWjattributestyle
+ (Wjattributestyle *) AttributeName: (NSString *) AttributeName Value: (ID) Value range: (Nsrange) Range {
Wjattributestyle *style = [selfclass]New];
Style.attributename = AttributeName;
Style.value = value;
Style.range = range;
returnStyle
}
@end
Derived, inherited from the above base class package color, and size will be easier to write.
Color:
#import "WJAttributeStyle.h"
#import"WJForeGroundColorStyle.h"
/**
* Color Rich Text
*/
@interfaceWjforegroundcolorstyle:wjattributestyle
+ (Wjforegroundcolorstyle *) Withcolor: (Uicolor *) color range: (nsrange) range;
@end#import"WJForeGroundColorStyle.h"
@implementationWjforegroundcolorstyle
+ (Wjforegroundcolorstyle *) Withcolor: (Uicolor *) color range: (nsrange) Range {
Wjforegroundcolorstyle *style =
(Wjforegroundcolorstyle *) [Wjforegroundcolorstyle attributename:nsforegroundcolorattributename Value:color Range:range];
returnStyle
}
@end
Size:
#import"WJAttributeStyle.h"
/**
* Size Rich Text
*/
@interfaceWjfontstyle:wjattributestyle
+ (Wjfontstyle *) Withfonte: (Uifont *) font range: (nsrange) range;
@end#import"WJFontStyle.h"
@implementationWjfontstyle
+ (Wjfontstyle *) Withfonte: (Uifont *) font range: (nsrange) Range {
Wjfontstyle *style =
(Wjfontstyle *) [Wjfontstyle attributename:nsfontattributename Value:font Range:range];
returnStyle
}
@end
Then use a class to set the property text for the string
#import<Foundation/Foundation.h>
#import"WJAttributeStyle.h"
#import"WJForeGroundColorStyle.h"
#import"WJFontStyle.h"
@interfaceNSString (Wjattributestyle)
/**
* Create rich Text based on the styles array
*
* An array of @param styles Wjattributestyle objects
*
* @return Rich Text
*/
-(Nsattributedstring *) Createattributestringwithstyles: (Nsarray *) styles;
@end#import"nsstring+wjattributestyle.h"
@implementationNSString (Wjattributestyle)
-(Nsattributedstring *) Createattributestringwithstyles: (Nsarray *) Styles {
if(Self.length <=0) {
returnNil
}
nsmutableattributedstring *attributestring = [[nsmutableattributedstring alloc]initwithstring:self];
for(inti =0; i < Styles.count; i + +) {
Wjattributestyle *style = styles[i];
[AttributeString AddAttribute:style.attributeName
Value:style.value
Range:style.range];
}
returnAttributeString;
}
@end
Use:
NSString *string=@"This is a test";
_label.attributedtext = [stringCreateattributestringwithstyles:
@[[wjattributestyle Attributename:nsforegroundcolorattributename
Value:[uicolor Redcolor]
Range:nsmakerange (0,1)],
[Wjforegroundcolorstyle Withcolor:[uicolor Graycolor] Range:nsmakerange (1,1)],
[Wjfontstyle Withfonte:[uifont Systemfontofsize: -] Range:nsmakerange (2,1)]
]];
Extended Uikit:https://github.com/youxianming/booktextview
Open source Rich Text: Https://github.com/nicolasgoutaland/GONMarkupParser
Picture and text mixed row: Https://github.com/12207480/TYAttributedLabel
iOS Rich Text-nsattributedstring simple package