2016-03-05 38 views
1

我有一个textView与图像,不同的文本样式等。我想在手表上显示它。有什么办法可以做到吗?Swift如何在手表上显示textStorage

+0

这是你目前在iOS上使用的UITextView吗? – Doug

+0

是的。我有UITextView与iOS应用程序中的图像和不同风格的字体。我想在手表上显示这些文字和图像。 – Woodgun11

+0

所以你需要使用WKInterfaceLabel并在其上调用setAttributedText。看看这是一个起点:http://stackoverflow.com/a/28748030/370965 – Doug

回答

0

它不是一个在Apple Watch上显示NSAttributedString的代码,但是我使用它将NSAttributedString与图像形式相结合,以便于显示。 代码可能不是高质量的(我几乎可以肯定它不是,特别是缩放图像)。

func sendText(stext: NSMutableAttributedString) { 
    dispatch_async(dispatch_get_main_queue()) { 
    let range:NSRange = NSMakeRange(0, stext.length) 
    stext.enumerateAttributesInRange(range, options: NSAttributedStringEnumerationOptions.init(rawValue: 0), usingBlock: { (dic: [String:AnyObject]?, rang:NSRange!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in 


     self.attachement = dic as NSDictionary! 
     self.attachement.enumerateKeysAndObjectsUsingBlock({ (key:AnyObject?, obj:AnyObject?, stop:UnsafeMutablePointer<ObjCBool>) -> Void in 


      let stri = key as! NSString! 
      if stri == "NSAttachment"{ 
       let att:NSTextAttachment = obj as! NSTextAttachment 
       if att.image == nil{ 
        print("no image") 
       }else{ 
        var im:UIImage = att.image! 

        print("image exists") 

        if im.size.height > 340 && im.size.width > 272 { 
         let heightScale: CGFloat = self.textView.frame.size.height/im.size.height 
         let widthScale: CGFloat = self.textView.frame.size.width/im.size.width 

         if heightScale < widthScale { 
          let height = im.size.height * heightScale * 0.7 
          let width = im.size.width * heightScale * 0.7 
          UIGraphicsBeginImageContext(CGSizeMake(width, height)) 
          im.drawInRect(CGRectMake(0, 0, width, height)) 
          im = UIGraphicsGetImageFromCurrentImageContext() 
          UIGraphicsEndImageContext() 
         } else { 
          let height = im.size.height * widthScale * 0.7 
          let width = im.size.width * widthScale * 0.7 
          UIGraphicsBeginImageContext(CGSizeMake(width, height)) 
          im.drawInRect(CGRectMake(0, 0, width, height)) 
          im = UIGraphicsGetImageFromCurrentImageContext() 
          UIGraphicsEndImageContext() 
         } 

        } 
        self.lastRange = rang.location 
        self.finalForWatch.append(NSAttributedString(attributedString: stext.attributedSubstringFromRange(NSMakeRange(0, rang.location)))) 
        self.finalForWatch.append(im) 

       } 
      } 
      if self.lastRange == 0 { 
       self.finalForWatch.append(stext) 
      } else { 
       self.finalForWatch.append(NSAttributedString(attributedString: stext.attributedSubstringFromRange(NSMakeRange(1, stext.length-1)))) 
      } 
     }) 

    }) 
     let data: NSData = NSKeyedArchiver.archivedDataWithRootObject(self.finalForWatch) 
     let applicationDict = ["done" : data] 

     let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) 

     let fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("file.txt") 

     do{ 
      try data.writeToURL(fileDestinationUrl, atomically: true) 
     } catch let error as NSError { 
      print("error writing to url \(fileDestinationUrl)") 
      print(error.localizedDescription) 
     } 

      self.session?.transferFile(fileDestinationUrl, metadata: nil) 

    } 
} 

我保存一切到一个文件中,并使用SENDFILE方法,因为appllicationContext,用户信息和消息总是返回“过大的有效载荷”。

相关问题