iOS-UILabel充分利用NSAttributedString

weixin_33895657發表於2018-08-22

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;
}

程式碼實現效果:


1787970-500103ba478a9f65.png
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;
}

程式碼執行效果:


1787970-c2fbbfaf184804d5.png
image.png

3、部分程式碼解釋:

  • 1> NSTextAttachment-bounds屬性
    label新增圖片中,attachment.bounds屬性,寬高不用多說,至於縱座標y值,如果設定為0,圖片不一定會和文字對齊,會有一定的偏移,那麼如何設定y值呢?
    請點選這裡檢視答案
    你會看到有個人這麼說:

    1787970-cdec8c9b508ff758.png
    image.png

    沒錯就是採用:(label.font.capHeight-image.size.height)/2.0

  • 2> NSMutableParagraphStyle-lineSpacing屬性
    lineSpacing屬性要想符合UI的要求請參考我之前的文章->iOS-文字行高

相關文章