TTTAttributedLabel addLinkToURL NSRange confusion - xcode

I am making a swift app and I want to add a url to a TTTAttributedLabel. I have got addLinkToUrl but it wants a NSRange. What should I put. I am new to swift. I want the url to be for the entire text.
//PersonTalking is a TTTAttributedLabel
var characterSpeaking:NSDictionary = item["characterspeaking"] as NSDictionary
var characterSpeakingString:String = characterSpeaking["text"] as String
var characterHref:String = characterSpeaking["href"] as String
var characterUrl = NSURL(string: characterHref)
println(characterSpeakingString)
PersonTalking.text = characterSpeakingString
PersonTalking.addLinkToURL(characterUrl, withRange: )
What can I do?
Thanks

If the link is meant to include the full label, then your range should be the full length of your text.
PersonTalking.addLinkToURL(characterUrl,
withRange:NSMakeRange(0,
countElements(characterSpeakingString))

Related

protractor does not recognize '\' at uploading a file

I've been reading a few answers to this and implemented one of those. Here is my code:
var path = require('path');
var fileToUpload = "C:\Users\patricio.lussenhoff\Desktop\test.txt",
absolutePath = path.resolve(__dirname, fileToUpload);
var type3 = browser.element(by.css('[type="file"]'));
type3.sendKeys(absolutePath);
The protractor apparently is not recognizing the slashes (I've tried '/' way too)
and the control shows like this:
here is the example I'm talking about
Any thoughts ?
Try it:
var fileToUpload = "C:\\Users\\patricio.lussenhoff\\Desktop\\test.txt";
var type3 = browser.element(by.css('[type="file"]'));
type3.sendKeys(fileToUpload);
This way you don't need to use 'path.resolve' because you are passing the complete and correct path.
Based on the OS Use single forward slash(Linux, Unix, etc.,) or double backward slash(Windows) to read files:
var fileToUpload = "C:/Users/patricio.lussenhoff/Desktop/test.txt",
var fileToUpload = "C:\\Users\\patricio.lussenhoff\\Desktop\\test.txt",

Swift UITextChecker

I am trying to use UITextChecker in my Swift project. The code below currently has an error on the last line:
var checker:UITextChecker = UITextChecker()
var textLength = countElements(textView.text)
var checkRange:NSRange = NSMakeRange(0, textLength)
var misspelledRange:NSRange = checker.rangeOfMisspelledWordInString(textView.text, range: checkRange, startingAt: checkRange.location, wrap: false, language: "en_Us")
var arrGuessed:NSArray = checker.guessesForWordRange(misspelledRange, inString: textView.text, language: "en_US")!
var correctedStr = textView.text.stringByReplacingCharactersInRange(misspelledRange, withString: [arrGuessed.objectAtIndex(0)])
The error says:
'NSRange' is not convertible to 'Range<String.index>'
I am not sure where I am going wrong.
Thanks
The stringByReplacingCharactersInRange method you're using expects Range<String.Index> to be passed in, not NSRange. You can't use misspelledRange because it's the wrong type. The link in the possible duplicate comment (NSRange to Range<String.Index>) has examples of deriving a Range<String.Index> from an NSRange or casting text to NSString, whose stringByReplacingCharactersInRange method does use NSRange.

Parse PDF with ABCPDF

I want to parse a PDF document I download with ABCPDF, but I cant find any elements in the document or how to reach them and iterate them. I want to parse out some text.
var webClient = new WebClient();
var bytes = webClient.DownloadData("http://test.com/test.pdf");
var doc = new Doc();
doc.Read(bytes);
Use the Doc.GetText method to extract content from the current page, specifying the format in which content is to be returned.
doc.PageNumber = 1;
string pageContent = doc.GetText("Text");
The example above will return plain text in layout order. Specifying "SVG" or "SVG+" returns additional information along with the text, such as style and position.

Break line in long label text

Is there any trick to break a label text? Because '\n' '\r' '\n\r' don't work.
Many Thanks
if you use those 2 parameters you do what you want don't you ?
app.createLabel(text).setWidth(width).setWordWrap(true)
here is an example (among other widgets ;-):
function showurl() {
var app = UiApp.createApplication();
app.setTitle("Anchor in a popup ;-)");
var panel = app.createFlowPanel()
var image = app.createImage('https://sites.google.com/site/appsscriptexperiments/home/photo.jpg').setPixelSize(50, 50)
var link = app.createAnchor('This is your link', 'https://sites.google.com/site/appsscriptexperiments/home');
var lab = app.createLabel("wrap it because it's too narrow").setWidth(90).setWordWrap(true);
var quit = app.createButton('quit');
panel.add(image).add(link).add(lab).add(quit);
app.add(panel);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
EDIT : I found an old post(on the Google group forum, thanks again Henrique ;-) about breaking lines in toast messages and here is the code I used for that case... the principle should work for Labels too but I didn't try.
To use it, just use \n (where you want to break the line) in a variable containing your text and pass it through this function. (there are some comment in the script to explain)
function break_(msg){
var temp = escape(msg);// shows codes of all chars
msg = unescape(temp.replace(/%20/g,"%A0")); // replace spaces by non break spaces
temp = msg.replace("\n"," "); // and replace the 'newline' by a normal space
return temp; // send back the result
}
Would something like this work?
//takes a line of text and returns a flex table broken by \n
function breakLabel(text) {
var app = UiApp.getActiveApplication();
var flexTable = app.createFlexTable();
text = text.split('\n'); // split into an array
for (var i=0; i<text.length; i++){
flexTable.setWidget(i, 0, app.createLabel(text[i].toString()));
}
return flexTable;
}
Adding them to a vertical panel helps as well (not the way you want, but still..):
var vPanel = app.createVerticalPanel().setSize(100,100);
var label = app.createLabel('predominantly blabla blala blabla');
app.add(vPanel.add(label));
See reference
For anyone just now stumbling upon this, the best solution seems to be creating an HTML output for anything that needs line breaks.
Documentation
var htmlApp = HtmlService
.createHtmlOutput('<p>A change of speed, a change of style...</p>')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('My HtmlService Application')
.setWidth(250)
.setHeight(300);
SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
// The script resumes execution immediately after showing the dialog.

setting Address Book image with MacRuby

I am trying to set my user's Address Book Image with MacRuby. Here is what I have so far:
framework 'ScriptingBridge'
framework 'AppKit'
download = "/path.png"
data = NSData.dataWithContentsOfFile(download)
bitmap = NSBitmapImageRep.imageRepWithData(data)
final = bitmap.representationUsingType(NSTIFFFileType, properties: nil)
book = SBApplication.applicationWithBundleIdentifier("com.apple.addressbook")
book.myCard.setImage(final)
I have also tried book.myCard.setImage(final.base64Encoding), by the way.
What do I need to do to make this work?
Thanks!
You must have an NSImage
myImage = NSImage.alloc.initWithContentsOfFile(download)
book = SBApplication.applicationWithBundleIdentifier("com.apple.addressbook")
book.myCard.setImage(myImage)

Resources