Ios UITextView内部UIScrollView不滚动-目标c
我试图使用UITextView以动态高度显示消息,但当消息足够长以覆盖屏幕时,我无法获得滚动功能。我尝试过许多在线解决方案,但都失败了。我使用Ios UITextView内部UIScrollView不滚动-目标c,ios,objective-c,xcode,uiscrollview,Ios,Objective C,Xcode,Uiscrollview,我试图使用UITextView以动态高度显示消息,但当消息足够长以覆盖屏幕时,我无法获得滚动功能。我尝试过许多在线解决方案,但都失败了。我使用nsmutableAttributeString格式化文本 这是我最后一次尝试: NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[self.descString dataUsingEncoding:NSUTF8StringEncod
nsmutableAttributeString
格式化文本
这是我最后一次尝试:
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[self.descString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
__block BOOL found = NO;
[attrStr beginEditing];
[attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:16];
[attrStr addAttribute:NSFontAttributeName value:newFont range:range];
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor darkGrayColor]
range:range];
found = YES;
}
}];
if (!found) {
// No font was found - do something else?
}
[attrStr endEditing];
CGRect labelSize = [attrStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
self.textviewHeight.constant = labelSize.size.height;
在Interface Builder中,我使用了一个UIScrollView,在它里面我放置了一个UIView,在UIView里面我添加了一个UITextView<代码>文本视图高度是UITextView的高度约束。我不敢相信像这样一件简单的事情花了我这么多小时,我仍然没有解决办法。请帮忙
我尝试过但失败的其他解决方案: 一, 二,
如果更容易实现,我不介意使用UILabel。如果我了解您试图实现的目标,您不需要进行任何编程高度计算 我的最佳猜测是,您正在为TextView设置底部约束(即限制它滚动其完整内容) 尝试将TextView作为标准UIView的子视图,并设置该视图的前导/尾随/底部/顶部约束(对于您希望TextView显示的总区域),然后将TextView置于其中,并设置前导/尾随/顶部而非底部(我假设您希望它能够垂直滚动)约束,它会抱怨没有高度/y约束,因此请继续,将centerY设置为与保持其centerY的视图相等(即视图中的垂直中心) 我附加了一个约束的示例(我添加了上面和下面的视图,只是为了显示它可以基于包含约束的视图约束到一个区域) 您的代码如下所示,并进行了细微调整:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nullable) IBOutlet UITextView *scrollingTextView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *reallyLongString = @"Really Long String here";
// your code to adjust it below
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[reallyLongString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
__block BOOL found = NO;
[attrStr beginEditing];
[attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:23];
[attrStr addAttribute:NSFontAttributeName value:newFont range:range];
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor darkGrayColor]
range:range];
found = YES;
}
}];
if (!found) {
// No font was found - do something else?
}
[attrStr endEditing];
[self.scrollingTextView setAttributedText:attrStr];
// Do any additional setup after loading the view, typically from a nib.
}
@end
有点明显。。。但仍然如此。您是否意外取消选中IB中的滚动启用复选框?是的。你是说在UITextView上,对吧?是的。所以检查一下?我已经尝试了所有可能的组合与此选项。什么都没用,组合?当您单击属性检查器中的UITextView时,序列图像板上将出现一个名为“已启用滚动”的框。是“开”还是“关”?只有一个问题。我需要为UITextView设置背景色,但颜色必须仅位于文本后面。当我使用你的方法时,如果文本很小,背景色会覆盖整个屏幕。有没有办法解决这个问题?@学生,我不知道我是否了解问题所在。您可以将UITextView(scrollingTextView)的背景色设置为所需的颜色,也可以设置容纳它的UIView的背景色,然后将UITextView(scrollingTextView)的背景色设置为清除。如果两者都不起作用,那么看看问题所在的屏幕截图会有所帮助。
CGRect labelSize = [attrStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width * 0.97, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
CGRect frame = self.descView.frame;
frame.size.height = labelSize.size.height;
self.descView.frame = frame;
#import "ViewController.h"
@interface ViewController ()
@property (strong, nullable) IBOutlet UITextView *scrollingTextView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *reallyLongString = @"Really Long String here";
// your code to adjust it below
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[reallyLongString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
__block BOOL found = NO;
[attrStr beginEditing];
[attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [oldFont fontWithSize:23];
[attrStr addAttribute:NSFontAttributeName value:newFont range:range];
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor darkGrayColor]
range:range];
found = YES;
}
}];
if (!found) {
// No font was found - do something else?
}
[attrStr endEditing];
[self.scrollingTextView setAttributedText:attrStr];
// Do any additional setup after loading the view, typically from a nib.
}
@end