Ulli H
Ulli H

Reputation: 1878

again convert textKit from Objective-C to Swift

The following code was used in my app to change the state for text in a textview with strikeThrough. Now i wrote a small sample-app, in Objective-C and Swift. Again the result is frustrating as u can see in the screenshots. Any help is welcome so much.

I just use a TextView and try to show some text with StrikeThrough-Layout (other styles like Bold, Italic, Underline... have the same result)

First objc, that is ok, although the font-size of the striked part is very small

enter image description here

and now with Swith. The font is small as with objc, but there is no strikethrough :-)

enter image description here

And now again (dont know another way) the test-code:

objc Part 1: set the Font for a Range and call makeStrikeThrough()

- (void) setFont
{
    NSRange range = NSMakeRange(11, 24);
    [self makeStrikeThrough:range];
}

same in swift:

func setFont() {
    let range = NSMakeRange(11, 24)
    self.makeStrikeThrough(range)
}

objc Part 2: the strikeThrough

- (void) makeStrikeThrough:(NSRange)selectedRange
{
    NSMutableDictionary *dict = [self getDict:selectedRange];
    [_textView.textStorage beginEditing];
    [_textView.textStorage setAttributes:dict range:selectedRange];
    [_textView.textStorage endEditing];
}

and in Swift:

func makeStrikeThrough(selectedRange: NSRange) {
    let dict = self.getDict(selectedRange)
    self.textView.textStorage.beginEditing()
    textView.textStorage.setAttributes([String() : dict], range: selectedRange)
    self.textView.textStorage.endEditing()
}

objc Part 3: the help-method getDict() to buid a dictionary with the StrikeThrough

- (NSMutableDictionary*) getDict:(NSRange)range
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:[NSNumber numberWithInt:2] forKey:NSStrikethroughStyleAttributeName];
    return dict;
}

and again in Swift

func getDict(range: NSRange) ->  NSMutableDictionary {
    let dict = NSMutableDictionary()
    dict[NSStrikethroughStyleAttributeName] = NSNumber(integer: NSUnderlineStyle.StyleDouble.rawValue)
    return dict
}

I´ve tried to reduce the problem to the root. Perhaps u vote me down ;-) But i need a solution...

Or should i use AttributedStrings?

The procts to download

objc

swift

Upvotes: 1

Views: 130

Answers (1)

Code Different
Code Different

Reputation: 93151

It boils down to one line of code in your Swift function:

func makeStrikeThrough(selectedRange: NSRange) {
    let dict = self.getDict(selectedRange)
    self.textView.textStorage.beginEditing()
    textView.textStorage.setAttributes([String() : dict], range: selectedRange) // error
    self.textView.textStorage.endEditing()
}

it should have been just dict:

func makeStrikeThrough(selectedRange: NSRange) {
    let dict = self.getDict(selectedRange)
    self.textView.textStorage.beginEditing()
    textView.textStorage.setAttributes(dict, range: selectedRange)
    self.textView.textStorage.endEditing()
}

And you need to change your getDict function too:

func getDict() -> [String: AnyObject] {
    return [NSStrikethroughStyleAttributeName: 2]
}

Upvotes: 1

Related Questions