NSTextAlignment ︰ 不能从objective-c 到转换swift

发布时间: 2016/5/8 10:28:55
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我感到非常失望的我,但我不能,把这段代码从目的 c 到 swift .:-(

有人可以帮我吗?我不知道,没有发现任何与谷歌。后沮丧的时间,我乞求你帮助;-)

目的-c-代码: (看起来简单 [里来])

-(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];
}

唯一的行,将是;-(

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

解决方法 1:

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()

}

希望这有助于:D

官方微信
官方QQ群
31647020