简体   繁体   English

在iOS7上自定义UITextView占位符对齐方式

[英]Custom UITextView Placeholder Alignment on iOS7

I've created a custom UITextView in order to show a Placeholder. 我已经创建了一个自定义的UITextView来显示占位符。 The method I used was drawInRect on the TextChangedNotification. 我使用的方法是TextChangedNotification上的drawInRect。 It mostly works fine and a Placeholder is displayed. 它大部分工作正常,并显示占位符。

The problem is that the Placeholder is positioned to the top left of the UITextView. 问题是占位符位于UITextView的左上角。 What I would like is to position it after the cursor. 我想要的是在光标后定位它。

The only solution I have found so far is to set the X and Y coordinates for the CGRect passed into drawInRect to pad the content ie 到目前为止我找到的唯一解决方案是设置传递给drawInRect的CGRect的X和Y坐标来填充内容,即

CGRectMake(6,Font.PointSize/2,Width,Height) // X value is hardcoded

This seems like a bit of a hack. 这看起来有点像黑客。

Is there a better way position the placeholder text after the cursor ? 是否有更好的方法在光标后定位占位符文本?

在此输入图像描述

你应该能够用这个检索插入矩形

CGRect caretRect = [textView caretRectForPosition:textView.beginningOfDocument];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM