2015-06-03 649 views
1

我试图使用NSTextAttachmentUITextField中显示图像,但我希望图像和文本之间有一定的水平间距。但是,如下所示将NSKernAttributeName属性添加到属性字符串时,会将附件的高度重置为与周围文本相同的高度。NSTextAttachment的水平间距

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

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

回答

0

最直接的办法就是在字符串开头设置一些空间:

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