1
我調整UITextView的屬性字符串大小,該字符串是根據API的響應創建的,該API可以有三個不同的屬性;一般條款,運輸條款和退貨條款。如果缺少其中一個屬性,我不會在最終文本中顯示該屬性。在iPhone 6上,所有內容都以其應有的方式顯示出來,但在iPhone 5/5s上,最後幾行文本被截斷。 textView正確調整大小,但直到最後幾行才顯示出所有文本。我希望我能展示照片,但顯然我沒有足夠的代表來這樣做。我搜索到的其他類似問題並不完全相同,並且這些解決方案似乎都無法解決問題。這是我在Swift中的代碼,用於創建屬性字符串並調整textView的大小。任何幫助將不勝感激。UITextView切斷iPhone 5/5s上的文本,但不切斷iPhone 6
這是格式化屬性串
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
}
根據我的假設,您可能爲iPhone5/5s傳遞了錯誤的「寬度」,或者您已經爲iPhone 6設計了屏幕。因此,只需調整調整textView大小的寬度,即可獲得解決方案。 –
我會給你一個鏡頭,但是文字不會脫離屏幕或任何東西,這導致我相信一切都在正確調整大小。我會記錄寬度。謝謝。 –