您的位置:首页 > 产品设计 > UI/UE

UILabel attributedText的一个坑

2016-07-08 16:44 357 查看
写一个自动计算高度控制行间距的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 alloc] initWithString:text];

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

    [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

上面博主真心大爱。。。。。崇拜 啥时候我能赶上那么一点点呢 ~_~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: