Ulli H
Ulli H

Reputation: 1888

NSTextAlignment: can't translate from objective-c to swift

I'm very disappointed of me, but Im not able, to translate this code from objective-c to swift. :-(

May someone help me? I've no idea and found nothing with Google. After a frustrated hours, i beg you for help ;-)

The objective-c - code: (looks simple [grrrrr])

-(void)setParagraphAlignment:(NSTextAlignment)newAlignment{
    NSRange selectedRange = [_textView selectedRange];
    NSMutableParagraphStyle *newParagraphStyle = [[NSMutableParagraphStyle alloc] init];
    [newParagraphStyle setAlignment:newAlignment];

    NSDictionary *dict = @{NSParagraphStyleAttributeName: newParagraphStyle};
    [textView.textStorage beginEditing];
    [textView.textStorage setAttributes:dict range:selectedRange];
    [textView.textStorage endEditing];
}

The only lines, i could transform are ;-(

textView.textStorage .beginEditing()
textView.textStorage .endEditing()

Upvotes: 2

Views: 179

Answers (1)

Charlie Hall
Charlie Hall

Reputation: 577

func selectParagraphAlignment(newAlignment:NSTextAlignment) {
    var selectedRange = textView.selectedRange
    var newParagraphStyle = NSMutableParagraphStyle()
    newParagraphStyle.alignment = newAlignment

    var dict = [NSParagraphStyleAttributeName: newParagraphStyle]
    textView.textStorage.beginEditing()
    textView.textStorage.setAttributes(dict, range: selectedRange)
    textView.textStorage.endEditing()

}

hope this helps :D

Upvotes: 3

Related Questions