相互转换,设置行高行间距等,计算文本宽度和高度

NSMutableAttributedString和NSString 的相互转换

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];

这里可以设置行高行间距等

//两行数据测试

//        NSString *str = @"哈佛交流活动空间和罚款决定是否看见啊上岛咖啡的回复大会费德勒尽快恢复冷静啊好打发";

//        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;


这里两个封装的方法可以计算文本宽度和高度

- (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;

}


//根据富文本和要求的她的宽度或者高度设置高度和宽度的自适应

+ (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;

}

提供了几种方法可以快速判断字符串,数组,集合等是否为空

- (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];

}

/* 获取对象的所有属性和属性内容 */

- (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;

}

/* 获取对象的所有方法 */

你可能感兴趣的:(相互转换,设置行高行间距等,计算文本宽度和高度)