iOS Rich Text-nsattributedstring simple package

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.