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!