UILabel attributedText的一个坑

写一个自动计算高度控制行间距的UILabel  


先写个UILabel

- (UILabel *)resultLbl{

    if (_resultLbl == nil) {

        _resultLbl = [[UILabel alloc] init];

        _resultLbl.backgroundColor =[UIColor whiteColor];

        _resultLbl.numberOfLines = 0;

        _resultLbl.textColor = [UIColor settingRightTitleColor];

        _resultLbl.font = [UIFont systemFontOfSize:RejectResult_resultlbl_fint];

        _resultLbl.textAlignment = NSTextAlignmentCenter;

        

        _resultLbl.frame = CGRectMake(15,0, ScreenWidth - 30, self.resultCellHeight);

    }

    return _resultLbl;

}


用到了富文本的设置

- (NSAttributedString *)setLabelLineSpace:(CGFloat)lineSpace text:(NSString *)text{

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:lineSpace];//调整行间距

  

    return attributedString;

}


然后我就开始计算了 

- (CGFloat)sizeWithFont:(UIFont *)font constrainedTosize:(CGSize)size lineSpace:(CGFloat)lineSpace text:(NSString *)text{

    CGFloat oneRowHeihgt = [@"test" sizeWithAttributes:@{NSFontAttributeName:font}].height;

    CGSize textSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;

    //计算出真实的大小

    CGFloat rows = textSize.height / oneRowHeihgt;

    CGFloat realHeight = (rows * ceilf(oneRowHeihgt)) + (rows - 1)* lineSpace;

    return realHeight;

    

}


然后我发现我怎么设置文本显示出来都不是居中了 竟然是左对齐  我的个去类!!!啥情况,然后开始找原因


原因是 在设置attributedText富文本的时候将忽略下列基础设置

@property(nullable, nonatomic,copy)   NSString           *text;            // default is nil

@property(null_resettable, nonatomic,strong) UIFont      *font;            // default is nil (system font 17 plain)

@property(null_resettable, nonatomic,strong) UIColor     *textColor;       // default is nil (text draws black)

@property(nullable, nonatomic,strong) UIColor            *shadowColor;     // default is nil (no shadow)

@property(nonatomic)        CGSize             shadowOffset;    // default is CGSizeMake(0, -1) -- a top shadow

@property(nonatomic)        NSTextAlignment    textAlignment;   // default is NSTextAlignmentLeft

@property(nonatomic)        NSLineBreakMode    lineBreakMode;   // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text


文档才是硬道理呀 !!!!!


解决办法是 在函数中恢复设置 。。。。。。。。。代码直接改下见谅。。。

- (NSAttributedString *)setLabelLineSpace:(CGFloat)lineSpace text:(NSString *)text{

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString allocinitWithString:text];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle allocinit];

    [paragraphStyle setLineSpacing:lineSpace];//调整行间距

  

  [paragraphStyle setAlignment:NSTextAlignmentCenter];

    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:RejectResult_resultlbl_fint] range:NSMakeRange(0, [text length])];

    [attributedString  addAttribute:NSBackgroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, [text length])];

    [attributedString  addAttribute:NSForegroundColorAttributeName value:[UIColor settingRightTitleColor] range:NSMakeRange(0, [text length])];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];


    return attributedString;

}

//练手记录、

使用方法:

为某一范围内文字设置多个属性

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

为某一范围内文字添加某个属性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

为某一范围内文字添加多个属性

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

移除某范围内的某个属性

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

2.     常见的属性及说明

NSFontAttributeName 字体

NSParagraphStyleAttributeName 段落格式 

NSForegroundColorAttributeName 字体颜色

NSBackgroundColorAttributeName  背景颜色

NSStrikethroughStyleAttributeName删除线格式

NSUnderlineStyleAttributeName     下划线格式

NSStrokeColorAttributeName       删除线颜色

NSStrokeWidthAttributeName删除线宽度

NSShadowAttributeName 阴影

参考资料:http://blog.csdn.net/reylen/article/details/41208747

在送一个很好的总结 :https://segmentfault.com/a/1190000003491677

上面博主真心大爱。。。。。崇拜 啥时候我能赶上那么一点点呢 ~_~




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值