如何在swift中使用NSWritingDirectionAttributeName?

时间:2015-11-18 16:49:07

标签: ios objective-c swift

如何在swift中使用NSWritingDirectionAttributeName?

我在Obj-c中有这段代码:

attrString = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont fontAwesomeFontOfSize:24], NSForegroundColorAttributeName:kHJColorIcon, NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]}];

效果很好!

现在我需要在另一个地方使用相同的代码,但语言很快。

我尝试了几种方法但是我遇到了错误。 enter image description here

2 个答案:

答案 0 :(得分:3)

NSWritingDirectionAttributeName需要数值,因此请使用原始值

[NSWritingDirectionAttributeName : [NSWritingDirection.LeftToRight.rawValue | NSTextWritingDirection.Override.rawValue]]

答案 1 :(得分:2)

NSWritingDirectionAttributeName需要数字值,因此在您的函数中使用rawValue,请尝试此操作

let attrString = NSMutableAttributedString(string: "Karthik Test",
                 attributes: [NSFontAttributeName: UIFont.systemFontOfSize(24),
                              NSWritingDirectionAttributeName:[NSWritingDirection.RightToLeft.rawValue |
                                                               NSTextWritingDirection.Override.rawValue]])