NSMutableAttributedString和NSString 的相互轉(zhuǎn)換
1到2
NSString *str = @“hello world”戳寸;
NSMutableAttributedString * strAttri = [ NSMutableAttributedString alloc] initWithString :str attributes :nil];
[strAttri setAttributes:@{NSForegroundColorAttributeName:[NSColor redColor]} range:NSMakeRange(0, 5)];
2到1
NSString *anotherString=[attributedString string];
這里可以設(shè)置行高行間距等
//兩行數(shù)據(jù)測(cè)試
//? ? ? ? NSString *str = @"哈佛交流活動(dòng)空間和罰款決定是否看見啊上島咖啡的回復(fù)大會(huì)費(fèi)德勒盡快恢復(fù)冷靜啊好打發(fā)";
//? ? ? ? NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.recommendReason];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//? [paragraphStyle setParagraphSpacing:10];
[paragraphStyle setLineSpacing:3];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [rowNode.recommendReasonText length])];
rowNode.recommendReasonText = attributedString;
這里兩個(gè)封裝的方法可以計(jì)算文本寬度和高度
- (CGSize)sizeWithSystemFontOfSize:(UIFont *)font maxWidth:(CGFloat)width {
CGSize textSize = CGSizeZero;
NSDictionary *attribute = @{NSFontAttributeName:font};
textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return textSize;
}
- (CGSize)sizeWithSystemFontOfSize:(UIFont *)font maxHeight:(CGFloat)hegiht {
CGSize textSize = CGSizeZero;
NSDictionary *attribute = @{NSFontAttributeName:font};
textSize = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, hegiht)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
return textSize;
}
//根據(jù)富文本和要求的她的寬度或者高度設(shè)置高度和寬度的自適應(yīng)
+ (CGFloat)getHeightAttrStr:(NSAttributedString *)attrStr width:(CGFloat)width
{
? ? ? ? if (!attrStr) {
? ? ? ? ? return 0;
}
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
return rect.size.height;
}
+ (CGFloat)getWidthAttrStr:(NSAttributedString *)attrStr
{
if (!attrStr) {
return 0;
}
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 10) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
return rect.size.width;
}
提供了幾種方法可以快速判斷字符串呈驶,數(shù)組,集合等是否為空
- (BOOL)isNumber {
return [self isKindOfClass:[NSNumber class]];
}
- (BOOL)isString {
return [self isKindOfClass:[NSString class]];
}
- (BOOL)isValidString {
return [self isString] && ((NSString *)self).length > 0;
}
- (BOOL)isArray {
return [self isKindOfClass:[NSArray class]];
}
- (BOOL)isValidArray {
return [self isArray] && ((NSArray *)self).count > 0;
}
- (BOOL)isDictionary {
return [self isKindOfClass:[NSDictionary class]];
}
- (BOOL)isValidDictionary {
return [self isDictionary] && ((NSDictionary *)self).count > 0;
}
- (BOOL)isSet {
return [self isKindOfClass:[NSSet class]];
}
- (BOOL)isValidSet {
return [self isSet] && ((NSSet *)self).count > 0;
}
+ (instancetype)object:(NSDictionary *)dictionary {
return [self mj_objectWithKeyValues:dictionary];
}
/* 獲取對(duì)象的所有屬性和屬性內(nèi)容 */
- (NSArray *)getAllProperties
{
u_int count;
objc_property_t *properties? =class_copyPropertyList([self class], &count);
NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
const char* propertyName =property_getName(properties[i]);
[propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
}
free(properties);
return propertiesArray;
}
/* 獲取對(duì)象的所有方法 */