UITextView 内的 TTStyledTextLabel
我需要显示包含 HTML 标签等的文本,而 TTStyledTextLabel 符合要求......但它不滚动。
我在 UITextView 中放置了一个,但这拒绝滚动?如果我直接在 UITextView 中输入文本,它会滚动正常,但随后我会看到所有未格式化的 HTML。
有没有办法设置 TTStyledTextLabel 滚动?
谢谢
I have a need to display text that includes HTML tags etc and TTStyledTextLabel fits the bill.....but it does not scroll.
I placed one inside a UITextView but this refuses to scroll? If I enter the text directly in the UITextView it scrolls OK but then I see all the HTML unformatted.
Is there a way to set TTStyledTextLabel to scroll?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
TTStyledTextLabel
放入UIScrollView
中。或者,您可以考虑直接使用
UIWebView
。Try putting the
TTStyledTextLabel
in aUIScrollView
.Alternately, you can consider using a
UIWebView
directly.我终于找到了合适的解决办法...
CGSize constrainSize;
CGSize 字符串大小;
// 允许尺寸过大
constrainSize.width = 300;
约束大小.高度 = 2000;
NSString *s = @"这可以是根据需要长或短的文本...;
UIFont *f = [UIFont fontWithName:@"Arial" size:14];
stringSize =[s sizeWithFont:f constrainedToSize:constrainSize lineBreakMode: UILineBreakModeWordWrap];
// 创建一个标签来容纳文本
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(14, 2, stringSize.width, stringSize.height)];
l.text = s;
[l setNumberOfLines:0]
;
// 现在创建一个 TTStyledTextLabel 以匹配我们刚刚获得的大小
TTStyledTextLabel *tl = [[TTStyledTextLabel alloc] initWithFrame:[l frame]];
// 使用链接等设置文本
tl.text = [TTStyledText textFromXHTML:l.text lineBreaks:YES URLs:YES];
[tl setBackgroundColor:[UIColor clearColor]
]
; (0, 185, 320, 300)];
// 调整滚动视图内容大小以适应TTStyledTextLabel
[sv setContentSize:CGSizeMake(tl.frame.size.width, tl.frame.size.height)];
[sv addSubview:tl];
[self.view addSubview:sv];
现在我可以有一个自动调整大小的 TTStyledTextLabel 滚动;-)
I finally got a suitable work around...
CGSize constraintSize;
CGSize stringSize;
// make an overly big size allowance
constraintSize.width = 300;
constraintSize.height = 2000;
NSString *s = @"this can be text as long or as short as required...;
UIFont *f = [UIFont fontWithName:@"Arial" size:14];
stringSize =[s sizeWithFont:f constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
// create a label to accomodate the text
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(14, 2, stringSize.width, stringSize.height)];
l.text = s;
[l setNumberOfLines:0];
[l sizeToFit];
// now create a TTStyledTextLabel to match the size we just obtained above
TTStyledTextLabel *tl = [[TTStyledTextLabel alloc] initWithFrame:[l frame]];
// set the text making use of links etc
tl.text = [TTStyledText textFromXHTML:l.text lineBreaks:YES URLs:YES];
[tl setBackgroundColor:[UIColor clearColor]];
tl.textColor = [UIColor whiteColor];
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 185, 320, 300)];
// adjust scrollview content size to accomodate the TTStyledTextLabel
[sv setContentSize:CGSizeMake(tl.frame.size.width, tl.frame.size.height)];
[sv addSubview:tl];
[self.view addSubview:sv];
Now I can have an auto sizing TTStyledTextLabel that scrolls ;-)