9

我正在关注@Duncan Groenewald 编写的在 OS X 和 iOS 上使用图像实现富文本的UITextView酷教程,并且能够在我的. 然而,这些图像并没有像我希望的那样居中。看图片

NSTextAttachment 示例图片

如您所见,我希望我的图像以 X 轴为中心。

我尝试rect用适当的值返回,-attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex但这没有帮助。

我还尝试NSKernAttributeNameNSTextAttachment属性字符串设置。但它所做的只是以某种方式隐藏图像。

4

3 回答 3

6

尝试使用居中对齐方式设置附件上的段落样式。

如果您的图像作为附件嵌入到属性字符串中,您可以通过枚举属性字符串的附件属性来访问它们。例如:

    attributedContent.enumerateAttribute(NSAttachmentAttributeName, inRange: NSRange(location: 0, length: attributedContent.length), options: nil) { (attribute, range, stop) -> Void in
        if let attachment = attribute as? NSTextAttachment {

            // this example assumes you want to center all attachments. You can provide additional logic here. For example, check for attachment.image.

            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .Center
            attributedContent.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
        }
    }
于 2015-05-10T19:42:29.177 回答
5

这是使用扩展的Swift 3.1

extension NSMutableAttributedString {

    func setAttachmentsAlignment(_ alignment: NSTextAlignment) {
        self.enumerateAttribute(NSAttachmentAttributeName, in: NSRange(location: 0, length: self.length), options: .longestEffectiveRangeNotRequired) { (attribute, range, stop) -> Void in
            if attribute is NSTextAttachment {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.alignment = alignment
                self.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
            }
        }
    }

}

通过这种方式,您可以轻松地为属性字符串上的附件应用对齐方式:

let attributeString = NSMutableAttributedString(string: "")
// add attachments
attributeString.setAttachmentsAlignment(.center) 
于 2017-07-19T08:12:01.677 回答
1

这是另一种设置 NSTextAttachment 图像对齐方式的方法。希望这也能帮助那些为此苦苦挣扎的人。我在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell中使用下面的代码

var buttonText = "My Button";

let align = NSMutableParagraphStyle();
align.alignment = NSTextAlignment.center;
align.firstLineHeadIndent = 10.0;
align.headIndent = 10.0;
align.tailIndent = -10.0;

let para = NSMutableAttributedString();

// top padding
para.append(NSAttributedString(string: "\n", attributes: [NSParagraphStyleAttributeName: align, NSFontAttributeName: UIFont(name: "Helvetica", size: 10.0)!, NSForegroundColorAttributeName: UIColor.white]));

// image
let img = NSTextAttachment();
img.image = UIImage(named: "MyIcon");
img.bounds = CGRect(x: 0, y: UIFont(name: "Helvetica", size: 18.0)!.descender, width: img.image!.size.width, height: img.image!.size.height);
let nas = NSAttributedString(attachment: img).mutableCopy() as! NSMutableAttributedString;
nas.addAttribute(NSParagraphStyleAttributeName, value: align, range: NSRange(location: 0, length: nas.length));
para.append(nas);

// space to text
buttonText = " " + buttonText;

// text
para.append(NSAttributedString(
    string: buttonText,
    attributes: [NSParagraphStyleAttributeName: align, NSFontAttributeName: UIFont(name: "Helvetica", size: 18.0)!, NSForegroundColorAttributeName: UIColor.black]));

// bottom padding
para.append(NSAttributedString(string: "\n", attributes: [NSParagraphStyleAttributeName: align, NSFontAttributeName: UIFont(name: "Helvetica", size: 10.0)!, NSForegroundColorAttributeName: UIColor.white]));

// set cell label
let label = cell.textLabel!;
label.numberOfLines = 0;
label.layer.borderWidth = 0;
label.layer.masksToBounds = false;
label.backgroundColor = UIColor.clear;
label.layer.backgroundColor = UIColor.green;
label.attributedText = para;
于 2017-03-01T15:42:30.717 回答