2016-03-05 51 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,用戶信息和消息總是返回「過大的有效載荷」。

相關問題