I want to convert the following void function written in objective-c to swift but it won't work the way I do it. It goes mostly wrong with step 1,2,4 and 5 cause I don't know how I should translate it.
-(void)applyStyletoSelection:(NSString *)style{
// 1. Get the range of the selected text.
NSRange range = [_textView selectedRange];
// 2. Create a new font with the selected text style.
UIFont *styledFont = [UIFont preferredFontForTextStyle:style];
// 3. Begin editing the text storage.
[_textView.textStorage beginEditing];
// 4. Create a dictionary with the new font as the value and the NSFontAttributeName property as a key.
NSDictionary *dict = #{NSFontAttributeName : styledFont};
// 5. Set the new attributes to the text storage object of the selected text.
[_textView.textStorage setAttributes:dict range:range];
// 6. Notify that we end editing the text storage.
[_textView.textStorage endEditing];
}
this is how far I got but I am sure it is wrong:
func applyStyleToSelection(style: NSString) {
//1
let range = textInput.selectedRange
//2
let styledFont = UIFont.preferredFontForTextStyle(style as String)
//3
textInput.textStorage .beginEditing()
//4
let dict: [String] = ["styledFont"]
//5
//Could not find an overload for 'init' that accepts the supplied arguments
textInput.textStorage .setAttributes([String() : dict]?, range: range)
//6
textInput.textStorage .endEditing()
}
Can anyone help me convert this to swift? You would definitely help me a lot!
You can use native swift types: String rather than NSString.
That dict variable is not actually a dictionary in your translation.
Avoid using var for objects you never mutate.
You never actually set the font attributes to your string, that's a big reason why its not working.
Related
When you use TextEdit and have a selection of string, it will give you the selection color, font, size and other attributes as you see above. How do you get those text selection attributes? I'm certain that I need to use the selectedTextAttributes method. I have the following lines of code.
- (void)textViewDidChangeSelection:(NSNotification *)notification {
if ([notification object] == textView1) {
...
...
NSMutableDictionary *dict = [[textView1 selectedTextAttributes] mutableCopy];
NSLog(#"%#",dict);
}
}
If I run it, the result is not quite like what I expect.
NSBackgroundColor = "NSNamedColorSpace System selectedTextBackgroundColor";
NSColor = "NSNamedColorSpace System selectedTextColor";
There aren't really useful values that I can use to get the text color of the string selection and other attributes. If I ask Google about selectedTextColor, I don't get much luck.
Thank you for your help.
selectedTextAttributes describe what the selection highlighting looks like, not the attributes of selected text. I looked for quite some time for the answer to this question, and finally found it here:
Attribute String Programming Guide
Some example code. For an NSTextView* named editingView, this gathers an array of NSDictionary objects for all the differently formatted ranges in the selection.
NSMutableArray* attributes = [NSMutableArray array];
NSRange selRange = editingView.selectedRange;
NSRange effectiveRange = NSMakeRange(selRange.location, 0);
while (NSMaxRange(effectiveRange) < NSMaxRange(selRange)) {
[attributes addObject: [editingView.textStorage attributesAtIndex: NSMaxRange(effectiveRange) longestEffectiveRange: &effectiveRange inRange: selRange]];
}
I have a predicate editor, which the template was generate via the following:
NSArray * test = [NSArray arrayWithObjects:
[NSExpression expressionForKeyPath: #"Abc"],
[NSExpression expressionForKeyPath: #"Def"],
nil];
NSPredicateEditorRowTemplate * template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions: test
rightExpressionAttributeType: NSStringAttributeType
modifier: NSDirectPredicateModifier
operators: [NSArray arrayWithObject:
[NSNumber numberWithUnsignedInteger:NSContainsPredicateOperatorType]]
options:(NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption)];
So if I fill in a predicate editor like this:
When I log out the generated predicate I get:
Abc CONTAINS[cd] "abc" OR Def CONTAINS[cd] "def"
What I'm wondering is if I can somehow have the predicate editors template display be different than the value that gets set in the generated predicate.
EX: I want the output predicate to have:
Field1 CONTAINS[cd] "abc" OR Field2 CONTAINS[cd] "def"
Even though the editor still displays abc and def as the fields. Is this possible?
Yes, you can do this.
You want the array of left expressions to be the actual keyPaths in the final predicate. In your case, "Field1" and "Field2".
As for making a different value appear in the popup, here's where a mind-bending concept comes in:
You're going to localize your predicate editor into English.
There are two ways you could do this.
With a .strings file
With an NSDictionary
With a .strings file
In your source, you would include the following in a comment:
// NSLocalizedStringFromTable(#"%[Field1,Field2]# %[contains]# %#", #"PredicateEditor", #"")
When you run genstrings on your source code, this will generate a PredicateEditor.strings file with the following entries:
"%[Field1]# %[contains]# %#" = "%[Field1]# %[contains]# %#";
"%[Field2]# %[contains]# %#" = "%[Field2]# %[contains]# %#";
You would change the values to be:
"%[Field1]# %[contains]# %#" = "%[Abc]# %[contains]# %#";
"%[Field2]# %[contains]# %#" = "%[Def]# %[contains]# %#";
Then, when you create your NSPredicateEditor, you would set the formattingStringsFileName property to "PredicateEditor", and the editor will take care of the rest.
With an NSDictionary
This would follow the same fundamental concepts as the .strings option, except that you would essentially do:
NSDictionary *formatting = #{
#"%[Field1]# %[contains]# %#" : #"%[Abc]# %[contains]# %#",
#"%[Field2]# %[contains]# %#" : #"%[Def]# %[contains]# %#"
}
[myPredicateEditor setFormattingDictionary:formatting];
That's all you have to do.
I blogged about this a long time ago, and that has more information that you might find useful.
Basically you want to modify the title of the menu items in your popup button. That's all you need to do. It shouldn't effect the underlying predicate that you get returned. If you created it in interface builder it's easy to get at the menu items of a template and set their title. But since you did this in code you'll have to fix it in code.
Here's how you might do that. In my row template class I wanted to change the width of my NSTextFields. So in my row template class I look for them and modify them like this...
- (void)awakeFromNib {
NSArray* views = [self templateViews];
for (id view in views) {
if ([[view class] isEqual:[NSTextField class]]) {
NSRect tfFrame = [view frame];
tfFrame.size.width = 600;
[view setFrame:tfFrame];
}
}
}
You can see that I get the templateViews and look for the NSTextFields... and then modify them. You could do something similar looking for NSPopupButtons. Once you found one check their menu item titles and look for the ones titled "abc" and "def" and change their title to "Field1" and "Field2" respectively.
Rather than using NSLocalizedString with the option literals in the specific format and genstrings to generate the localization strings, it seems easier/cleaner to generate the final localization .strings file strings yourself.
From this blog post, we can use the Private API _generateFormattingDictionaryStringsFile to get the formatting strings from the NSPredicateEditor itself:
extension NSPredicateEditor {
func formattingDictionaryStrings() -> String? {
var strings: String? = nil
if let formattingDictionaryData = self.perform(Selector("_generateFormattingDictionaryStringsFile"))?.takeRetainedValue() as? Data {
strings = String(data: formattingDictionaryData, encoding: .utf16)
}
return strings
}
}
This generates all of the permutations, skipping the need for genstrings. You then replace the tokens to the right of the = with your user-displayed strings.
"%[ABC]# %[is]# %[123]#" = "%1$[ABC]# %2$[is]# %3$[123]#";
"%[ABC]# %[is]# %[456]#" = "%1$[ABC]# %2$[is]# %3$[456]#";
"%[ABC]# %[is]# %[789]#" = "%1$[ABC]# %2$[is]# %3$[789]#";
"%[ABC]# %[contains]# %[123]#" = "%1$[ABC]# %2$[contains]# %3$[123]#";
"%[ABC]# %[contains]# %[456]#" = "%1$[ABC]# %2$[contains]# %3$[456]#";
"%[ABC]# %[contains]# %[789]#" = "%1$[ABC]# %2$[contains]# %3$[789]#";
Even better, you can construct the code that builds each NSPredicateEditorRowTemplate to take as input both the keypath used internally in the predicate and the localized string for that option.
Your method can then generate the strings above, but with the correct localization already inserted.
NOTE: calling this _generateFormattingDictionaryStringsFile private API method seems to cause random crashes within the NSPredicateEditor:
this class is not key value coding-compliant for the key rowType.
So be sure to run it once when needed, but don't leave it active when you're normally running or testing your app.
Yes, it is all a matter of localization, thanks to the fact that the objects are menu items. And they can be treated as such easily.
All you need to do is ...
localize your application.
Then enter the .strings file and change the value to what you want to be displayed
or...
use tools for managing/translating localized apps.
Here is an example of changing things directly in a .strings file:
Change is to ist and booktitle to Buchtitel
/* Class = "NSMenuItem"; title = "is"; ObjectID = "G1c-st-GEK"; */
"G1c-st-GEK.title" = "ist";
/* Class = "NSMenuItem"; title = "booktitle"; ObjectID = "nQh-54-5Nx"; */
"nQh-54-5Nx.title" = "Buchtitel";
Remark: The best way to find the line to change is by looking for the ObjectID. This can be found for each MenuItem by the UIB identity inspector:
I'm currently working on a project where the user defines some parameters in a NSDictionnary, that I'm using to setup some objects.
For example, you can ask to create a Sound object with parameters param1=xxx, param2=yyy, gain=3.5 ... Then an Enemi object with parameters speed=10, active=YES, name=zzz ...
{
active = NO;
looping = YES;
soundList = "FINAL_PSS_imoverhere_all";
speed = 100.0;
}
I then instantiate my classes, and would like to set the ivars automatically from this dictionnary.
I've actually wrote some code to check that this parameter exists, but I'm having trouble in actually setting the parameter value, especially when the parameter is non object (float or bool).
Here's what I'm doing so far :
//aKey is the name of the ivar
for (NSString *aKey in [properties allKeys]){
//create the name of the setter function from the key (parameter -> setParameter)
NSString *setterName = [aKey stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[aKey substringToIndex:1] uppercaseString]];
setterName = [NSString stringWithFormat:#"set%#:",setterName];
SEL setterSelector = NSSelectorFromString(setterName);
//Check if the parameter exists
if ([pge_object respondsToSelector:setterSelector]){
//TODO : automatically set the parameter
}
else{
[[PSMessagesChecker sharedInstance]logMessage:[NSString stringWithFormat:#"Cannot find %# on %#", aKey, [dict objectForKey:#"type"]] inColor:#"red"];
NSLog(#"Cannot find %# on %#", aKey, [dict objectForKey:#"type"]);
}
}
}
As you can see, I don't know what to do once I've found that the parameter exists on the object. I tried to use "performSelector... withObject..., but my problem is that some of the parameters are non-objects (float or bool).
I also tried to get the class of the parameter, by using the setter, but it didn't help.
Did anyone manage to do something like that?
Jack Lawrence's comment is spot on.
What you are looking for is called Key Value Coding, or just KVC.
This fundamental part of Cocoa lets you get and set any instance variable using its name as a String and a new value.
It will automatically handle coercing Objects to primitive values, so you can use it for int and float properties too.
There is also support for validating values and handling unknown properties.
see the docs
your code, without validation, could be written
for( id eachKey in props ) {
[anOb setValue:props[eachKey] forKey:eachKey];
}
or just
[anOb setValuesForKeysWithDictionary:props];
as Jack said.
For the non-object parameters you have to put them into an object, for example NSNumber or NSValue. You can then add these objects into your dictionary.
For Example:
float f = 0.5;
NSNumber f_obj = [NSNumber numberWithFloat:f];
I have a NSTableView that shows the path of files in one column. When the user resizes the tableview I want the pathname (e.g. /Users/name/testfile.m) to be resized, but I want the end of the pathname (e.g. ...name/testfile.m) to be visible and not the start (e.g. /Users/test/te...) of the path as happens by default. I wrote a function that successfully does what I want to do, but the tableview flickers while redrawing as the user scales the tableview. I think there must be a better, more elegant algorithm for doing this, but I have looked into the documentation for NSString and on Stackoverflow and I cant find anything that gives a better solution. If anyone has a more elegant solution to this problem that would be greatly appreciated. Thanks! Cheers, Trond
My current function:
-(NSString *) truncateString:(NSString *) myString withFontSize:(int) myFontSize withMaxWidth:(NSInteger) maxWidth
{
// Get the width of the current string for a given font
NSFont *font = [NSFont systemFontOfSize:myFontSize];
CGSize textSize = NSSizeToCGSize([myString sizeWithAttributes:[NSDictionary dictionaryWithObject:font forKey: NSFontAttributeName]]);
NSInteger lenURL =(int)textSize.width;
// Prepare for new truncated string
NSString *myStringShort;
NSMutableString *truncatedString = [[myString mutableCopy] autorelease];
// If the available width is smaller than the string, start truncating from first character
if (lenURL > maxWidth)
{
// Get range for first character in string
NSRange range = {0, 1};
while ([truncatedString sizeWithAttributes:[NSDictionary dictionaryWithObject:font forKey: NSFontAttributeName]].width > MAX(TKstringPad,maxWidth))
{
// Delete character at start of string
[truncatedString deleteCharactersInRange:range];
}
myStringShort = [NSString stringWithFormat:#"...%#",truncatedString];
}
else
{
myStringShort=myString;
}
return myStringShort;
}
The typical approach would be simply:
[tableViewCell setLineBreakMode:NSLineBreakByTruncatingHead];
As Dondragmer noted, this property may also be set in Xcode's NIB editor.
I need to find the pixel-frame for different ranges in a textview. I'm using the - (CGRect)firstRectForRange:(UITextRange *)range; to do it. However I can't find out how to actually create a UITextRange.
Basically this is what I'm looking for:
- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView {
UITextRange*range2 = [UITextRange rangeWithNSRange:range]; //DOES NOT EXIST
CGRect rect = [textView firstRectForRange:range2];
return rect;
}
Apple says one has to subclass UITextRange and UITextPosition in order to adopt the UITextInput protocol. I don't do that, but I tried anyway, following the doc's example code and passing the subclass to firstRectForRange which resulted in crashing.
If there is a easier way of adding different colored UILables to a textview, please tell me. I have tried using UIWebView with content editable set to TRUE, but I'm not fond of communicating with JS, and coloring is the only thing I need.
Thanks in advance.
You can create a text range with the method textRangeFromPosition:toPosition. This method requires two positions, so you need to compute the positions for the start and the end of your range. That is done with the method positionFromPosition:offset, which returns a position from another position and a character offset.
- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView
{
UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
UITextPosition *end = [textView positionFromPosition:start offset:range.length];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
CGRect rect = [textView firstRectForRange:textRange];
return [textView convertRect:rect fromView:textView.textInputView];
}
It is a bit ridiculous that seems to be so complicated.
A simple "workaround" would be to select the range (accepts NSRange) and then read the selectedTextRange (returns UITextRange):
- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView {
textView.selectedRange = range;
UITextRange *textRange = [textView selectedTextRange];
CGRect rect = [textView firstRectForRange:textRange];
return rect;
}
This worked for me even if the textView is not first responder.
If you don't want the selection to persist, you can either reset the selectedRange:
textView.selectedRange = NSMakeRange(0, 0);
...or save the current selection and restore it afterwards
NSRange oldRange = textView.selectedRange;
// do something
// then check if the range is still valid and
textView.selectedRange = oldRange;
Swift 4 of Andrew Schreiber's answer for easy copy/paste
extension NSRange {
func toTextRange(textInput:UITextInput) -> UITextRange? {
if let rangeStart = textInput.position(from: textInput.beginningOfDocument, offset: location),
let rangeEnd = textInput.position(from: rangeStart, offset: length) {
return textInput.textRange(from: rangeStart, to: rangeEnd)
}
return nil
}
}
To the title question, here is a Swift 2 extension that creates a UITextRange from an NSRange.
The only initializer for UITextRange is a instance method on the UITextInput protocol, thus the extension also requires you pass in UITextInput such as UITextField or UITextView.
extension NSRange {
func toTextRange(textInput textInput:UITextInput) -> UITextRange? {
if let rangeStart = textInput.positionFromPosition(textInput.beginningOfDocument, offset: location),
rangeEnd = textInput.positionFromPosition(rangeStart, offset: length) {
return textInput.textRangeFromPosition(rangeStart, toPosition: rangeEnd)
}
return nil
}
}
Swift 4 of Nicolas Bachschmidt's answer as an UITextView extension using swifty Range<String.Index> instead of NSRange:
extension UITextView {
func frame(ofTextRange range: Range<String.Index>?) -> CGRect? {
guard let range = range else { return nil }
let length = range.upperBound.encodedOffset-range.lowerBound.encodedOffset
guard
let start = position(from: beginningOfDocument, offset: range.lowerBound.encodedOffset),
let end = position(from: start, offset: length),
let txtRange = textRange(from: start, to: end)
else { return nil }
let rect = self.firstRect(for: txtRange)
return self.convert(rect, to: textInputView)
}
}
Possible use:
guard let rect = textView.frame(ofTextRange: text.range(of: "awesome")) else { return }
let awesomeView = UIView()
awesomeView.frame = rect.insetBy(dx: -3.0, dy: 0)
awesomeView.layer.borderColor = UIColor.black.cgColor
awesomeView.layer.borderWidth = 1.0
awesomeView.layer.cornerRadius = 3
self.view.insertSubview(awesomeView, belowSubview: textView)
- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView {
UITextRange *textRange = [[textView _inputController] _textRangeFromNSRange:range]; // Private
CGRect rect = [textView firstRectForRange:textRange];
return rect;
}
Here is explain.
A UITextRange object represents a range of characters in a text
container; in other words, it identifies a starting index and an
ending index in string backing a text-entry object.
Classes that adopt the UITextInput protocol must create custom
UITextRange objects for representing ranges within the text managed by
the class. The starting and ending indexes of the range are
represented by UITextPosition objects. The text system uses both
UITextRange and UITextPosition objects for communicating text-layout
information. There are two reasons for using objects for text ranges
rather than primitive types such as NSRange:
Some documents contain nested elements (for example, HTML tags and
embedded objects) and you need to track both absolute position and
position in the visible text.
The WebKit framework, which the iPhone text system is based on,
requires that text indexes and offsets be represented by objects.
If you adopt the UITextInput protocol, you must create a custom
UITextRange subclass as well as a custom UITextPosition subclass.
For example like in those sources