首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTextAttachment周围的间距

NSTextAttachment周围的间距
EN

Stack Overflow用户
提问于 2017-01-06 16:18:30
回答 4查看 2.4K关注 0票数 12

如何在NSTextAttachments周围设置间距,如下例所示?

在本例中,当我向NSAttributedString追加NSTextAttachment时,No spacing是我得到的默认行为。

EN

回答 4

Stack Overflow用户

发布于 2020-07-25 00:15:34

这对我在Swift很管用

代码语言:javascript
复制
public extension NSMutableAttributedString {    
    func appendSpacing( points : Float ){
        // zeroWidthSpace is 200B
        let spacing = NSAttributedString(string: "\u{200B}", attributes:[ NSAttributedString.Key.kern: points])
        append(spacing)
    }
}
票数 6
EN

Stack Overflow用户

发布于 2021-09-09 14:11:33

上面的答案在iOS 15下不再适用,所以我最终在图像附件和属性文本之间创建并添加了一个空的“填充”附件

代码语言:javascript
复制
let padding = NSTextAttachment()
//Use a height of 0 and width of the padding you want
padding.bounds = CGRect(width: 5, height: 0) 
            
let attachment = NSTextAttachment(image: image)
let attachString = NSAttributedString(attachment: attachment)

//Insert the padding at the front
myAttributedString.insert(NSAttributedString(attachment: padding), at: 0)

//Insert the image before the padding
myAttributedString.insert(attachString, at: 0)
票数 2
EN

Stack Overflow用户

发布于 2019-05-30 14:28:44

代码语言:javascript
复制
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
// append NSTextAttachments instance to attributedText...
// then add non-printable string with NSKernAttributeName attributes
unichar c[] = { NSAttachmentCharacter };
NSString *nonprintableString = [NSString stringWithCharacters:c length:1];
NSAttributedString *spacing = [[NSAttributedString alloc] initWithString:nonprintableString attributes:@{
    NSKernAttributeName : @(4) // spacing in points
}];
[attributedText appendAttributedString:spacing];

// finally add other text...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41501623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档