3

我正在尝试在UITextFieldusing中显示图像NSTextAttachment,但我希望图像和文本之间有一些水平空间。但是,当NSKernAttributeName如下将属性添加到属性字符串时,它会将附件的高度重置为与周围文本相同的高度。

var str = NSMutableAttributedString(attributedString: NSAttributedString(attachment: imageAttachment))
str.addAttribute(NSKernAttributeName, value: 10, range: NSRange(location: 0,length: 1))

是否有另一种方法可以在图像和文本之间添加水平空间?

4

1 回答 1

-1

最直接的方法是在字符串开始设置几个空格:

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
[attachment setImage:[UIImage imageNamed:@"dest_poi_content_quotation"]];
NSString *reviewText = [NSString stringWithFormat:@"  %@", review.text];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:reviewText];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedString insertAttributedString:attrStringWithImage atIndex:0];
[self.lblComment setAttributedText:attributedString];
于 2015-06-03T03:07:20.927 回答