【问题标题】:get the position of Text in UITextview获取文本在 UITextview 中的位置
【发布时间】:2014-07-18 11:13:19
【问题描述】:

我有一个带有 CGRect (0,y,320,58) 和居中对齐文本的 UITextView,我想要文本在 UITextView 中的位置

 _________________________
|                         |
|     Hey how are you?    |
|_________________________|

我想要左边的文本的位置/填充我将如何得到它

【问题讨论】:

  • Hay @user1201239 如何获取文本的位置。

标签: ios position uitextfield uitextview


【解决方案1】:

UITextView 采用 UITextInput 协议。该协议具有 -firstRectForRange: 方法,它将告诉您覆盖一系列字符的矩形。

【讨论】:

【解决方案2】:

如果文本在 UIImageView 内的 UILabel 内,那么您可以通过

获取标签的 y 位置
CGFloat yPosition = [self.myLabel.frame.origin.y];

如果没有,请使用标签并将其设置为情节提要中 UIView 中所需的位置。 获取相对于 Imageview 的 Text y 位置。

希望这会有所帮助。

【讨论】:

  • 我认为他的问题在于 UITextView
【解决方案3】:

我认为你可以使用 textView 的这个属性

textContainerInset

textView.textContainerInset = UIEdgeInsetsMake(TOP, LEFT, BOTTOM, RIGHT);

【讨论】:

  • 这是设置 containerInset 的位置,而我想检索它
【解决方案4】:

试试这可能会有所帮助...

UITextView *textStuff = [[UITextView alloc] init];
textStuff.frame = CGRectMake(2.0, 200.0, 200.0, 40.0);
textStuff.text = @"how are you today?";
textStuff.textColor = [UIColor blackColor];

UITextPosition *Pos2 = [textStuff positionFromPosition: textStuff.endOfDocument offset: nil];
UITextPosition *Pos1 = [textStuff positionFromPosition: textStuff.endOfDocument offset: -textStuff.text.length];

UITextRange *range = [textStuff textRangeFromPosition:Pos1 toPosition:Pos2];

CGRect result1 = [textStuff firstRectForRange:(UITextRange *)range ];

NSLog(@"%f, %f", result1.origin.x, result1.origin.y);

UIView *view1 = [[UIView alloc] initWithFrame:result1];
view1.backgroundColor = [UIColor colorWithRed:0.2f green:0.5f blue:0.2f alpha:0.4f];
[textStuff addSubview:view1];

[self.view addSubview:textStuff];

打印到“CGRect result1”并查看它正在返回文本发布...

【讨论】:

    【解决方案5】:

    好吧,我就是这样做的,

    UITextPosition *post = [_textview beginningOfDocument];
    
    
    CGRect r = [_textview caretRectForPosition:post];
    

    【讨论】:

      猜你喜欢
      • 2014-10-08
      • 2023-03-14
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      相关资源
      最近更新 更多