2014-11-20 15 views
6

Ich folge diesem coolen Tutorial Implementing Rich Text with Images on OS X and iOS von @Duncan Groenewald und konnte Bilder in meinem UITextView anzeigen. Diese Bilder sind jedoch nicht so zentriert, wie ich es möchte. Siehe BildNSTextAttachment Bildausrichtung

NSTextAttachment sample image

Wie Sie sehen können, würde ich mein Bild auf der X-Achse zentriert werden mag.

Ich habe versucht, die rect mit entsprechenden Werten in -attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex zurückgeben, aber das hat nicht geholfen.

Ich habe auch versucht, die NSKernAttributeName für die NSTextAttachment zugeschriebene Zeichenfolge festlegen. Aber alles was er tat, war das Bild zu verbergen.

Antwort

5

Versuchen Sie, den Absatzstil auf Ihrem Anhang mit einer zentrierten Ausrichtung einzustellen.

Wenn Ihre Bilder in einer attributierten Zeichenfolge als Anlagen eingebettet sind, können Sie auf sie zugreifen, indem Sie die Anlagenattribute der attributierten Zeichenfolge auflisten. Zum Beispiel:

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

Danke! Das hat wie ein Zauber funktioniert! – Vik

0

Hier ist eine andere Art und Weise, wie die Ausrichtung für ein NSTextAttachment Bild zu setzen. Hoffentlich hilft das auch jemandem, der damit zu kämpfen hat. Ich bin den Code unten in einer func Tableview mit (_ 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

Dies ist Swift 3.1 mit Erweiterung:

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

} 

Auf diese Weise können Sie leicht Ausrichtung für Anhänge auf attributierte Zeichenfolge anwenden:

let attributeString = NSMutableAttributedString(string: "") 
// add attachments 
attributeString.setAttachmentsAlignment(.center)