我正在为一个属性化字符串调整UITextView的大小,该字符串是基于来自API的响应创建的,该响应最多可以有三个不同的属性:常规条款、装运条款和退货条款。如果其中一个属性丢失,我不会在最终文本中显示该属性。在iPhone 6上,所有的东西都以它应该的方式显示,但是在iPhone 5/5s上,最后几行文字被切掉了。textView的大小调整正确,但直到最后几行才显示所有文本。我希望我能展示照片,但显然我没有足够的代表这样做。我搜索过的其他类似问题并不完全相同,而且这些解决方案似乎都无法解决问题。这是我的Swift代码,用于创建属性字符串和调整textView的大小。任何帮助都将不胜感激。
这是格式化属性字符串的代码

var termStrings = String()

                    if (!generalString.isEqualToString("")) {
                        termStrings += "GENERAL:\n\(generalString)\n\n"
                    }

                    if (!shippingString.isEqualToString("")) {
                        termStrings += "SHIPPING:\n\(shippingString)\n\n"
                    }

                    if (!returnString.isEqualToString("")) {
                        termStrings += "RETURNS:\n\(returnString)"
                    }

                    var newNSString = termStrings as NSString

                    var attTermString = NSMutableAttributedString(string: termStrings as String)

                    let boldAttribute = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.boldSystemFontOfSize(14)]
                    let subtitleAttribute = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(12)]
                    let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle]
                    var paragraphStyle = NSMutableParagraphStyle()
                    paragraphStyle.lineSpacing = 5.0

                    if (!generalString.isEqualToString("")) {
                        attTermString.addAttributes(boldAttribute, range: newNSString.rangeOfString("GENERAL:"))
                        attTermString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: newNSString.rangeOfString("GENERAL:"))
                        attTermString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: newNSString.rangeOfString("GENERAL:\n"))
                        attTermString.addAttributes(subtitleAttribute, range: newNSString.rangeOfString("\(generalString)"))
                    }

                    if (!shippingString.isEqualToString("")) {
                        attTermString.addAttributes(boldAttribute, range: newNSString.rangeOfString("SHIPPING:"))
                        attTermString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: newNSString.rangeOfString("SHIPPING:"))
                        attTermString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: newNSString.rangeOfString("SHIPPING:\n"))
                        attTermString.addAttributes(subtitleAttribute, range: newNSString.rangeOfString("\(shippingString)"))
                    }

                    if (!returnString.isEqualToString("")) {
                        attTermString.addAttributes(boldAttribute, range: newNSString.rangeOfString("RETURNS:"))
                        attTermString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: newNSString.rangeOfString("RETURNS:"))
                        attTermString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: newNSString.rangeOfString("RETURNS:\n"))
                        attTermString.addAttributes(subtitleAttribute, range: newNSString.rangeOfString("\(returnString)"))
                    }

                    changeTextViewHeight(attTermString, width: textView.frame.size.width)

这是我调整textView大小的代码
func changeTextViewHeight(attributedString: NSMutableAttributedString, width: CGFloat) {
    textView.attributedText = attributedString
    var newSize: CGSize  = textView.sizeThatFits(CGSizeMake(width, CGFloat(FLT_MAX)))
    var newFrame: CGRect = textView.frame
    newFrame.size = CGSizeMake(CGFloat(fmaxf((Float)(newSize.width), (Float)(width))), newSize.height)
    textView.frame = newFrame
}

最佳答案

解决步骤(这将在5/5秒内将UITextView滚动到顶部)
禁用UITextView滚动视图
设置scrollRectToVisible启用
UITextView滚动
银行代码3:

self.yourTextView.isScrollEnabled = false
let rect:CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
self.yourTextView.scrollRectToVisible(rect, animated: false)
self.yourTextView.isScrollEnabled = true

这对我有效。

关于ios - UITextView可以在iPhone 5/5s上截断文字,但在iPhone 6中则不会截断文字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31064508/

10-11 15:33