根据字体和设定宽度计算高度
//自适应字体
- (CGSize)sizeWithString:(NSString*)string font:(UIFont*)font width:(float)width {
CGRect rect = [string boundingRectWithSize:CGSizeMake(width,80000) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
return rect.size;
}
限制高度截取内容
//获取字体高度
- (CGFloat)getTextHeight:(NSString*)string{
CGSize size = CGSizeMake(PKScreenW - 32 - 10, MAXFLOAT);
CGFloat height;
if (string == nil) {
height = [@"高" boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
}
else
{
height = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
}
return height;
}
//获取指定高度所含字符内容
- (NSString*)getTextWithHeight:(CGFloat)heigth andString:(NSString*) str{
//NSString *str = self.textView.text;
NSInteger i = 0 ;
for (; i< str.length; i++) {
NSString * test = [str substringToIndex:i];
CGFloat h = [self getTextHeight:test];
if (h > heigth) {
break;
}
}
PKLog(@"%@",[str substringToIndex:i-1]);
return [str substringToIndex:i-1];
}
动态计算高度和限制行数 并判断字符类型
#pragma mark - UItextViewDelegate代理
- (void)textViewDidChange:(UITextView *)textView{
if ([self.delegate respondsToSelector:@selector(LongPostImageViewCell:DidChangedText:)]) {
NSString * toBeString = textView.text;
NSInteger maxLen = 200 ;
NSDictionary *dic = [NSString StringLengthWithEmoji:toBeString AndMax:maxLen];
NSInteger nowLen = [(NSNumber*)dic[@"allCnt"] integerValue];
NSInteger zHLen = [(NSNumber*)dic[@"useCnt"] integerValue];
//清除字数限制提示标记
if(nowLen < maxLen){
self.isAlreadyShowChineseCharacterToast = false;
}
// 键盘输入模式
NSString *inputMode = [[UIApplication sharedApplication]textInputMode].primaryLanguage;
if ([inputMode isEqualToString:@"zh-Hans"]) {
//获取高亮部分
UITextRange *selectrange = [self.textView markedTextRange];
UITextPosition *position = [textView positionFromPosition:selectrange.start offset:0];
if(!
position){
if (nowLen > maxLen) {
self.textView.text = [toBeString substringToIndex:zHLen];
[self cmpFontLenThenToast];
}
[self fixLineIfOverLimit];
}
} else {
if (nowLen > maxLen) {
self.textView.text = [toBeString substringToIndex:zHLen];
[self cmpFontLenThenToast];
}
[self fixLineIfOverLimit];
}
[self.delegate LongPostImageViewCell:self DidChangedText:self.textView.text];
// 文本框换行-----------------------------------------------
CGFloat nowTextViewHeight = self.getNowTextViewHeight;
if (self.textViewSaveHeight != nowTextViewHeight ) {
self.textViewSaveHeight = nowTextViewHeight;
[self setNeedsLayout];
if ([self.delegate respondsToSelector:@selector(LongPostImageView:DidChangedHeight:)]) {
[self.delegate LongPostImageView:self DidChangedHeight:self.textViewSaveHeight];
}
}
}
[self placeHolderSet];
}
动态改变高度
- (void)textViewDidChange:(UITextView *)textView{
//获取文本中字体的size
CGSize size = [self sizeWithString:textView.text font:QianBaoFont(F3_14) width:textView.width];
NSLog(@"height = %f",size.height);
//获取一行的高度
CGSize size1 = [self sizeWithString:@"Hello" font:QianBaoFont(F3_14) width:textView.width];
NSInteger i = size.height/size1.height;
if (i==1) {
//设置全局的变量存储数字如果换行就改变这个全局变量
self.number = i;
}
if (self.number!=i) {
self.number = i;
NSLog(@"selfnum = %ld",self.number);
self.textView.height = size.height;
[self.tableView reloadRowsAtIndexPaths:@[self.indexPahth] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}
}