NSTextAttachment周围的间距

时间:2017-01-06 08:18:30

标签: swift3 uitextview nsattributedstring nstextattachment

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

在示例中,“无间距”是我将NSTextAttachment附加到NSAttributedString时的默认行为。

enter image description here

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这在Swift中对我有用

public extension NSMutableAttributedString {    
    func appendSpacing( points : Float ){
        // zeroWidthSpace is 200B
        let spacing = NSAttributedString(string: "\u{200B}", attributes:[ NSAttributedString.Key.kern: points])
        append(spacing)
    }
}