字符串的编辑

/* 前字符串 后图标 拼接 */

  • (NSAttributedString *)appendAttributedStringWithFrontTitle:(NSString *)title withBehindIcon:(NSString *)icon withTextColor:(UIColor *)textColor withTextFont:(UIFont *)textFont
    {
    NSMutableAttributedString *mutableAttr = [NSMutableAttributedString new];

    NSTextAttachment* textAttachment = [NSTextAttachment new];
    textAttachment.image = [UIImage imageNamed:icon];

    [mutableAttr appendAttributedString:[[NSAttributedString alloc] initWithString:title]];
    [mutableAttr addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, title.length)];
    [mutableAttr addAttribute:NSFontAttributeName value:textFont range:NSMakeRange(0, title.length)];
    [mutableAttr appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [mutableAttr appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];

    return mutableAttr;
    }

你可能感兴趣的:(字符串的编辑)