Tap*_*ker 5 objective-c ios ios7
我在项目中的要求是UITextView应该根据内容的内容减小字体大小UITextView.所以我想用估计文本的大小boundingRectWithSize.
问题是我得到的字体有点太大,文本的某些部分会被剪裁.
我的职责:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
我试图做同样的事情时遇到了同样的问题.
问题是UITextViewruning的换行与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.left + 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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,CGSizeRound仅仅是一个自定义函数,我写的是圆整width并height的CGSize到最近0.5.
为了便于比较,如果您创建第二个UITextView,并确保textContainer.lineFragmentPadding和textContainerInset是相同的,你应该看到的值几乎相同的最近0.5.
关于计算正确的pointSize的问题,这是一些伪代码:
CGFloat pointSize = 64;
CGFloat minPointSize = 32;
CGFloat decrementor = 4;
CGFloat padding = self.textContainerInset.left + 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;
Run Code Online (Sandbox Code Playgroud)
同样,以上是基于我的实现的伪代码(并不意味着只是替换你所拥有的).希望这可以帮助!
尝试这样
UITextView *textViewObj;//initialise textview.
textViewObj.autoresizesSubviews = NO;
textViewObj.autoresizingMask = UIViewAutoresizingNone;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1006 次 |
| 最近记录: |