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<LWidth(20)) {
        return LWidth(20);
    }else{
        return 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];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值