I have a textView with images, different text styles etc. I wanna display it on watch. Is there any way to do it?
Asked
Active
Viewed 107 times
1
-
Is that a UITextView that you currently have on iOS? – Doug Mar 05 '16 at 10:56
-
Yes. I have UITextView with images and different styles of font in iOS app. And I'd like to display this text and images on watch. – Woodgun11 Mar 05 '16 at 11:01
-
So you'll need to use a WKInterfaceLabel and call setAttributedText on it. Check out this as a starting point: http://stackoverflow.com/a/28748030/370965 – Doug Mar 05 '16 at 11:06
-
Thanks:) I'll check it at the moment because now I'm fighting with sending this text to Watch(I thought it's easier). – Woodgun11 Mar 05 '16 at 11:09
-
Check out MMWormhole for a simple message sending implementation: https://github.com/mutualmobile/MMWormhole – Doug Mar 05 '16 at 11:24
-
It didn't work:( But I've just done it with WCSession – Woodgun11 Mar 05 '16 at 11:40
-
I have a problem with images - I append this attributed string to label and I receive: "Attributed string '' contains NSAttachmentAttributeName. Removed". – Woodgun11 Mar 05 '16 at 12:02
1 Answers
0
It is not a code for displaying NSAttributedString on apple watch, but I use it for sending NSAttributedString with images in the form which will be easier to display. Code may not be a high-quality(I am almost sure it isn't, especially scaling images).
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)
}
}
I save everything into a file and use a sendFile method because appllicationContext, userInfo and messages always return "too big payloads".

Woodgun11
- 122
- 9