【问题标题】:UITextView alignment is changing on its ownUITextView 对齐方式正在自行改变
【发布时间】:2012-07-16 08:18:19
【问题描述】:

我有一个带有 RTL 对齐的UITextView。

如果用户输入文本并关闭键盘,则对齐保持 - RTL。

但如果用户选择空字符串,我将字符串更改为占位符,对齐翻转为 LTR。

我试图明确要求 RTL,但没有帮助。

-(void)cancelPad{
    [userTextView resignFirstResponder];
    userTextView.textAlignment = UITextAlignmentRight;
    userTextView.text = @"place holder text";
}

-(void)doneWithNumberPad{
    if ([userTextView.text isEqualToString:@""]) {
        [self cancelPad];
    }
    [userTextView resignFirstResponder];
    userTextView.textAlignment = UITextAlignmentRight;
}

有人有什么想法吗?

【问题讨论】:

    标签: iphone objective-c xcode uitextview right-to-left


    【解决方案1】:

    代替:

    userTextView.text = ""
    

    你应该输入:

    userTextView.text = [NSString stringWithFormat:@"\u202b"];
    

    这是“开始 RTL”的 UTF-8 字符

    【讨论】:

    • (尽管这仍需要更多的调整,但它应该可以帮助您入门)
    【解决方案2】:

    最后我只是添加了一个带有占位符文本的标签。 当键盘显示时,placeholderLabel 被隐藏。 如果键盘以空文本消失,则 placeholderLabel 将再次显示。 如果有更合适的解决方案,请不要犹豫在此处发布。

    【讨论】:

      【解决方案3】:

      首先: 在 .h 文件中添加这个

      UIViewController<UITextFieldDelegate>
      

      将您的文本字段分配给此

      userTextView.delegate =self;
      

      然后使用此方法并将您的代码放入

      -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
      
      
       }     
      -(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
      
      }
      -(BOOL) textFieldShouldReturn:(UITextField *)textField{
      
      }
      

      例子:

      -(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
      
      if ([userTextView.text isEqualToString:@""]) {
          [self cancelPad];
      }
      [userTextView resignFirstResponder];
      userTextView.textAlignment = UITextAlignmentRight;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多