NSAttributedString 属性

/** NSAttributedString 属性字符串,可以用于图文混排,尤其在QQ聊天界面,使用最为广泛

 */

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component

{// 1. 获取到数据模型

    CZFlag *flag = self.flags[row];

       // 2. 实例化文本附件

    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];

    // 2.1 设置文本附件的图片

    attachment.image = [UIImage imageNamed:flag.icon];

    // 2.2 设置文本附件的大小

    attachment.bounds = CGRectMake(0, 0, 160, 44);

    // 3. 使用文本附件创建属性字符串

    NSAttributedString *myString = [NSAttributedString attributedStringWithAttachment:attachment];

    return myString;

}

原文地址:https://www.cnblogs.com/bluceZ/p/4629378.html