iOS-UILabel充分利用NSAttributedStrin

2018-08-22  本文已影响54人  Freedom_fly

1、同一块文本中设置不同字体、颜色,及换行

代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *text = @"百家姓:\n赵钱孙李,周吴郑王。\n冯陈褚卫,蒋沈韩杨。\n朱秦尤许,何吕施张。\n孔曹严华,金魏陶姜。\n戚谢邹喻,柏水窦章。\n云苏潘葛,奚范彭郎。\n鲁韦昌马,苗凤花方。\n俞任袁柳,酆鲍史唐。\n费廉岑薛,雷贺倪汤。\n滕殷罗毕,郝邬安常。";
    CGFloat labelHeight = [self heightOfLabelWithText:text font:[UIFont systemFontOfSize:12.0] width:self.view.frame.size.width-20];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.view.frame.size.width-20, labelHeight)];
    label.font = [UIFont systemFontOfSize:12.0];
    label.textColor = [UIColor darkGrayColor];
    label.attributedText = [self configLabelTextWithText:text label:label boundaryLength:4];
    [self.view addSubview:label];
}

#pragma mark - 计算高度
- (CGFloat)heightOfLabelWithText:(NSString *)text font:(UIFont *)font width:(CGFloat)width{
    UILabel *testLabel = [[UILabel alloc] init];
    testLabel.font = font;
    CGRect oldFrame = testLabel.frame;
    oldFrame.size.width = width;
    [testLabel setFrame:oldFrame];
    testLabel.attributedText = [self configLabelTextWithText:text label:testLabel boundaryLength:4];
    [testLabel sizeToFit];
    return testLabel.frame.size.height;
}

#pragma mark - 设置具体文本属性
- (NSAttributedString *)configLabelTextWithText:(NSString *)labelText label:(UILabel *)label boundaryLength:(NSInteger)boundaryLenght{
    NSString  *text = labelText; //[NSString stringWithFormat:@"%@",[labelText stringByReplacingOccurrencesOfString:@"\\n" withString:@"\r\n" ]];
    label.numberOfLines = 0;
    NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedText setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName:[[UIColor orangeColor] colorWithAlphaComponent:0.5]} range:NSMakeRange(0, boundaryLenght)];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:(8.0-(label.font.lineHeight - label.font.pointSize))];
    [paragraphStyle setLineBreakMode:label.lineBreakMode];
    [paragraphStyle setAlignment:label.textAlignment];
    [attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
    return attributedText;
}

代码实现效果:


image.png

2、文本中添加图片

只修改configLabelTextWithText方法,代码如下:

- (NSAttributedString *)configLabelTextWithText:(NSString *)labelText label:(UILabel *)label boundaryLength:(NSInteger)boundaryLenght{
    NSString  *text = labelText; // [NSString stringWithFormat:@"%@",[labelText stringByReplacingOccurrencesOfString:@"\\n" withString:@"\r\n" ]];
    label.numberOfLines = 0;
    NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedText setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0],NSForegroundColorAttributeName:[[UIColor orangeColor] colorWithAlphaComponent:0.5]} range:NSMakeRange(0, boundaryLenght)];
    
    //创建NSTextAttachment
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"NaturePatterns02.jpg"];
    attachment.bounds = CGRectMake(0, (label.font.capHeight-10)/2.0, 16, 10);
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:(8.0-(label.font.lineHeight - label.font.pointSize))];
    [paragraphStyle setLineBreakMode:label.lineBreakMode];
    [paragraphStyle setAlignment:label.textAlignment];
    [attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
    [attributedText insertAttributedString:attachmentString atIndex:boundaryLenght+4];
    [attributedText insertAttributedString:attachmentString atIndex:text.length-20];
    return attributedText;
}

代码运行效果:


image.png

3、部分代码解释:

上一篇下一篇

猜你喜欢

热点阅读