- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我在 UIScrollView
中有一个 UITextView
,它在从 xcode 4.x
构建的 iOS 6
上工作得很好>,但是现在使用 xcode 5
构建它无法正常工作,即使在 iOS 6
上也是如此。
问题是即使 UITextView
和 UIScrollView
的宽度很大,文本也会随着屏幕宽度换行。我使用这段代码计算出 UITextView
的新宽度和高度,即使 TextView 向左/向右滚动,文本也会被换行,就好像宽度只是屏幕的宽度一样。
谢谢
self.responceTextView.text = [NSString stringWithFormat:@"%@%@",_responceTextView.text,responce];
[self textViewDidChange:self.responceTextView];
- (void)textViewDidChange:(UITextView *)textView
{
// Recalculate size of text field
CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
CGSize reqSize = [textView.text sizeWithFont:[UIFont fontWithName:@"Courier" size:12] constrainedToSize:maxSize lineBreakMode:NSLineBreakByClipping];
self.responceTextView.frame = CGRectMake(0, 0, reqSize.width+16, reqSize.height+16);
// Resize scroll view if needs to be smaller so text stays at top of screen
CGFloat maxScrollHeight = maxScrollViewSize.size.height;
if (self.responceTextView.frame.size.height < maxScrollHeight) {
self.responceScrollView.frame = CGRectMake(self.responceScrollView.frame.origin.x, self.responceScrollView.frame.origin.y, self.responceScrollView.frame.size.width, self.responceTextView.frame.size.height);
} else {
self.responceScrollView.frame = maxScrollViewSize;
}
// Set content size
self.responceScrollView.contentSize = CGSizeMake(self.responceTextView.frame.size.width, self.responceTextView.frame.size.height);
[self scrollToCursor];
}
编辑----
好的,看来 sizeWithFont
在 iOS 7 中被弃用了。很奇怪我怎么没有收到编译器警告。它在 iOS 6 上不起作用仍然没有意义(或者在使用 iOS 7 SDK 构建时它是否被完全删除?)
我已经尝试了这 2 个备选方案,但从所有备选方案中得到的尺寸完全相同。
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Courier" size:12], NSFontAttributeName,
nil];
CGRect rect = [textView.text boundingRectWithSize:maxSize options:NSLineBreakByClipping | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];
返回:{{0, 0}, {439.27148, 168}}
CGSize rect2 = [textView.text sizeWithAttributes:attributes];
返回:{439.27148, 168}
上面的原始返回 {439.27148, 168}
他们都应该返回更广阔的视野。
编辑 2 ----从上面看来,返回的框架是正确的(439 宽),但它是仍在 TextView 中进行换行的文本。
最佳答案
尝试使用:
[textView.text boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil];
字符串测量似乎有问题。对于我所做的测试,这是提供正确大小的唯一选项组合。
我在 iOS7 中成功使用了以下代码(它是一个具有最小和最大高度的 UITextField
。当文本的高度大于 MAX_HEIGHT_MESSAGE_TEXTBOX
时,会出现滚动条在 UITextField
中)。
const float MAX_HEIGHT_MESSAGE_TEXTBOX = 80;
const float MIN_HEIGHT_MESSAGE_TEXTBOX = 30;
- (void)setFrameToTextSize:(CGRect)txtFrame textView:(UITextView *)textView
{
if(txtFrame.size.height > MAX_HEIGHT_MESSAGE_TEXTBOX)
{
//OK, the new frame is to large. Let's use scroll
txtFrame.size.height = MAX_HEIGHT_MESSAGE_TEXTBOX;
textView.scrollEnabled = YES;
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
}
else
{
if (textView.frame.size.height < MIN_HEIGHT_MESSAGE_TEXTBOX) {
//OK, the new frame is to small. Let's set minimum size
txtFrame.size.height = MIN_HEIGHT_MESSAGE_TEXTBOX;
}
//no need for scroll
textView.scrollEnabled = NO;
}
//set the frame
textView.frame = txtFrame;
}
- (void)setframeToTextSize:(UITextView *)textView animated:(BOOL)animated
{
//get current height
CGRect txtFrame = textView.frame;
//calculate height needed with selected font. Note the options.
//append a new line to make space for the cursor after user hit the return key
txtFrame.size.height =[[NSString stringWithFormat:@"%@\n ",textView.text]
boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil].size.height;
if (animated) {
//set the new frame, animated for a more nice transition
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut |UIViewAnimationOptionAllowAnimatedContent animations:^{
[self setFrameToTextSize:txtFrame textView:textView];
} completion:nil];
}
else
{
[self setFrameToTextSize:txtFrame textView:textView];
}
}
- (void)textViewDidChange:(UITextView *)textView
{
[self setframeToTextSize:textView animated:YES];
}
编辑
当字符串测量正确时,您可能需要更改 UITextView
的 textContainer 上的 lineBreakMode
。 (NSTextContainer
是 iOS7 中的一个新类,包含有关文本应如何布局的信息):
textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping; // default is NSLineBreakByWordWrapping
祝你好运!
关于ios - UITextView 在使用 iOS 7 SDK 构建时换行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18867006/
IO 设备如何知道属于它的内存中的值在memory mapped IO 中发生了变化? ? 例如,假设内存地址 0 专用于保存 VGA 设备的背景颜色。当我们更改 memory[0] 中的值时,VGA
我目前正在开发一个使用Facebook sdk登录(通过FBLoginView)的iOS应用。 一切正常,除了那些拥有较旧版本的facebook的人。 当他们按下“使用Facebook登录”按钮时,他
假设我有: this - is an - example - with some - dashesNSRange将使用`rangeOfString:@“-”拾取“-”的第一个实例,但是如果我只想要最后
Card.io SDK提供以下详细信息: 卡号,有效期,月份,年份,CVV和邮政编码。 如何从此SDK获取国家名称。 - (void)userDidProvideCreditCardInfo:(Car
iOS 应用程序如何从网络服务下载图片并在安装过程中将它们安装到用户的 iOS 设备上?可能吗? 最佳答案 您无法控制应用在用户设备上的安装,因此无法在安装过程中下载其他数据。 只需在安装后首次启动应
我曾经开发过一款企业版 iOS 产品,我们公司曾将其出售给大型企业,供他们的员工使用。 该应用程序通过 AppStore 提供,企业用户获得了公司特定的配置文件(包含应用程序配置文件)以启用他们有权使
我正在尝试将 Card.io SDK 集成到我的 iOS 应用程序中。我想为 CardIO ui 做一个简单的本地化,如更改取消按钮标题或“在此保留信用卡”提示文本。 我在 github 上找到了这个
我正在使用 CardIOView 和 CardIOViewDelegate 类,没有可以设置为 YES 的 BOOL 来扫描 collectCardholderName。我可以看到它在 CardIOP
我有一个集成了通话工具包的 voip 应用程序。每次我从我的 voip 应用程序调用时,都会在 native 电话应用程序中创建一个新的最近通话记录。我在 voip 应用程序中也有自定义联系人(电话应
iOS 应用程序如何知道应用程序打开时屏幕上是否已经有键盘?应用程序运行后,它可以接收键盘显示/隐藏通知。但是,如果应用程序在分屏模式下作为辅助应用程序打开,而主应用程序已经显示键盘,则辅助应用程序不
我在模拟器中收到以下错误: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedIm
如 Apple 文档所示,可以通过 EAAccessory Framework 与经过认证的配件(由 Apple 认证)进行通信。但是我有点困惑,因为一些帖子告诉我它也可以通过 CoreBluetoo
尽管现在的调试器已经很不错了,但有时找出应用程序中正在发生的事情的最好方法仍然是古老的 NSLog。当您连接到计算机时,这样做很容易; Xcode 会帮助弹出日志查看器面板,然后就可以了。当您不在办公
在我的 iOS 应用程序中,我定义了一些兴趣点。其中一些有一个 Kontakt.io 信标的名称,它绑定(bind)到一个特定的 PoI(我的意思是通常贴在信标标签上的名称)。现在我想在附近发现信标,
我正在为警报提示创建一个 trigger.io 插件。尝试从警报提示返回数据。这是我的代码: // Prompt + (void)show_prompt:(ForgeTask*)task{
您好,我是 Apple iOS 的新手。我阅读并搜索了很多关于推送通知的文章,但我没有发现任何关于 APNS 从 io4 到 ios 6 的新更新的信息。任何人都可以向我提供 APNS 如何在 ios
UITabBar 的高度似乎在 iOS 7 和 8/9/10/11 之间发生了变化。我发布这个问题是为了让其他人轻松找到答案。 那么:在 iPhone 和 iPad 上的 iOS 8/9/10/11
我想我可以针对不同的 iOS 版本使用不同的 Storyboard。 由于 UI 的差异,我将创建下一个 Storyboard: Main_iPhone.storyboard Main_iPad.st
我正在写一些东西,我将使用设备的 iTunes 库中的一部分音轨来覆盖 2 个视频的组合,例如: AVMutableComposition* mixComposition = [[AVMutableC
我创建了一个简单的 iOS 程序,可以顺利编译并在 iPad 模拟器上运行良好。当我告诉 XCode 4 使用我连接的 iPad 设备时,无法编译相同的程序。问题似乎是当我尝试使用附加的 iPad 时
我是一名优秀的程序员,十分优秀!