UITextView带有placeholder
参考:
可以使用第三方
基本原理:重写drawRect:方法,在drawRect:方法中绘制文字
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
if (self.displayPlaceHolder && self.placeholder && self.placeholderColor)
{
if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = self.textAlignment;
[self.placeholder drawInRect:CGRectMake(5, 8 + self.contentInset.top, self.frame.size.width-self.contentInset.left, self.frame.size.height- self.contentInset.top) withAttributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.placeholderColor, NSParagraphStyleAttributeName:paragraphStyle}];
}
else {
[self.placeholderColor set];
[self.placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withFont:self.font];
}
}
}
UITextView in Cell
- Expanding UITextView inside UITableViewCell
- Self-Sizing UITableViewCell with UITextView in iOS 8
- https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/
- UITableViewCell with UITextView height in iOS 7?
根据UITextView的内容计算UITextView的适合大小
How do I size a UITextView to its content?
- (void)textViewDidChange:(UITextView *)textView { CGFloat fixedWidth = textView.frame.size.width; CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; CGRect newFrame = textView.frame; newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height); textView.frame = newFrame; }
可参考的第三方:
HPGrowingTextView中计算TextView的方法如下:
- (CGFloat)measureHeight
{
if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
{
return ceilf([self.internalTextView sizeThatFits:self.internalTextView.frame.size].height);
}
else {
return self.internalTextView.contentSize.height;
}
}
UITextView自适应高度
请参考How do I size a UITextView to its content?
第一种方式,sizeThatFits来计算高度:
例子,是用AutoLayout,在UIViewController中放置一个TextView,有一个textView的高度的约束:
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *heightConstraint;
在代理方法中- (void)textViewDidChange:(UITextView *)textView
,随输入的文字来调整约束:
_heightConstraint.constant = textView.contentSize.height;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
效果如下,可以发现,是有效果的,但并不是很理想,textView有时会突然的变小:
第二种方式,是通过sizeThatFits:方法来计算:
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
NSLog(@"newSize is %@", NSStringFromCGSize(newSize));
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
_heightConstraint.constant = newFrame.size.height;
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
效果如下:
这两种方法,都有一个弊端是,输入的时候,有时文字会上移。
对于这种情况,可以设置_textView.scrollEnabled = NO
即可。
针对第二种情况,我们设置后,来看效果:
这样设置之后,效果就好多了。
UITextView验证输入
验证字符串的长度
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
if (textView.text.length > 50) {
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"Entry Error"
message:@"You must enter less than 50 characters." delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:@"Clear", nil] autorelease];
[av show];
return NO;
}
return YES;
}
也可以通过textField:shouldChangeCharactersInRange:replacementString:
方法,来验证每一个输入的字符。
下面的例子是核实输入的是否是数字:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if ([string isEqualToString:@""]) return YES;
if (textField.tag == SalaryFieldTag) {
unichar c = [string characterAtIndex:0];
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c]) {
return YES;
} else {
return NO;
}
}
return YES;
}