标签: nsattributedstring

文本属性未显示在丰富的UITextView(iOS 6)中

我正在尝试为iOS 6 UITextView中的某些初始文本设置字体颜色.在UI 6模拟器中运行时,视图会出现,但不会呈现属性.我错过了什么吗?

NSAttributedString *as = [[NSAttributedString alloc] initWithString:boilerText
    attributes:[NSDictionary dictionaryWithObjectsAndKeys:
      NSForegroundColorAttributeName, [UIColor redColor] ,
      NSUnderlineStyleAttributeName,
          [NSNumber numberWithInt:  NSUnderlineStyleSingle],nil]
      ];

_descriptionView.attributedText = as;
Run Code Online (Sandbox Code Playgroud)

iphone uitextview nsattributedstring ios ios6

2
推荐指数
1
解决办法
1341
查看次数

NSRange,在一个段落中找到一个错误的结果

我写了一个方法来突出显示一个段落NSString中的单词,通过发送一个单词,它完美地工作,直到我遇到这种情况:

当我有这个文字时:

他们的母亲试图在其他地方穿上它们......

当我传递这个词时other,"m other" 这个词正在突出显示,当我走过时,in我得到了"礼服ing".

这是我的代码:

-(void)setTextHighlited :(NSString *)txt{
    NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.textLabel.text];

    for (NSString *word in [self.textLabel.text componentsSeparatedByString:@" "]) {

        if ([word hasPrefix:txt]) {
            NSRange range=[self.textLabel.text rangeOfString:word];
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
    }
Run Code Online (Sandbox Code Playgroud)

我试过使用rangeOfString:options:包含所有选项,但仍然有同样的问题.

请指教

PS: 这是我的代码的来源

objective-c nsstring nsattributedstring ios nsrange

2
推荐指数
1
解决办法
1342
查看次数

iOS 7使用HTML字符串作为NSAttributedString并设置字体?

我有一个UITextView,我正在尝试显示一些HTML格式的属性文本.它使用内联粗体和斜体等内容.客户端现在希望更改此字体.但是,每当我更改字体时,它似乎忽略任何HTML格式.是否有任何方法可以在保留其他格式属性的同时更改字体,例如粗体化斜体?

uitextview nsattributedstring ios7 textkit

2
推荐指数
1
解决办法
1823
查看次数

将嵌套的粗体斜体HTML标记转换为NSAttributedString

我使用此代码使用RegEx将HTML字符串转换为NSAttributedString.我唯一的问题是嵌套的粗体和斜体标签.RegEx是正确的方法吗?

另外,我想避免使用HTML解析器,因为我需要的只有Bold,Italic和Underline属性以及StrikeThrough(如果可能).

有什么建议?

- (NSMutableAttributedString *)applyStylesToString:(NSString *)string searchRange:(NSRange)searchRange {

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    [attributedString addAttribute:NSFontAttributeName value:[StylesConfig regularLargeFont] range:searchRange];

    NSDictionary *boldAttributes = @{ NSFontAttributeName : [StylesConfig regularBoldLargeFont] };
    NSDictionary *italicAttributes = @{ NSFontAttributeName : [StylesConfig regularItalicLargeFont] };
    NSDictionary *underlineAttributes = @{ NSUnderlineStyleAttributeName : @1};
    NSDictionary *strikeThroughAttributes = @{ NSStrikethroughStyleAttributeName : @1};

    NSDictionary *replacements = @{
                                   @"<b>(.*?)</b>" : boldAttributes,
                                   @"<i>(.*?)</i>" : italicAttributes,
                                   @"<u>(.*?)</u>" : underlineAttributes,
                                   @"<s>(.*?)</s>" : strikeThroughAttributes
                                   };

    for (NSString *key in replacements) {

        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:key …
Run Code Online (Sandbox Code Playgroud)

html attributes objective-c nsattributedstring ios

2
推荐指数
1
解决办法
1496
查看次数

无法将NSRange类型的值转换为预期的参数类型

我试图将一些属性附加到NSAttributeString并继续得到以下错误. 截图

 func strikeThroughStyle() {            
   let range = NSMakeRange(0, 1)
   // add styles to self
   self.attribute(self.string, atIndex: 0, effectiveRange: range)
 }
Run Code Online (Sandbox Code Playgroud)

我收到错误:

无法将'NSRange'(又名'_NSRange')类型的值转换为预期的参数类型'NSRangePointer'(又名'UnsafeMutablePointer <_NSRange>')

nsattributedstring ios swift

2
推荐指数
1
解决办法
1112
查看次数

具有右对齐的NSAttributedString在末尾删除空格

我创建了一个简单的聊天应用程序,其中我们的消息在右侧(右对齐),所有其他消息在左侧(左对齐).我正在使用,NSAttributedString因为我大量修改文本的颜色等.每条消息都是UILabel.我的问题是,在正确对齐的消息的末尾,我想放一个空格,所以它看起来像这样:

"Some example sentence "
Run Code Online (Sandbox Code Playgroud)

而不是这样的:

"Some example sentece"
Run Code Online (Sandbox Code Playgroud)

每次我把空白放在那里它都被删除了(我也尝试了不间断的空间\u00a0,我得到了同样的问题(空格被删除)我的右对齐代码如下所示:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text /*attributes:attrDict*/];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentRight];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
Run Code Online (Sandbox Code Playgroud)

后来我添加了一些其他属性与颜色等(没有任何改变文本本身).文本总是像最后一样用最后的空格:"Some example sentece " 最后我做这样的事情:

self.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)

并且...我的空间被删除了.如何防止我的文本删除最后的空格?我需要那里.

编辑:

if (self.textAlignment == NSTextAlignmentRight) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setAlignment:NSTextAlignmentRight];
        [paragraphStyle setTailIndent:0.1];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
    }
