问题是我得到的字体有点太大,文本的某些部分会被剪裁.
我的职责:
-(BOol)updateTextVIEwFontSizeforText:(Nsstring*)text{ float FontSize = self.maximumFontSizeInPoints; self.Font = [self.Font FontWithSize:FontSize]; CGSize tallerSize ; CGSize stringSize ; do { if (FontSize <= self.minimumFontSizeInPoints) // it just won't fit return NO; FontSize -= 1.0; self.Font = [self.Font FontWithSize:FontSize]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setlineBreakMode:NSlineBreakByWorDWrapPing]; NSDictionary *attributes = @{ NSFontAttributename: self.Font,NSParagraphStyleAttributename : paragraphStyle }; tallerSize = CGSizeMake(self.frame.size.wIDth,self.frame.size.height-16);// the 16 is given because uitextvIEw adds some offset stringSize = [text boundingRectWithSize:CGSizeMake(self.contentSize.wIDth,CGfloat_MAX) options:NsstringDrawingUseslineFragmentOrigin | NsstringDrawingUsesFontLeading attributes:attributes context:nil].size; }while(stringSize.height >= tallerSize.height); if ([self.onTextChangDelegate respondsToSelector:@selector(onTextChangDelegate)]) { [self.onTextChangDelegate onTextChanged:text]; } return YES;}解决方法 我试图做同样的事情时遇到了同样的问题.
问题是UITextVIEw与boundingRectWithSize相比如何运行其换行符.您可以在此处阅读更多详细信息:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextLayout/Concepts/CalcTextLayout.html
但你实际上可以计算出确切的大小! UITextVIEw基本上有两个属性,您需要考虑这些属性才能获得正确的大小估计值.第一个是textContainer.lineFragmentpadding,第二个是textContainerInset.
首先,textContainer.lineFragmentpadding:您可能已经注意到您的大小调整通常总是偏离10px,这是因为系统默认值是5px.当您计算估计的大小时,您需要从您检查的大小中减去此值,并在获得最终值时将其重新添加.
二,textContainerInset.这是一个UIEdgeInset,您需要将其添加回最终计算值以匹配系统.
这是基于我如何解决问题的代码:
- (CGSize)sizeThatFits:(CGSize)size CGfloat lineFragmentpaddings = self.textContainer.lineFragmentpadding * 2; CGfloat horzpadding = self.textContainerInset.@R_403_6823@ + self.textContainerInset.right + lineFragmentpaddings; CGfloat vertpadding = self.textContainerInset.top + self.textContainerInset.bottom; size.wIDth -= horzpadding; CGRect boundingRect = [attributedText boundingRectWithSize:size options:NsstringDrawingUseslineFragmentOrigin context:nil]; size = boundingRect.size; // I found through deBUGging that adding 0.25 rounded // matches sizeThatFits: IDentically. Not sure why… size.wIDth += horzpadding + 0.25; size.height += vertpadding + 0.25; size = CGSizeRound(size); return size;}
注意,CGSizeRound只是我编写的一个自定义函数,它将CGSize的宽度和高度四舍五入到最接近的0.5.
为了进行比较,如果您创建第二个UITextVIEw,并确保textContainer.lineFragmentpadding和textContainerInset相同,您应该看到几乎与最接近的0.5相同的值.
关于计算正确的pointSize的问题,这是一些伪代码:
CGfloat pointSize = 64;CGfloat minPointSize = 32;CGfloat decrementor = 4;CGfloat padding = self.textContainerInset.@R_403_6823@ + self.textContainerInset.right + lineFragmentpaddings;CGfloat actualWIDth = self.maxTextVIEwSize.wIDth - padding * 2;CGRect boundingRect = CGRectZero;BOol isValIDPointSize = NO;do { if (pointSize < minPointSize) { pointSize = minPointSize; boundingRect.size.height = self.maxTextVIEwSize.height; isValIDPointSize = YES; } else { NSDictionary *defaultAttributes = [self.customTextStorage defaultAttributesForPointSize:pointSize]; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:string attributes:defaultAttributes]; boundingRect = [attrString boundingRectWithSize:CGSizeMake(actualWIDth,1024) options:NsstringDrawingUseslineFragmentOrigin context:nil]; // is the height to big? if (boundingRect.size.height > self.maxTextVIEwSize.height) { // reduce the point size for next iteration of loop pointSize -= decrementor; } // passes height test else { isValIDPointSize = YES; } }} while (!isValIDPointSize);return pointSize;
同样,以上是基于我的实现的伪代码(并不意味着只是替换你所拥有的).希望这可以帮助!
总结以上是内存溢出为你收集整理的ios – boundingRectWithSize不复制UITextView全部内容,希望文章能够帮你解决ios – boundingRectWithSize不复制UITextView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)