gpt4 book ai didi

ios - 如果发生溢出,如何为 UITextView 设置最大高度和固定宽度并强制省略而不是滚动?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:31 25 4
gpt4 key购买 nike

这是我的目标:

  1. 我想使用 UITextView 而不是 UILabel,因为我希望用户能够选择文本并进行复制。
  2. 我希望 UITextView 在 60 点的高度达到最大值。
  3. 我希望 UITextView 的宽度固定为 300 点。
  4. 我希望 UITextView 在单词上换行。
  5. 假设,根据我提供给它的属性文本字符串,它需要 3 行才能达到 60 点的最大高度。因此,如果我向 UITextView 提供 6 行属性文本,我希望 UITextView 最大显示 60 点并显示 3 行后跟一个省略号(例如 ...)。
  6. 我不希望 TextView 可以滚动。
  7. 如果我向 UITextView 提供单个词作为属性文本,例如“Hello”,我希望 UITextView 仍然具有 300 点的固定宽度,但动态高度可以缩放到尽可能小,大约 20本例中单行文本的点数。
  8. 我希望 UITextView 的内部填充为零。

有什么想法吗?

最佳答案

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andWidth:(CGFloat)width
{
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}

- (void)viewDidLoad
{
// Invoke super
[super viewDidLoad];

// Get text of unknown length
NSMutableAttributedString *myAttributedString = [[NSMutableAttributedString alloc] initWithString:@"String of unknown length here..." attributes:@{NSForegroundColorAttributeName : [UIColor redColor]}];

// Get ellipsis w/ matching attributes
NSDictionary *endCharAttributes = [myAttributedString attributesAtIndex:myAttributedString.length - 1 effectiveRange:NULL];
NSAttributedString *ellipsis = [[NSAttributedString alloc] initWithString:@"..." attributes:endCharAttributes];

// Define size constraints
CGFloat maxHeight = 60;
CGFloat fixedWidth = 300;

// Get starting height
CGFloat textViewHeight = [self textViewHeightForAttributedText:myAttributedString andWidth:fixedWidth];

// Reduce string size and add ellipsis until we fit within our height constraint
while (textViewHeight > maxHeight)
{
NSLog(@"%f", textViewHeight);
NSRange substringRange = {0, myAttributedString.length - 6}; // Reducing by 6 works for my app (strings are never huge)
myAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[myAttributedString attributedSubstringFromRange:substringRange]];
[myAttributedString appendAttributedString:ellipsis];
NSLog(@"myAttributedString = %@", myAttributedString);
textViewHeight = [self textViewHeightForAttributedText:myAttributedString andWidth:fixedWidth];
}

// Init and config UITextView
UITextView *textView = [[UITextView alloc] init];
textView.attributedText = myAttributedString;
textView.frame = CGRectMake(0, 0, fixedWidth, textViewHeight);
[self.view addSubview:textView];
}

有更优雅的解决方案吗?发布它!

更新:您可以通过添加辅助类并实现以下类方法来提高- (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andWidth:(CGFloat)width 的性能:

// Private, gets us to alloc init calculation view one time for life of application
+ (UITextView *)calculationView
{
static UITextView *_calculationView;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_calculationView = [[UITextView alloc] init];
});
return _calculationView;
}

// Public, app calls this a lot
+ (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andWidth:(CGFloat)width usingUIEdgeInset:(UIEdgeInsets)edgeInsets
{
[self calculationView].textContainerInset = edgeInsets;
[self calculationView].attributedText = text;
CGSize size = [[self calculationView] sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}

关于ios - 如果发生溢出,如何为 UITextView 设置最大高度和固定宽度并强制省略而不是滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22594889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com