我正在尝试使用NSTextAttachment在UITextField中显示图像,但我想在图像和文本之间留出一些水平空间。但是,当按如下方式将NSKernAttributeName属性添加到属性化字符串时,它会将附件的高度重置为与周围文本相同的高度。
var str = NSMutableAttributedString(attributedString: NSAttributedString(attachment: imageAttachment))
str.addAttribute(NSKernAttributeName, value: 10, range: NSRange(location: 0,length: 1))有没有其他方法可以在图像和文本之间添加水平空间?
发布于 2015-06-03 11:07:21
最直接的方法是在字符串开头设置几个空格:
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];https://stackoverflow.com/questions/30608726
复制相似问题