2014-11-20 15 views
6

Sto seguendo questo interessante tutorial Implementing Rich Text with Images on OS X and iOS da @Duncan Groenewald ed è stato in grado di visualizzare le immagini nel mio UITextView. Tuttavia, queste immagini non sono centrate come vorrei che fossero. Vedi immagineNSTextAttachment allineamento immagine

NSTextAttachment sample image

Come potete vedere, vorrei che la mia immagine per essere centrata sulla X-asse.

Ho provato a restituire il rect con i valori appropriati in -attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex ma ciò non ha aiutato.

Ho anche provato a impostare lo NSKernAttributeName per la stringa attribuita NSTextAttachment. Ma tutto ciò che ha fatto è stato nascondere l'immagine come.

risposta

5

Provare a impostare lo stile di paragrafo sull'allegato con un allineamento del centro.

Se le immagini sono incorporate in una stringa attribuita come allegati, è possibile accedervi eseguendo l'enumerazione tramite gli attributi di allegato della stringa attribuita. Per esempio:

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

Grazie! Questo ha funzionato come un fascino! – Vik

0

Ecco un altro modo come impostare l'allineamento di un'immagine NSTextAttachment. Speriamo che questo aiuti anche qualcuno che sta lottando con questo. Sto usando il codice seguente in un tableView func (_ 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

Questo è Swift 3.1 utilizzando estensione:

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

} 

In questo modo è possibile applicare facilmente l'allineamento per gli allegati sulla stringa attribuita:

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