2015-06-03 101 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];