简体   繁体   English

如何将UILabel放在UITextField的最后一个字符旁边?

[英]How to place UILabel next to last character of UITextField?

I am trying to achieve the same effect as Facebook's new status field: place something next to the last character which cannot be modified or selected by the user (for example, a tag: "with Joh Doe"). 我试图获得与Facebook的新状态字段相同的效果:在用户无法修改或选择的最后一个字符旁边放置一些内容(例如,标签:“with Joh Doe”)。

What's the best way of achieving it? 实现它的最佳方法是什么?

Thanks 谢谢

Nick 缺口

截图

An easier solution would be to simply make the last n characters in the TextView not editable. 一个更简单的解决方案是简单地使TextView中的最后n个字符不可编辑。 Then let the TextView handle moving and wrapping the text as needed. 然后让TextView句柄根据需要移动和包装文本。 I would guess that is what Facebook is doing, just with some attributed text at the end. 我猜这就是Facebook正在做的事情,最后只是一些归属文本。

In your ViewControllers.h, make sure the viewController conforms to the UITextViewDelegate as follows. 在ViewControllers.h中,确保viewController符合UITextViewDelegate,如下所示。

@interface ViewController : UIViewController <UITextViewDelegate>

Then in the viewDidLoad: method, set the delegate for the UITextView to the ViewController. 然后在viewDidLoad:方法中,将UITextView的委托设置为ViewController。 You can also add your fixed text. 您还可以添加固定文本。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.myTextView.delegate = self;
    self.myTextView.text = " posted by Mike";
}

Then, we will use the shouldChangeCharactersInRange: method to prevent the user from editing the text at the end you want to preserve. 然后,我们将使用shouldChangeCharactersInRange:方法来阻止用户编辑要保留的末尾的文本。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    BOOL deleteKeyPressed = ([text length] == 0 && range.length > 0);

    if( range.location > (textView.text.length - @" - posted by Mike".length) || (deleteKeyPressed && range.location > (textView.text.length - @" - posted by Mike".length)) )
    {
        return NO;
    }
    return YES;
}

I think this should get you started and make you life easier than finding the end of the last text, then putting another view at the end that may need to wrap if there is not enough room to fit it. 我认为这应该让你开始,让你的生活比找到最后一个文本的结尾更容易,然后在最后放置另一个视图,如果没有足够的空间来适应它可能需要包装。

So to prevent the user from selecting the "reserved" text, add the following delegate method to your ViewController's .m: 因此,为了防止用户选择“保留”文本,请将以下委托方法添加到ViewController的.m中:

-(void) textViewDidChangeSelection:(UITextView *)textView
{
    if( textView.selectedRange.location > (textView.text.length - @" - posted by Mike".length) )
    {
        [textView setSelectedRange:NSMakeRange((textView.text.length - @" - posted by Mike".length), 0 )];
    }
}

You'll still need to handle if the user selects all the text, and chooses to "cut" or "paste", but that should just be another special case in shouldChangeTextInRange . 如果用户选择所有文本,您仍然需要处理,并选择“剪切”或“粘贴”,但这应该只是shouldChangeTextInRange另一个特例。 I'll let you figure that one out - shouldn't be hard. 我会让你想出那一个 - 不应该很难。 Certainly a lot easier than trying to dynamically place, size, and wrap a TextView within another TextView. 当然比尝试在另一个TextView中动态放置,调整和包装TextView要容易得多。

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

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