Run Code Online (Sandbox Code Playgroud)

这是我的tailIndent代码,它看起来像这样.在tailIndent之前,我在聊天的右侧有一条消息"test"(这里是左边因为我不知道如何正确对齐文本:P):

测试

在tailIndent之后:

Ť

那么会发生什么:文本从右到左,在这种情况下只留下最后一个字符.只有tailIndent 0.1!

whitespace objective-c nsattributedstring right-align nsmutableattributedstring

2
推荐指数
1
解决办法
1767
查看次数

读取所有NSAttributedString属性及其有效范围

我正在开发项目,需要在textview中找到一系列大胆的单词并替换它们的颜色,我已经尝试过以下但是它没有用.

.enumerateAttribute (NSFontAttributeName, in:NSMakeRange(0, descriptionTextView.attributedText.length), options:.longestEffectiveRangeNotRequired) { value, range, stop in

}
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift3

2
推荐指数
1
解决办法
2817
查看次数

如何将[String:Any]转换为[NSAttributedStringKey:Any]

处理一些objC API,我收到一个NSDictionary<NSString *, id> *>转换为[String : Any]Swift的,我用于NSAttributedString.addAttributes:range : .

但是,此方法签名现在已随Xcode 9更改,现在需要一个[NSAttributedStringKey : Any].

let attr: [String : Any]? = OldPodModule.getMyAttributes()
// Cannot assign value of type '[String : Any]?' to type '[NSAttributedStringKey : Any]?'
let newAttr: [NSAttributedStringKey : Any]? = attr
if let newAttr = newAttr {
    myAttributedString.addAttributes(newAttr, range: range)
}
Run Code Online (Sandbox Code Playgroud)

如何将a转换[String : Any][NSAttributedStringKey : Any]

nsattributedstring swift swift4

2
推荐指数
1
解决办法
5334
查看次数

Swift - 将HTML文本转换为归属字符串

在我的一个模块中,我希望使用UILabel将多语言HTML文本(英语和泰米尔语)显示为NSAttributedString.如果文本是纯英文,我可以用这种方式表现出我的愿望.但我的内容包含英文和泰米尔语字符.我该怎么处理这个场景.如果有人知道,请分享您的建议.

HTML内容

<medium><b><font color='#2f3744'>IPL Tamil Web Series Episode #3 | ?????? Swetha ? | Tamil Comedy Web Series | Being Thamizhan</font></b></medium> has been succesfully scheduled on <medium><b><font color='#2f3744'>2018-05-23 08:51 PM</font></b></medium>
Run Code Online (Sandbox Code Playgroud)

期望

IPL泰米尔语网络系列第3集| யாருடாSwetha?| 泰米尔喜剧网络系列| 作为Thamizhan成功地安排在2018-05-23 08:45 PM

电流输出

IPL泰米尔语网络系列第3集| &*$%!@#$ @ ^&$&^%$ Swetha?| 泰米尔喜剧网络系列| 作为Thamizhan成功地安排在2018-05-23 08:45 PM

注意:我尝试使用下面的代码段来存档它

extension String {
var htmlToAttributedString: NSAttributedString? {
    guard let data = data(using: .utf8) else { return NSAttributedString() }
    do {
        return try NSAttributedString(data: data, options:
            [NSAttributedString.DocumentReadingOptionKey.documentType: …
Run Code Online (Sandbox Code Playgroud)

html nsattributedstring ios swift

2
推荐指数
1
解决办法
2861
查看次数

将Markdown格式的字符串转换为Swift中的NSAttributedString

有没有一种方法来转换包含文本降价纯文本字符串(即# heading,* list item,[a link](http://example.com)等)的NSAttributedString斯威夫特?我想我可以对某些MD模式的索引执行某种正则表达式搜索并从中创建属性字符串,但这看起来很笨拙并且对我来说感觉不对.

有更简单的方法吗?

macos markdown nsattributedstring ios swift

2
推荐指数
3
解决办法
1213
查看次数