2014-11-20 523 views
6

我正在關注@Duncan Groenewald的這個很酷的教程Implementing Rich Text with Images on OS X and iOS,並且能夠在我的UITextView中顯示圖像。但是,這些圖像並非以我希望它們的方式居中。請查看圖片NSTextAttachment圖像對齊

NSTextAttachment sample image

正如你可以看到,我想在X軸爲中心我的形象。

我試着用-attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex中的適當值返回rect,但這並沒有幫助。

我也試着爲NSTextAttachment屬性字符串設置NSKernAttributeName。但它所做的只是隱藏了一些圖像。

回答

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

謝謝!這工作就像一個魅力! – Vik 2015-11-24 15:12:12

0

這裏的另一種方式如何設置對齊方式的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; 
1

這是夫特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)