UItextView阿拉伯语文本右对齐

Kha*_*aus 8 iphone objective-c uitextview ios inputview

在UItextView inputView上使用我的自定义阿拉伯语键盘,我用阿拉伯语文本填充我的textView但是无法将书面文本对齐到右边....需要帮助将文本对齐到右边.

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{

 if(showCustomKeyboard==NO){
 [textView resignFirstResponder];
 textView.inputView=nil;
 [textView becomeFirstResponder];
 return YES; 
}

 else{
     [textView resignFirstResponder];
     if(customKeyboard==nil){
     customKeyboard=[[CustomKeyboard alloc] initWithFrame:CGRectMake(0, 264, 320, 216)];
     [customKeyboard setDelegate:self];
 } 
 if([[UIApplication sharedApplication] respondsToSelector:@selector(inputView)]){
     if (textView.inputView == nil) {
           textView.inputView = customKeyboard;
           [textView becomeFirstResponder];
     }
 } 
    self.customKeyboard.currentField=textView;
    [textView becomeFirstResponder];
 }
 return YES; 
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*ton 17

您可以使用setBaseWritingDirection选择器设置UITextView的写入方向:

UITextView *someTextView = [[UITextView] alloc] init];
[someTextView setBaseWritingDirection:UITextWritingDirectionLeftToRight forRange:[someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]]];
Run Code Online (Sandbox Code Playgroud)

代码有点棘手,因为UITextView支持文本的不同部分具有不同的书写方向.在我的例子中,我使用[someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]]来选择UITextView的全文范围.如果您的需求不同,您可以调整该部分.

您可能还想检查UITextView中的文本是否为LTR到RTL.你可以这样做:

if ([someTextView baseWritingDirectionForPosition:[someTextView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionLeftToRight) {
    // do something...
}
Run Code Online (Sandbox Code Playgroud)

请注意,我使用[someTextView beginOfDocument]指定了文本的开头,并使用UITextStorageDirectionForward向前搜索.您的需求可能不同.

如果您继承UITextView,那么用"self"而不是"someTextView"替换所有这些代码示例.

我建议在http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextInput_Protocol/Reference/Reference.html上阅读UITextView符合的UITextInput协议.

关于在iOS 5.1或更早版本中使用textAlignment属性的警告:如果您将此方法与设置基本书写方向一起使用,则会出现问题,因为在UITextView中左对齐时的RTL文本实际上在视觉上与右侧对齐.将RTL书写方向设置为右对齐将使其与UITextView的左侧对齐.