我遇到了UILabels的.center属性问题。我需要根据光标在textView中的位置移动标签。我的代码如下:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if([[textView text] length] > 160){
return NO;
}
//CharacterLimitLabel.text = [NSString stringWithFormat:@"%u", 160 - [[textView text] length]];
[self setCharacterLabelPosition];
return YES;
}
-(void)setCharacterLabelPosition{
CGPoint cursorPosition = [CreateTextView caretRectForPosition:CreateTextView.selectedTextRange.start].origin;
cursorPosition.x = cursorPosition.x + CreateTextView.frame.origin.x + 45;
cursorPosition.y = cursorPosition.y + CreateTextView.frame.origin.y + 15;
CharacterLimitLabel.center = cursorPosition;
}
这在很大程度上起作用,但是,当我取消注释标签的设置时,标签不再改变其中心。