UITextView Alternative [英] UITextView Alternative

查看:135
本文介绍了UITextView Alternative的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我从Apple docs中了解的UITextView的attributesText属性:

As i understood from Apple docs attributedText property of UITextView:


默认情况下,此属性为零。为此属性分配新值也会使用相同的字符串数据替换text属性的值,尽管没有任何格式设置信息。此外,分配新值会更新font,textColor和textAlignment属性中的值,以便它们反映从属性字符串中的位置0开始的样式信息。

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.

因此,我不能在不同位置使用多个属性的代码添加一个attributionString。

Therefore i cannot add an attributedString by code with multiple Attributes at different locations.

有人可以帮助我通过代码创建以下效果在iOS6?这可以使用nib文件和更改UITextView中文本部分的范围属性,但我似乎无法通过代码重现效果。

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> 所需<'/ font> 效果<'textColor = [UIColor redColor]> 写入b <'/ textColor> y 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。我不想使用CATextLayers,因为我正在尝试使用UITextView的AutoLayout功能。

P.S. I don't want to use CATextLayers since i am trying to use the AutoLayout feature with the UITextView.

推荐答案

你可以建立一个带有NSDictionary的NSAttributedString包含您需要的所有属性:

You can build an NSAttributedString with an NSDictionary containing all of the attributes you need:

NSAttributedString* attString = [[NSAttributedString alloc] 
               initWithString: @"Desired effect to be written by code" 
                   attributes: (NSDictionary*)attributes];

或者使用它的可变子类NSMutableAttributedString:

Or use it's mutable subclass NSMutableAttributedString:

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]  
               initWithString: @"Desired effect to be written by code"];

    [attString addAttribute: NSForegroundColorAttributeName 
                      value: [UIColor redColor] 
                      range: NSMakeRange(15,15)];

... etc

then ...

...etc
then...

myTextView.attributedText = attString;

每个属性都应用于字符串的NSRange(位置,长度)。不同的属性类型(例如颜色,字体大小)可以重叠不同的范围。

Each attribute is applied to an NSRange (location, length) of the string. Distinct attribute types (eg colour, font size) can overlap different ranges.

更新

使用NSMutableAttributedString示例。 NSAttributedString的initWithString:attributes 将在字符串的整个范围内应用属性,这是您要避免的,抱歉混淆。

update
Use the NSMutableAttributedString example. NSAttributedString's initWithString:attributes will apply attributes across the entire range of the string, which is what you are trying to avoid, sorry for confusion.

这篇关于UITextView Alternative的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