2014-11-20 25 views
6

Bu harika eğitici Implementing Rich Text with Images on OS X and iOS tarafından @Duncan Groenewald'i takip ediyorum ve UITextView numaralı telefonu görüntüleyebiliyordum. Ancak, bu görüntüler onların olmasını istediğim şekilde ortalanmıyor. Gördüğünüz gibi, benim görüntü X ekseni merkezli istiyorum görüntüNSTextAttachment görüntü hizalaması

NSTextAttachment sample image

bakınız.

-attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex'da uygun değerlerle rect'u döndürmeyi denedim, ancak bu yardımcı olmadı.

Ayrıca, öznitelikli dize için NSKernAttributeName ayarlamayı denedim. Ama tüm yaptığı görüntüyü nasıl saklıyordu.

cevap

5

Eki üzerindeki paragraf stilini orta hizalama ile ayarlamayı deneyin.

Resimleriniz iliştirilmiş bir dizeye ek olarak gömülü olarak eklenmişse, bunlara atıfta bulunulan dizenin ek özniteliklerini sıralayarak erişebilirsiniz. Örneğin :

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

Teşekkürler! Bu bir çekicilik gibi çalıştı! – Vik

0

İşte bir NSTextAttachment görüntü için hizalama nasıl ayarlanacağı başka bir yolu. Umarım bu da bununla mücadele eden birine yardımcı olacaktır. Ben fonk tableView aşağıdaki kodu kullanıyorum (_ 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

Bu uzantısını kullanılarak Swift 3.1 geçerli:

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

} 

Bu şekilde, ilişikteki dize ekleri kolayca hizalayabilirsiniz:

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