NSTextView add URL link to the selected Text? - cocoa

I Have an NSTextView.
I just want to add an Attribute (an NSLinkAttributeName) to the selected Text in the NSTextView...
Can You Help me ?
Thanks.

You want to get the view's textStorage (which is basically a mutable attributed string), then add the NSLinkAttributeName attribute to the selected range; the value of that attribute is the URL to link to.
[[textView textStorage] addAttribute: NSLinkAttributeName value: url range:[textView selectedRange]];

Been a while since I played with ObjC but this should do the trick. It replaces the selected text with the original content with your attr appended. Checked through it but please excuse any typos.
NSTextView *textView = ...;
NSDictionary *attributes = ...;
//Get selected text string from TextView (see Text superclass) and append attr link
NSRange selRange = [textView selectedRange];
NSMutableString *changedStr = [[[textView string] substringWithRange:selRange] mutableCopy];
[changedStr appendString:[attributes objectForKey:NSLinkAttributeName]];
//Replace the selected text range in the TextView
[textView replaceCharactersInRange:selRange withString:[NSString stringWithString:changedStr]];
[changedStr release];
See class defs:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html
-replaceCharactersInRange:withString:
-selectedRange
-scrollRangeToVisible: if you want to present your change immediately
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
substringWithRange:

Related

NSAttributedString from localized string with format specifier

