ios – NSAttributedString没有按时渲染 – Swift

ios – NSAttributedString没有按时渲染 – Swift,第1张

概述所以,在某些iOS版本的旧iPhone中,某些字符串没有被正确渲染的项目中,我遇到了问题(具体来说,它不适用于iOS 5的iPhone 5,而对于iOS9.3而言,这是非常奇怪的).为了减少这个问题,我写了这个代码: import UIKitclass ViewController: UIViewController { @IBOutlet weak var label: UILabe 所以,在某些iOS版本的旧iPhone中,某些字符串没有被正确渲染的项目中,我遇到了问题(具体来说,它不适用于iOS 5的iPhone 5,而对于iOS9.3而言,这是非常奇怪的).为了减少这个问题,我写了这个代码:
import UIKitclass VIEwController: UIVIEwController {    @IBOutlet weak var label: UILabel!    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // Long HTML,although we'll make it even larger to prove a point        let string = "<h1>Thing 1</h1><p>Lorem ipsum dolor sit amet,consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dAPIbus diam. Sed nisi. Nulla quis sem at nibh elementum imperdIEt. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra,per inceptos himenaeos. </p><h1>Thing 2</h1><p>Curabitur soDales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus,iaculis vel,suscipit quis,luctus non,massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus,ullamcorper vel,tincIDunt sed,euismod in,nibh. </p><h1>Thing 3</h1><p>Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra,per inceptos himenaeos. Nam nec ante. Sed lacinia,urna non tincIDunt mattis,tortor neque adipiscing diam,a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdIEt. Vestibulum sAPIen. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. </p><h1>Thing 4</h1><p>Sed lectus. Integer euismod lacus luctus magna. Quisque cursus,metus vitae pharetra auctor,sem massa mattis sem,at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestIE dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc,viverra nec,blandit vel,egestas et,augue. Vestibulum tincIDunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. </p><h1>Thing 5</h1><p>Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer ID quam. Morbi mi. Quisque nisl felis,venenatis tristique,dignissim in,ultrices sit amet,augue. Proin soDales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus,commodo ac,facilisis ac,ultricIEs eu,pede. Ut orci risus,accumsan porttitor,cursus quis,aliquet eget,justo. Sed pretium blandit orci. </p><h1>Thing 6</h1><p>Ut eu diam at pede suscipit soDales. Aenean lectus elit,fermentum non,convallis ID,sagittis at,neque. Nullam mauris orci,aliquet et,iaculis et,viverra vitae,ligula. Nulla ut felis in purus aliquam imperdIEt. Maecenas aliquet mollis lectus. Vivamus consectetuer risus et tortor. Lorem ipsum dolor sit amet,consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dAPIbus diam. Sed nisi. Nulla quis sem at nibh elementum imperdIEt. Duis sagittis ipsum. Praesent mauris. </p>"        let attrStr = try! NSAttributedString(            data: (string + string + string + string + string + string + string + string).data(using: String.EnCoding.unicode,allowLossyConversion: true)!,options: [ NSdocumentTypedocumentAttribute: NSHTMLTextdocumentType],documentAttributes: nil)        label.attributedText = attrStr        label.backgroundcolor = UIcolor(red:0.00,green:1.00,blue:0.00,Alpha:1.0)    }}

足够简单当然真正的问题比这更复杂,但这个想法是一样的.

所以,在iPhone 7中,一切正常:

但是,当它加载到iPhone 4s:

调试时,在这两种情况下,attrStr似乎都被正确设置.

有任何想法吗?我的理论是,旧的手机太慢,无法及时呈现,但我不太清楚如何解决这个问题.

谢谢!

编辑:对于那些问,这是故事板:

<?xml version="1.0" enCoding="UTF-8"?><document type="com.apple.InterfaceBuilder3.Cocoatouch.Storyboard.XIB" version="3.0" toolsversion="11201" systemVersion="16A323" targetRuntime="iOS.Cocoatouch" propertyAccessControl="none" useautolayout="YES" useTraitCollections="YES" colorMatched="YES" initialVIEwController="JzW-qG-LFG">    <dependencIEs>        <deployment IDentifIEr="iOS"/>        <plugIn IDentifIEr="com.apple.InterfaceBuilder.IBCocoatouchPlugin" version="11161"/>        <capability name="Constraints to layout margins" minToolsversion="6.0"/>        <capability name="documents saved in the Xcode 8 format" minToolsversion="8.0"/>    </dependencIEs>    <scenes>        <!--VIEw Controller-->        <scene sceneID="9PR-d2-hNU">            <objects>                <vIEwController ID="JzW-qG-LFG" customClass="VIEwController" customModule="things" customModuleProvIDer="target" sceneMemberID="vIEwController">                    <layoutGuIDes>                        <vIEwControllerLayoutGuIDe type="top" ID="VGp-YT-QQN"/>                        <vIEwControllerLayoutGuIDe type="bottom" ID="SeT-t7-CD0"/>                    </layoutGuIDes>                    <vIEw key="vIEw" contentMode="scaletoFill" ID="gtg-CO-E9d">                        <rect key="frame" x="0.0" y="0.0" wIDth="375" height="667"/>                        <autoresizingMask key="autoresizingMask" wIDthSizable="YES" heightSizable="YES"/>                        <subvIEws>                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" numberOflines="0" baselineAdjustment="alignBaselines" adjustsFontSizetoFit="NO" translatesautoresizingMaskIntoConstraints="NO" ID="ID1-A1-RaT">                                <FontDescription key="FontDescription" type="system" pointSize="17"/>                                <nil key="textcolor"/>                                <nil key="highlightedcolor"/>                            </label>                        </subvIEws>                        <color key="backgroundcolor" white="1" Alpha="1" colorSpace="calibrateDWhite"/>                        <constraints>                            <constraint firstAttribute="trailingmargin" secondItem="ID1-A1-RaT" secondAttribute="trailing" ID="0qp-Rk-PC2"/>                            <constraint firstItem="ID1-A1-RaT" firstAttribute="leading" secondItem="gtg-CO-E9d" secondAttribute="leadingmargin" ID="3Vc-he-HqH"/>                            <constraint firstItem="ID1-A1-RaT" firstAttribute="top" secondItem="VGp-YT-QQN" secondAttribute="bottom" ID="7IE-fY-4H0"/>                        </constraints>                    </vIEw>                    <navigationItem key="navigationItem" ID="Opz-px-hsl"/>                    <connections>                        <outlet property="label" destination="ID1-A1-RaT" ID="TNv-G9-ASE"/>                    </connections>                </vIEwController>                <placeholder placeholderIDentifIEr="IBFirstResponder" ID="qlV-6d-ISJ" userLabel="First Responder" sceneMemberID="firstResponder"/>            </objects>            <point key="canvasLocation" x="2848.8000000000002" y="-280.20989505247377"/>        </scene>    </scenes></document>
解决方法 这里的问题是UIVIEw的高度限制为8192点.一旦标签高达8192点(或更高),它不再更新帧缓冲区. (推测窗口服务器只要忽略标签的CALayer,一旦图层太大).推测行为是未定义的,并且可能会因设备和版本的iOS而异.

(编辑:考虑到这一点,我怀疑极限是16,384像素,不是8,192点,我不认为窗口服务器处理积分.)

为了演示,我拿了你的代码和故事板,并添加了三件事情:

>我把你的标签的高度限制在小于或等于8185分.
>我添加了一个滑块来更新该高度约束的常量.滑块允许范围8185至8200.
>我添加了另一个显示高度约束常数的标签.

在运行iOS 10.2的iPhone SE模拟器中会发生什么:

在演示中,您可以看到,一旦高度限制超过8192点,滑块的拇指开始看起来涂抹,高度标签的文本可以覆盖自身.这是因为绿色标签不再更新帧缓冲区,所以以前任何位置(由滑块和高度标签绘制)都将保留,仅覆盖滑块和高度标签重绘的位置.一旦高度回落到8192点的门槛,绿色标签就会自动恢复,清理混乱.

我认为您在iPhone 7测试中没有看到这一点,因为iPhone 7的屏幕比iPhone SE更宽.所有3.5英寸和4英寸屏幕的iPhone都是320点宽. 4.7英寸屏幕的iPhone是375点宽,而5.5英寸屏幕的iPhone是414点宽.

更宽的屏幕意味着更广泛的UILabel.这意味着更多的文字适合每一行,所以标签不一定要高到适合所有的文字.我怀疑在较大的屏幕上,您的标签短于8192分,所以他们不会打到未定义的行为.

解决方法:

>如果您不打算让用户滚动查看整个文本,只需在标签上放置高度约束条件即可.
>如果要让用户滚动,请尝试使用UIWebVIEw或WKWebVIEw而不是UILabel来显示文本.我相信这些观点可以处理高达8192分的内容.

顺便说一句,你不是第一个遇到这个问题的人:UILabel view disappear when the height greater than 8192.但是,你的问题目前有一个开放的赏金,它阻止它被重新关闭.

总结

以上是内存溢出为你收集整理的ios – NSAttributedString没有按时渲染 – Swift全部内容,希望文章能够帮你解决ios – NSAttributedString没有按时渲染 – Swift所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1096836.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存