8

在 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; 
}
4

6 回答 6

17

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

UITextView *someTextView = [[UITextView] alloc] init];
[someTextView setBaseWritingDirection:UITextWritingDirectionLeftToRight forRange:[someTextView textRangeFromPosition:[someTextView beginningOfDocument] toPosition:[someTextView endOfDocument]]];

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

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

if ([someTextView baseWritingDirectionForPosition:[someTextView beginningOfDocument] inDirection:UITextStorageDirectionForward] == UITextWritingDirectionLeftToRight) {
    // do something...
}

请注意,我使用 [someTextView beginningOfDocument] 指定了文本的开头,并使用 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 的左侧对齐。

于 2012-06-02T02:23:51.363 回答
4

试试textAlignment属性。

textView.textAlignment = UITextAlignmentRight;

看看UITextView 类参考

编辑:也许CATextLayer可以帮助你,有人建议使用这个类来自定义文本,但我从来没有亲自使用过它......

否则,您可以强制您的 textView 在UITextFieldDelegate方法中反转您的输入:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

每当用户在文本字段中键入新字符或删除现有字符时,文本字段都会调用此方法。在这里,您可以用新的 NSString 替换您的输入,您可以在其中从右到左放置字符。

希望这是有道理的...

啊...不要忘记设置

textView.textAlignment = UITextAlignmentRight;

将提示移到右侧。

于 2012-03-26T09:55:29.753 回答
1

试试这个代码:

yourtextview.textAlignment = UITextAlignmentRight;

希望这对您有所帮助。

于 2012-03-26T09:58:17.977 回答
0

此处或任何其他帖子中没有人提到的事情是确保您没有为 TextView 调用 sizeToFit。它简单地将 textView(不是文本)向左对齐,从而产生文本从左到右而不是从右到左的错觉。

于 2015-10-10T11:42:01.393 回答
0

如果您从 Storyboard 创建 UI,则对 Lead 或 Trailing 空格的设置约束以及 First Item 的值将是Respect Language Direction

于 2015-12-28T07:01:00.873 回答
0

迅速你可以使用这个代码

textView.makeTextWritingDirectionRightToLeft(true)
于 2019-07-23T06:55:32.527 回答