首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTextAttachment图像对齐

NSTextAttachment图像对齐
EN

Stack Overflow用户
提问于 2014-11-21 02:24:00
回答 3查看 6.9K关注 0票数 9

我正在遵循@Duncan Groenewald的这个很酷的教程Implementing Rich Text with Images on OS X and iOS,并且能够在我的UITextView中显示图像。然而,这些图像并不像我希望的那样居中。请参见图像

如你所见,我希望我的图像在X轴上居中。

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

我还尝试为NSTextAttachment属性字符串设置NSKernAttributeName。但它所做的一切只是隐藏了一些图像。

EN

回答 3

Stack Overflow用户

发布于 2015-05-11 03:42:29

尝试在附件中设置居中对齐的段落样式。

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

代码语言:javascript
复制
    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)
        }
    }
票数 6
EN

Stack Overflow用户

发布于 2017-07-19 16:12:02

这是使用扩展的Swift 3.1

代码语言:javascript
复制
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)
            }
        }
    }

}

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

代码语言:javascript
复制
let attributeString = NSMutableAttributedString(string: "")
// add attachments
attributeString.setAttachmentsAlignment(.center) 
票数 5
EN

Stack Overflow用户

发布于 2017-03-01 23:42:31

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

代码语言:javascript
复制
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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27046797

复制
相关文章

相似问题

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