/**
* 根据指定文本和字体计算尺寸
*/
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font {
NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
attrDict[NSFontAttributeName] = font;
return [text sizeWithAttributes:attrDict];
}
/**
* 修改 字体颜色
* .attributedText = str
*/
- (NSMutableAttributedString *)addStr:(NSString *)strrrrrr
addrangeOfStr:(NSString *)ofstr
addcolor:(UIColor *)color
addfont:(NSInteger)font {
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", strrrrrr]];
//获取要调整颜色的文字位置,调整颜色
NSRange range2 = [strrrrrr rangeOfString:ofstr];//匹配得到的下标
[str addAttribute:NSForegroundColorAttributeName value:color range:range2];
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:range2];
return str;
}
#pragma mark 获得高度
/**
*获得高度
*/
- (float)getContactHeight:(NSString *)contact addTitleFontOfSize:(NSInteger)FontOfSize addDeviceWidth:(NSInteger)DeviceWidth {
NSDictionary *attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:FontOfSize]};
CGSize maxSize = CGSizeMake(DeviceWidth, MAXFLOAT);
// 计算文字占据的高度
CGSize size = [contact boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
return size.height;
}
网友评论