NSAttributedString写作方向

时间:2014-04-18 17:10:07

标签: ios nsattributedstring right-to-left

我正在尝试更改NSAttributedString中的写入方向。但是,我很难搞清楚如何去做。

CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"ArialRoundedMTBold", 16, NULL);

NSDictionary *attrDictionary = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,(NSString *)kCTFontAttributeName, nil];
CFRelease(fontRef);

NSAttributedString *attString=[[NSAttributedString alloc] initWithString:self.stringMap attributes:attrDictionary];

self.attString = attString;

这是我初始化我的NSAttributedString的代码,我已经读过常量 kCTWritingDirectionRightToLeft ,我觉得我必须把它放在某个地方,但我可以弄清楚在哪里和如何。

有人有任何建议吗?

1 个答案:

答案 0 :(得分:6)

我仔细阅读了文档。

我会使用NSFontAttributeNameNSWritingDirectionAttributeName,因为我比使用所有bridge内容更方便,而且还使用简写语法。

所以NSWritingDirectionAttributeName等待NSArray个NSNumbers。那就是问题所在。 其中一个数字必须是 NSWritingDirection LeftToRight RightToLeft ),另一个是 NSTextWritingDirection 嵌入覆盖)。

所以,可能的组合是(我认为你正在寻找第四个):

NSDictionary *attrDictionary = @{NSFontAttributeName:font,
                                 NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]};

NSDictionary *attrDictionary = @{NSFontAttributeName:font,
                                 NSWritingDirectionAttributeName:@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]};

NSDictionary *attrDictionary = @{NSFontAttributeName:font,
                                 NSWritingDirectionAttributeName:@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]};

NSDictionary *attrDictionary = @{NSFontAttributeName:font,
                                 NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]};

Source: Documentation of NSWritingDirectionAttributeName