如何截断UITextView的内容,以适应缩小的大小?
我很难解决这个问题。
正如标题所示,我在iPhone应用程序的视图上有几个UITextViews。 我以编程方式创build它们,并成功地填充文本,但在某些情况下,我放在视图中的文本占用比我分配的框架更多的空间。 在这种情况下,我想文本被截断,但我不知道如何做到这一点。
我已经预定义了以下常量;
#define viewOriginX 20 #define viewOriginY 180
这是我的UITextView创build代码;
textViewOne = [[UITextView alloc] initWithFrame:CGRectMake(viewOriginX, viewOriginY + 65, 280, 45]; textViewOne.delegate = self; textViewOne.scrollEnabled = NO; textViewOne.contentInset = UIEdgeInsetsZero; textViewOne.font = viewFont; textViewOne.textColor = [UIColor blackColor]; textViewOne.textAlignment = UITextAlignmentLeft; [self.view addSubview:textViewOne];
在某些情况下,我在这里有15到20行文本,我想截断它到2行。
任何人都可以帮助我正确的方向吗?
提前致谢! :d
你可以做一个字符数。 例如,如果你有像这样的UITextField:
+--------------------+ |This is a string in | |a text view. | +--------------------+
你有每行20个字符的东西。 如果你知道这个数字,你可以通过-substringToIndex:
方法简单地截断你的string。
int maxCharacters = 40; // change it to your max if([myString length] > maxCharacters) { myString = [myString substringToIndex:maxCharacters]; }
你也可以考虑一下UILabel
。 由于您只需要两行文本,UILabel的numberOfLines
属性可以解决您的问题。
原答复(iOS 6及以下)
不幸的是,如果你需要视图是可编辑的,带有numberOfLines
UILabel
不会这样做。 或者你想UITextView
的(本地)垂直alignment。
这是一个NSString
类别,它根据string的大小从string中删除单词:
@interface NSString (StringThatFits) - (NSString *)stringByDeletingWordsFromStringToFit:(CGRect)rect withInset:(CGFloat)inset usingFont:(UIFont *)font @end @implementation NSString (StringThatFits) - (NSString *)stringByDeletingWordsFromStringToFit:(CGRect)rect withInset:(CGFloat)inset usingFont:(UIFont *)font { NSString *result = [self copy]; CGSize maxSize = CGSizeMake(rect.size.width - (inset * 2), FLT_MAX); CGSize size = [result sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; NSRange range; if (rect.size.height < size.height) while (rect.size.height < size.height) { range = [result rangeOfString:@" " options:NSBackwardsSearch]; if (range.location != NSNotFound && range.location > 0 ) { result = [result substringToIndex:range.location]; } else { result = [result substringToIndex:result.length - 1]; } size = [result sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; } return result; } @end
对于UITextView
,使用8的插图来说明UIKit绘制它们的方式:
CGRect rect = aTextView.frame; NSString *truncatedString = [theString stringByDeletingWordsFromStringToFit:rect withInset:8.f usingFont:theTextView.font];
更新的答案(iOS 7)
现在UITextView
在内部使用TextKit,它更容易。
而不是截断实际的string,将text
(或attributedText
text
)属性设置为整个string,并截断容器中显示的文本的数量(就像我们用UILabel
):
self.textView.scrollEnabled = NO; self.textView.textContainer.maximumNumberOfLines = 0; self.textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
因为sizeWithFont:constrainedToSize:lineBreakMode:
已经在iOS 7中被弃用了,所以我做了一些改变:
- (NSString *)stringByDeletingWordsFromStringToFit:(CGRect)rect withInset:(CGFloat)inset usingFont:(UIFont *)font { NSString *result = [self copy]; CGSize maxSize = CGSizeMake(rect.size.width - (inset * 2), FLT_MAX); if (!font) font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; CGRect boundingRect = [result boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font, } context:nil]; CGSize size = boundingRect.size; NSRange range; if (rect.size.height < size.height) while (rect.size.height < size.height) { range = [result rangeOfString:@" " options:NSBackwardsSearch]; if (range.location != NSNotFound && range.location > 0 ) { result = [result substringToIndex:range.location]; } else { result = [result substringToIndex:result.length - 1]; } if (!font) font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; CGRect boundingRect = [result boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font, } context:nil]; size = boundingRect.size; } return result;
}
这里有一个我写的截断string(通过单词)以适应特定的宽度和高度的代码的和平:
- (NSString *)stringByTruncatingString:(NSString *)string toHeight:(CGFloat)height maxWidth:(CGFloat)width withFont: (UIFont *)font { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil]; NSMutableString *truncatedString = [string mutableCopy]; if ([string sizeWithAttributes: @{NSFontAttributeName: font}].height > height) { // this line is optional, i wrote for better performance in case the string is too big if([truncatedString length] > 150) truncatedString = [[truncatedString substringToIndex:150] mutableCopy]; // keep removing the last word until string is short enough while ([truncatedString boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size.height > height) { NSRange wordrange= [truncatedString rangeOfString: @" " options: NSBackwardsSearch]; truncatedString = [[truncatedString substringToIndex: wordrange.location] mutableCopy]; } // add elipsis to the end truncatedString = [NSMutableString stringWithFormat:@"%@...",truncatedString]; } return truncatedString; }
用法:
NSString *smallString = [self stringByTruncatingString:veryBigString toHeight:65 maxWidth:200 withFont:[UIFont systemFontSize:14.0f]];