0

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!

  • 1
    Update your question with your attempted Swift code. Explain what issues you are having with the translation. – rmaddy Jul 08 '15 at 16:36
  • OK, you've added the Swift code. Now you need to tell us what issue you have with it. Simply saying you are sure it is wrong doesn't help any. Be specific about what your issues are and what problems you are having with the Swift code compared to the Objective-C code. – rmaddy Jul 08 '15 at 16:59
  • I don't really know what the problems are. That's why I ask help on this forum. I'm pretty new to swift so I don't know how to translate such a function from objective-c to swift. At this moment the problem occurred here "var dict = NSFontAttributeName(styledFont)" It says:"Cannot invoke 'NSFontAttributeName' with an argument list of type '(UIFont)' – user3638160 Jul 08 '15 at 17:22
  • You should edit the error message into the question, instead of just leaving it in the comments. That will make this a complete question. – skrrgwasme Jul 08 '15 at 17:23
  • Thanks! I will update it with the error. – user3638160 Jul 08 '15 at 17:29
  • I updated the question, I hope it is possible to give me an answer. If something misses, please tell me. If I solved the problem I'll let it know. – user3638160 Jul 08 '15 at 19:07

1 Answers1

0

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.

Chris Slowik
  • 2,859
  • 1
  • 14
  • 27