ios - NSTextAttachment 的水平间距

标签 ios swift nsattributedstring kerning

我正在尝试使用 NSTextAttachmentUITextField 中显示图像,但我希望图像和文本之间有一些水平空间。但是,当如下所示将 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/

相关文章:

iphone - Bundle ID 应该如何用于多个 Apple 应用程序?

ios - 按用户名对 PFObject 数组进行排序

swift - Swift 中的 Realm 迁移

ios - 我可以获取元素在 NSAttributedString 中的位置吗?

ios - 来自 Rails 应用程序的 Apple 推送通知

ios - 如何本地化 iOS info.plist 文件中的字符串?

ios - 带有 Objective-C 函数的 Swift block 语法 [Venmo-iOS-SDK]

swift - 在 Swift 中查找和替换

Swift 3 属性文本解决方案

UITextView 中带有 <blockquote> 标签的 HTML