NSTextAttachment图像对齐

时间:2014-11-20 18:24:00

标签: ios image sdk uitextview nstextattachment

我正在关注@Duncan Groenewald的这篇很酷的教程Implementing Rich Text with Images on OS X and iOS,并能够在UITextView中显示图像。但是,这些图像并没有按照我希望的方式居中。见图片

NSTextAttachment sample image

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

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

我还尝试为NSKernAttributeName归因字符串设置NSTextAttachment。但它所做的只是隐藏图像。

3 个答案:

答案 0 :(得分:5)

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

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

    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)
        }
    }

答案 1 :(得分:2)

这是 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) 

答案 2 :(得分: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;