我正在尝试使用 NSTextAttachment
在 UITextField
中显示图像,但我希望图像和文本之间有一些水平空间。但是,当如下所示将 NSKernAttributeName
属性添加到属性字符串时,它会将附件的高度重置为与周围文本相同的高度。
var str = NSMutableAttributedString(attributedString: NSAttributedString(attachment: imageAttachment))
str.addAttribute(NSKernAttributeName, value: 10, range: NSRange(location: 0,length: 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];
关于ios - NSTextAttachment 的水平间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30608726/