I have a localized string:
"%# some text" = "%# some text";
The format specifier %# may appear in any location in the localized string.
The problem is this string should be an NSAttributedString; the %# replacement and the rest of the text should have different attributes. How can I solve this issue?
1) Get your localized template using NSLocalizedString().
2) Get the text to insert.
3) Combine the two using -stringWithFormat:.
4) In the template, find the location of the placeholder using -rangeOfString:
5) Find the range of the inserted text in the formatted string, using the start position found in the last step, with -rangeOfString:options:range:. (The third argument here is the range within which to search; this avoids finding non-substituted text.)
6) Create an attributed string from the formatted string, using the range to apply attributes to the inserted text.
You can use NSMutableAttributedString for this case. Here is Apple documentation
NSString *textToDisplay = [NSString stringWithFormat:#"%# somet text",localizedString];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:textToDisplay];
[attrStr addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"Exo2-Regular" size:30]
range:NSMakeRange(0, locatilzedString.length)];
[attrStr addAttribute:NSFontAttributeName
value:[UIFont fontWithName:#"Exo2-Bold" size:30]
range:NSMakeRange(locatilzedString.length, attrStr.length)];
label.attributedText = attrStr

Replace NSTextattachement Image on NSMutableattributedString

I'm building an UITextView with text and images (Subclassing NSTextstorage for displaying my content)
I'm having textcontent with images URLs.
So my problem is that i need download all the images if they're not cached.
So i want to first insert a placeholder image, download the image and then replace the placeholder image by the downloaded one.
Here's how i do my stuff.
First, i'm formatting my text with images url by replacing all urls with this tag :
[IMG]url[/IMG]
Then i'm using a regex to get all these tags.
I'm testing if there's a cached image or not. If not, i extract all the urls, download them and cache them.
I've created an NSObject class ImageCachingManager and declared a delegate method called when an image has been downloaded :
#protocol ImageCachingManagerDelegate <NSObject>
- (void)managerDidCacheImage:(UIImage *)image forUrl:(NSString *)url;
#end
Like this, I tough that I could use the url of the image got by the delegate method to search the matching url in my NSTextstorage attributedString and replace the current NSTextattachement image by the downloaded one.
But I don't know how to do that...
Thanks for help !
I'm working on something very similar to this at the moment and think this might help. The code is very much alpha but hopefully it will get you to the next step - I'll step through:
Overall Cycle
1. Find you image tags in the full text piece using Reg Ex or XPath - personally i find Hppl to be more powerful but if your content is well structured and reliable, regex is probably fine.
https://github.com/topfunky/hpple
Reduce the space of this match to 1 character and store that range - A textAttachment occupies only 1 character of space within a textview so it's best to reduce this to 1 otherwise when you replace your first match of characters in a range with the first textattachment the next range marker becomes out of date which will lead to issues. Depending on how much processing you need to do this text input during init, this is an important step, i have to do a lot of processing on the text and the ranges change during this parsing so I created an array of special characters that I know is never going to be in the inputs and push these single characters into the reserved space, at the same time i store this special character and the src of the image in an array of a very simple NSObject subclass that stores the SpecialChar, ImgSrc plus has space for the NSRange but i basically find the special character later in the process again because it has been moved about since this point and then set the nsrange at the very end of processing - this may not be necessary in your case but the principle is the same; You need a custom object with NsRange (which will become a text attachment) and the imgSource.
Loop through this array to add placeholder imageAttachments to your attributed string. You can do this by adding a transparent image or a 'loading' image. You could also check your cache for existing images during this point and skipping the placeholder if it exists in cache.
Using your delegate, when the image is successfully downloaded, you need to replace the current attachment with your new one. By replacing the placeholder in the range you've already stored in your object. Create a placeholder attributedString with the NSTextAttachment and then replace that range as below.
Some sample code:
Steps 1 & 2:
specialCharsArray = [[NSArray alloc]initWithObjects:#"Û", #"±", #"¥", #"å", #"æ", #"Æ", #"Ç", #"Ø", #"õ", nil];
//using Hppl
NSString *allImagesXpathQueryString = #"//img/#src";
NSArray *imageArray = [bodyTextParser searchWithXPathQuery:allImagesXpathQueryString];
//
imageRanges = [[NSMutableArray alloc] init];
if([imageArray count]){
for (TFHppleElement *element in imageArray) {
int i = 0;
NSString *imgSource = [[[element children] objectAtIndex:0] content];
NSString *replacementString = [specialCharsArray objectAtIndex:i];
UIImage *srcUIImage = [UIImage imageNamed:imgSource];
[srcUIImage setAccessibilityIdentifier:imgSource]; //only needed if you need to reference the image filename later as it's lost in a UIImage if stored directly
//imagePlacement is NSObject subclass to store the range, replacement and image as above
imagePlacement *foundImage = [[imagePlacement alloc]init] ;
[foundImage initWithSrc:srcUIImage replacement:replacementString];
[imageRanges addObject:foundImage];
i++;
}
Step 3:
-(void)insertImages{
if ([imageRanges count]) {
[self setScrollEnabled:NO]; //seems buggy with scrolling on
int i = 0; //used to track the array placement for tag
for(imagePlacement *myImagePlacement in imageRanges){
// creates a text attachment with an image
NSMutableAttributedString *placeholderAttString = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
//scales image down to ration of width of view - you probably don't need this
CGSize scaleToView = imagePlacement.imgSrc.size;
scaleToView.width = self.frame.size.width;
scaleToView.height = (self.frame.size.width/imagePlacement.imgSrc.size.width)*imagePlacement.imgSrc.size.height;
attachment.image = [self imageWithColor:[UIColor clearColor] andSize:scaleToView];
NSMutableAttributedString *imageAttrString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
[self setAttributedText:placeholderAttString];
i++;
}
}
[self setScrollEnabled:YES];
}
- (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize) size {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Change the Color of specific substrings in NSTextView

I want to change the color of specific text in NSTextView. The method should check that after a keydown event.
For example: the word void is finished and the string void changes the
color to blue. Like a code editor.
I searched for a long time but don't find anything.
My Code:
NSRange range = [text rangeOfString:#"void"];
NSString *substring = [[text substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//I think, here is the mistake.
NSAttributedString *now = [NSAttributedString initWithString:substring];
[now setTextColor:[[NSColor blueColor]]];
I have read that i have to use a NSAttributedString but i don't know how I can get this class from a string.
I'am quite new in cocoa programming.
Thanks for every help!
You can do this way:
NSString *str = #"Hello. That is a test attributed string.";
//in place of NSMakeRange put your range
[self.textview setRichText:YES];
[self.textview setString:str];
[self.textview setTextColor:[NSColor redColor] range:NSMakeRange(3,5)];

Cocoa NSScrollView starts at center

I have a NSScrollView item in my main window. Whenever I launch the program, there is text in the scroll view and it starts at the center. Shouldn't the user start reading at the top but why does the app launch it at the center? Thanks!
This can happen if you do not set your views up correctly.
Here's the information I have:
1: You use Interface Builder to set up your views.
2: You are using a NSTextView
3: The code you use for changing the text is:
[[_chapterContent insertText:[Book objectForKey:bookAndChapters]];
4: As you're using a NSTextView, we're not dealing with an issue caused by flipped coordinates.
I'm not sure whether you should use insertText for this task.
At least, you can use something like this...
range.location = 0;
range.length = 0;
[textView setSelectedRange:range];
...to set the position of the caret.
You can then do a...
[textView scrollRangeToVisible:range];
...if you like.
Note: You can use setSelectedRange with a length of 0 and any location within [textStorage length], to position the caret.
-Let me know if this makes the change you need.
I'd suggest using...
textStorage = [textView textStorage];
range.location = 0;
range.length = [textStorage length];
if([self shouldChangeTextInRange:range replacementString:string])
{
[textStorage beginEditing];
[textStorage replaceCharactersInRange:range withAttributedString:attrStr];
[textStorage endEditing];
}
... to replace the text with an attributed string.
If you don't need an attributed string, there's a simpler method available:
[textStorage replaceCharactersInRange:range withString:str];
It's worked well for me (and a number of other people) for many years; it's the recommended way of changing the contents of the NSTextView.
-If you just want to replace the entire contents with a string, you can use
[textView setString:string];

Setting the text of an NSTextField programmatically

I have a GUI built on IB (Xcode 4).
It has a Static Text field connected to an NSTextField. After reading the information from an XML file it's supposed to change the text to whatever it is coming from the XML
the .h is as follow:
IBOutlet NSTextField * DataBaseLocation;
the .m
NSMutableArray* DBLoc = [[NSMutableArray alloc] initWithCapacity:1];
NSXMLElement* root = [doc rootElement];
NSArray* DBarray = [root nodesForXPath:#"//DataBaseLocation" error:nil];
for(NSXMLElement* xmlElement in DBarray)
[DBLoc addObject:[xmlElement stringValue]];
NSString * DBLocationString = [DBLoc objectAtIndex:0];
[DataBaseLocation setStringValue:DBLocationString];
NSLog(#"DBLoc: %#", DBLoc);
The NSLog shows that DBLoc has the correct string, yet the Text Field is empty and never gets set.
yes, I checked the connections in IB.
Any ideas? thanks!
Found the answer.
I needed to initialize the NSXMLDocument with NSXMLDocumentTidyXML like:
NSXMLDocument* doc = [[NSXMLDocument alloc] initWithContentsOfURL: [NSURL fileURLWithPath:input] options:NSXMLDocumentTidyXML error:NULL];
You should print out DBLocationString instead of DBLoc to make sure it's not empty or in some corrupted format that can't be passed as a string value and go from there.

Resources