iOS 有关字符串宽度,高度处理以及富文本显示问题

1.计算字符高度

+(CGFloat)returnStrHeighWithStr:(NSString *)str withFont:(UIFont*)font withWidth:(float)width{
    //计算字符高度
    NSDictionary *minattributesri = @{NSFontAttributeName:font};
    CGSize mindetailSizeRi = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:minattributesri context:nil].size;
    if (mindetailSizeRi.height

2.计算字符宽度

+(CGFloat)returnBtnWidthWithStr:(NSString *)str withFont:(UIFont*)font withWidth:(float)width{
    //计算字符宽度
    NSDictionary *minattributesri = @{NSFontAttributeName:font};
    CGSize mindetailSizeRi = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesFontLeading attributes:minattributesri context:nil].size;
    return mindetailSizeRi.width+LWidth(14);
    
}

3.设置UILabel的行间距以及字样位置

NSString *labelText = self.titleLabel.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:LWidth(4)];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
self.titleLabel.attributedText = attributedString;
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;

4.设置UILabel的富文本格式

NSString *labelText = self.titleLabel.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];

//设置字体
[attributedString addAttribute:NSFontAttributeName
                value:[UIFont systemFontOfSize:30.0f]
                range:NSMakeRange(6, 3)];

//设置颜色
[attrStr addAttribute:NSForegroundColorAttributeName
                value:[UIColor redColor]
                range:NSMakeRange(4, 3)];

//设置背景颜色
[attrStr addAttribute:NSBackgroundColorAttributeName
                value:[UIColor redColor]
                range:NSMakeRange(4, 3)];

//空心字
[attrStr addAttribute:NSStrokeColorAttributeName
                value:[UIColor redColor]
                range:NSMakeRange(4, 3)];  // 设置空心字描边颜色 要和NSStrokeWidthAttributeName设置描边宽度一起使用
[attrStr addAttribute:NSStrokeWidthAttributeName
                value:@1.5
                range:NSMakeRange(4, 3)]; 

//字间距
[attrStr addAttribute:NSKernAttributeName
                value:@10                    // NSNumber
                range:NSMakeRange(4, 3)];//字间距

//倾斜
[attrStr addAttribute:NSObliquenessAttributeName
                value:@(0.5f)          
                range:NSMakeRange(4, 3)];// 正值向右倾斜 负值向左倾斜

//拉伸
[attrStr addAttribute:NSExpansionAttributeName
                value:@(0.5f)      // 正值横向拉伸 负值横向压缩
                range:NSMakeRange(4, 3)];

//基线偏移
[attrStr addAttribute:NSBaselineOffsetAttributeName
                value:@(10)   // 正值上偏 负值下偏
                range:NSMakeRange(4, 3)];

//阴影
NSShadow *shadow = [[NSShadow alloc] init];  // NSShadow只有3个属性:阴影颜色,模糊半径和偏移
shadow.shadowOffset     = CGSizeMake(3, 3);   // 阴影偏移(X方向偏移和Y方向偏移)
shadow.shadowBlurRadius = 1.5;                // 模糊半径
shadow.shadowColor      = [UIColor redColor]; // 阴影颜色
[attrStr addAttribute:NSShadowAttributeName
                value:shadow
                range:NSMakeRange(4, 3)];

//书写方向
[attrStr addAttribute:NSWritingDirectionAttributeName
                value:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
                range:NSMakeRange(0, 20)];


self.titleLabel.attributedText = attributedString;
self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;

5.图文混排

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"这是一个富文本字符串!"];
// 创建一个文字附件对象
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"20.png"];  //设置图片源
textAttachment.bounds = CGRectMake(0, -6, 30, 30);  //设置图片位置和大小
// 将文字附件转换成属性字符串
NSAttributedString *attachmentAttrStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
// 将转换成属性字符串插入到目标字符串
[attrStr insertAttributedString:attachmentAttrStr atIndex:8];

你可能感兴趣的:(前端,ios,objective-c,开发语言,富文本)