UITextView Alternative(UITextView Alternative)
正如我从Apple docs UITextView的attributesText属性中理解的那样:
默认情况下,此属性为零。 为此属性分配新值也会使用相同的字符串数据替换text属性的值,尽管没有任何格式设置信息。 此外,指定新值会更新font,textColor和textAlignment属性中的值,以便它们反映从属性字符串中的位置0开始的样式信息。
因此,我不能通过在不同位置具有多个属性的代码添加一个attributionString。
有人可以帮我用iOS6中的代码创建以下效果吗? 这可以使用nib文件和在UITextView中更改文本部分的范围属性,但我似乎无法通过代码重现效果。
<'font systemFontOfSize = 18> 所需的 <'/ font> 效果 <'textColor = [UIColor redColor]> 写入b <'/ textColor> y代码 。
假设标签对应于属性。
PS我不想使用CATextLayers,因为我试图使用UITextView的AutoLayout功能。
As i understood from Apple docs attributedText property of UITextView:
This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and textAlignment properties so that they reflect the style information starting at location 0 in the attributed string.
Therefore i cannot add an attributedString by code with multiple Attributes at different locations.
Can someone please help me create the following effect by code in iOS6? This is possible using nib files and by changing range attributes for text parts in UITextView but i cant seem to reproduce the effect by code.
<'font systemFontOfSize=18>Desired<'/font> effect <'textColor = [UIColor redColor]> to be written b<'/textColor>y code.
Suppose the tags correspond to attributes.
P.S. I don't want to use CATextLayers since i am trying to use the AutoLayout feature with the UITextView.
原文:https://stackoverflow.com/questions/13932550
最满意答案
Data.Text.concat
是一个O(n+m)
操作,其中n
和m
是要连接的字符串的长度。 这是因为必须分配大小为n + m
的新内存缓冲区来存储连接的结果。
Builder
专门针对mappend
操作进行了优化。 这是一种廉价的O(1)
操作(功能组合,GHC也极好地优化)。 使用Builder
您实际上是在构建有关如何生成最终字符串结果的说明,但是延迟实际创建,直到您执行某些Builder -> Text
转换。要回答您的问题,如果您已经分析了应用程序并发现
Text.concat
在运行时占主导地位,则应选择Builder
。 这显然取决于您的需求和应用。 什么时候应该使用Builder
没有通用规则,但对于简短的Text
文字,可能没有必要。如果使用
Builder
将涉及“进行大规模重构”,那么分析肯定是值得的。 虽然不言而喻,Haskell自然会使这种重构比你习惯使用开发人员友好的语言少得多,所以它可能并不是一项艰巨的任务。
Data.Text.concat
is anO(n+m)
operation wheren
andm
are the lengths of the strings you want toconcat
. This is because a new memory buffer of sizen + m
must be allocated to store the result of the concatenation.
Builder
is specifically optimized for themappend
operation. It's a cheapO(1)
operation (function composition, which is also excellently optimized by GHC). WithBuilder
you are essentially building up the instructions for how to produce the final string result, but delaying the actual creation until you do someBuilder -> Text
transformation.To answer your questions, you should choose
Builder
if you have profiled your application and discovered thatText.concat
are dominating the run time. This will obviously depend on your needs and application. There is no general rule for when you should useBuilder
but for shortText
literals there is probably no need.Profiling would definitely be worthwhile if using
Builder
would involve "undertaking a large-scale refactoring". Although it goes without saying that Haskell will naturally make this kind of refactoring much less painful than you might be used to with less developer friendly languages, so it might not be such a difficult undertaking after all.
相关问答
更多-
作为一种方法,这种方法很好,但是为了更加健全 ,我会在init(Client $client)传递Client init()而不是在类中实例化它init(Client $client) 。 这样做和$settings ,更好地传递作为参数或保留在一些private变量而不是初始化器中的硬编码。 它指的是D和L字母, 依赖倒置原则和Liskov替换原则 没有性能问题,只有架构方法。 但至于我,我没有看到任何先决条件,以避免构造函数和使用$api = new API($client, $settings); 而 ...
-
你可以breakOn子串, breakOn :: Text -> Text -> (Text, Text) 在模式的第一次出现时将Text分成两部分,然后在第二部分的开头替换子字符串: replaceOne :: Text -> Text -> Text -> Text replaceOne pattern substitution text | T.null back = text -- pattern doesn't occur | otherwise = T.concat [front ...
-
我认为你想限制Int的值为{1,2,3} 。 如果是这样,你可以定义一个新类型Oct : newtype Oct = Oct Int deriving(Show) 然后定义一个使得Oct为的函数Oct : oct :: Int -> Oct oct n | n > 0 && n < 4 = Oct n | otherwise = error "invalid value: " ++ show n 如果抛出error是不可接受的,那么Maybe会有所帮助: oct :: Int ...
-
为什么从Data.Text构建一个Haskell字符串非常缓慢(Why is building a Haskell String from Data.Text so slow)[2021-12-04]
你不能尽快完成,但你可以加快速度。 追加 Text表示为数组。 这使得<>相当慢,因为必须分配一个新数组并将每个Text复制到其中。 您可以通过先将每个片段转换为一个String ,然后连接它们来解决这个问题。 我想Text可能也提供了一种有效的方法来一次连接多个文本(正如一位评论者提到的,你可以使用一个懒惰的构建器),但为此目的会更慢。 另一个好的选择可能是Text的懒惰版本,它可能支持高效的连接。 分享 在基于String的实现中,根本不必复制description字段。 它只是在Location和显示 ... -
Data.Text.concat是一个O(n+m)操作,其中n和m是要连接的字符串的长度。 这是因为必须分配大小为n + m的新内存缓冲区来存储连接的结果。 Builder专门针对mappend操作进行了优化。 这是一种廉价的O(1)操作(功能组合,GHC也极好地优化)。 使用Builder您实际上是在构建有关如何生成最终字符串结果的说明,但是延迟实际创建,直到您执行某些Builder -> Text转换。 要回答您的问题,如果您已经分析了应用程序并发现Text.concat在运行时占主导地位,则应选择Bu ...
-
GHCJS文本数据构造器(GHCJS Text Data Constructor)[2023-12-02]
看起来您可能正在使用reflex-platform 。 我们最近整合了一个对文本使用JSString的更改,而不是通常的实现; 这是因为JSString比纯Haskell Text实现快得多并且使用的内存要少得多。 如果这是你遇到的问题,我建议你坚持使用JSString,除非你对性能完全不关心。 但是,如果您想要禁用此优化,我认为它应该只是注释掉这个块 ,然后重新输入你的try-reflex / work-on / nix-shell。 It looks like you might be using re ... -
你想构建parseJSON吗? 它不一定是parseJSON ...... abParser :: String -> Value -> Parser MyRecord abParser valueC = withObject "MyRecord" $ \o -> do valueA <- o .: "a" valueB <- o .: "b" return MyRecord { a = valueA, b = valueB, c = valueC } createMyRecord ...
-
如何使用Data.Text.Lazy.IO用Aeson解析JSON文件(How to use Data.Text.Lazy.IO to parse JSON files with Aeson)[2023-07-13]
一般来说,我建议使用流式库来解析像JSON文件这样的任意大小的数据。 但是,在使用aeson解析JSON的特定情况下,过度运行内存的问题并不像IMO那么重要,因为aeson库本身最终会将内存中的整个文件表示为Value类型。 因此,您可以选择简单地使用严格的字节串I / O. 我给出了使用管道和严格I / O来解析JSON值的示例。 (我认为管道版本已存在于某些库中,我不确定。) #!/usr/bin/env stack {- stack --resolver lts-7.14 --install-ghc ... -
构造Xpath(Construct Xpath)[2021-08-12]
这是我的测试代码: test_content = 'param_value1param_value2' sel = HtmlXPathSelector(text=test_content) sel.select('//div/strong[@class="param"]/text()').extract()[0] sel.s ... -
使用类型族来避免MPTC中的显式类型参数(Using Type Families to avoiding explicit type class parameters in MPTCs)[2023-09-10]
嗯,明显的解决方案如下: class Builder b where type Element b build :: Element b -> b instance Builder B.Builder where type Element B.Builder = Int8 build = B.int8 instance Builder T.Builder where type Element T.Builder = Int8 build = T.decimal 但是,这个问题是 ...