标签: nsattributedstring

Objective C - 更改NSAttributedString中的所有属性?

[attributedString enumerateAttributesInRange:range options:NSAttributedStringEnumerationReverse usingBlock:
     ^(NSDictionary *attributes, NSRange range, BOOL *stop) {

         NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
         [mutableAttributes setObject:[NSNumber numberWithInt:1] forKey:@"NSUnderline"];
         attributes = mutableAttributes;

     }];
Run Code Online (Sandbox Code Playgroud)

我试图循环所有属性并添加NSUnderline给他们.在调试时,似乎NSUnderline被添加到字典中,但是当我第二次循环时它们被删除.我在更新NSDictionaries时做错了什么?

iphone objective-c nsdictionary nsattributedstring

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

为什么UITextView结合了先前设置的attributionText的属性?

在玩NSAttributedString时,我遇到了一些来自UITextView的奇怪行为.说我有两个属性:

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextView *textView;
Run Code Online (Sandbox Code Playgroud)

在这些属性的拥有控制器中,我有以下代码:

NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:20.],
                            NSForegroundColorAttributeName: [UIColor redColor]};
NSAttributedString *as = [[NSAttributedString alloc] initWithString:@"Hello there!" attributes:attributes];


NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithString:@"Hello where?" attributes:nil];
[mas addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3, 5)];

self.label.attributedText = as;
self.label.attributedText = mas;


self.textView.attributedText = as;
self.textView.attributedText = mas;
Run Code Online (Sandbox Code Playgroud)

在模拟器中运行时,标签使用系统默认字体查看(呃,使用您的想象力),如下所示:

<black>Hel</black><yellow>lo wh</yellow><black>ere?</black>
Run Code Online (Sandbox Code Playgroud)

使用大小为20.0的系统字体,文本视图如下所示:

<red>Hel</red><yellow>lo wh</yellow><red>ere?</red>
Run Code Online (Sandbox Code Playgroud)

看起来文本视图组合了两个属性字符串的属性.我发现这是一个令人惊讶的结果,并期望它表现得像标签.

我怀疑这是一个错误.如果不是,UITextView如何以及为什么以不同于UILabel的方式处理attributionText?

(XCode版本4.5.1)

uitextview nsattributedstring uilabel ios

9
推荐指数
2
解决办法
3693
查看次数

Swift NSAttributedString自定义字体

我已经阅读了不同的解决方案,但似乎没有任何效果.此代码创建一个零异常:

[NSFontAttributeName: UIFont(name: "Raleway-SemiBold", size: 16)!]
Run Code Online (Sandbox Code Playgroud)

我正确安装了字体,它们在应用程序中正确显示(目标设置).
我尝试添加应用程序提供的字体plist但没有发生任何事情.我无法编辑数组中的项目:(they are item0 : string : Raleway-SemiBold.tff).

所以基本上我被卡住了......有时Swift和Apple环境对于程序员来说是很好的,有时候(大多数时候),它们太缺陷了,需要这么多的解决方法来达到预期的结果.

非常感谢您的帮助.

nsattributedstring custom-font ios swift

9
推荐指数
1
解决办法
1万
查看次数

NSAttributedString中行尾的NSKernAttributeName空格

使用NSKernAttributeName它时在每行的末尾放置一个空格,有什么方法可以解决这个问题吗?我可以将属性设置为:

NSRange(location: 0, length: self.text!.characters.count-1)
Run Code Online (Sandbox Code Playgroud)

但我不想为每一行设置这个.

这是我正在使用的游乐场的测试代码

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground

var text = "Hello, playground\nhow are you?"

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 50
paragraphStyle.alignment = NSTextAlignment.Left
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail

let attributes = [
    NSParagraphStyleAttributeName: paragraphStyle
    , NSKernAttributeName: 20
]


let attributedString = NSAttributedString(string: text, attributes: attributes)

let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 0
label.textColor = UIColor.greenColor()
label.backgroundColor = UIColor.orangeColor()
label.sizeToFit()
label.center = CGPoint(x: …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring ios swift

9
推荐指数
1
解决办法
3146
查看次数

UILabel中的adjustsFontForContentSizeCategory具有属性文本

我以为我会节省其他时间的调试,并说设置adjustsFontForContentSizeCategory为true对你设置attributedText值的标签没有影响(至少在tableview单元格标签中).

幸运的是,解决方案是自己在属性字符串上设置字体.我写了一个小的实用程序扩展:

public extension NSMutableAttributedString {
    public func setFont(_ font: UIFont) -> NSMutableAttributedString {
        addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: string.count))

        return self
    }
}
Run Code Online (Sandbox Code Playgroud)

在哪里设置您要调用的attributedText属性

@IBOutlet weak var myLabel: UILabel!

var myAttributedString: NSAttributedString = .....

myLabel.attributedText = NSMutableAttributedString(attributedString: myAttributedString).setFont(myLabel.font)
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel ios

9
推荐指数
1
解决办法
339
查看次数

是否可以在NSAttributedString中调整基线?

我想略微修改一个角色的y位置NSAttributedString.我知道我可以使用属性字典下标(或上标):

@{(id)kCTSuperscriptAttributeName: @(-1)}
Run Code Online (Sandbox Code Playgroud)

不幸的是,由此引起的转变在我的情况下太大了.我正在寻找一个以1点为单位调整基线的选项.

属性字符串将显示在UIButton使用中-setAttributedTitle:forState:.

cocoa-touch nsattributedstring core-text ios ios6

8
推荐指数
1
解决办法
5362
查看次数

UITextView在iOS 8中崩溃,其中包含一个表的属性字符串

在iOS 8(或甚至可能在iOS 7/7.1中)NSAttributedStringiOS上添加了渲染文本表的功能.此API 在OS X公开,但在iOS 公开.但是,可以通过将包含表的HTML传递给iOS,在iOS上创建包含文本表的属性字符串-[NSAttributedString initWithData:options:documentAttributes:error:].

正确创建了属性字符串,但是当我尝试使用a渲染字符串时UITextView,会抛出此异常:

<NSATSTypesetter: 0x7f8900faec90>: Exception *** -[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds raised during typesetting layout manager <NSLayoutManager: 0x7f8902e13440>
1 containers, text backing has 1884 characters
Currently holding 1884 glyphs.
Glyph tree contents:  1884 characters, 1884 glyphs, 1 nodes, 64 node bytes, 7680 storage bytes, 7744 total bytes, 4.11 bytes per character, 4.11 bytes per glyph
Layout tree contents:  1884 …
Run Code Online (Sandbox Code Playgroud)

objective-c uitextview nsattributedstring ios nstextstorage

8
推荐指数
0
解决办法
904
查看次数

如何在不减慢过程的情况下使用颜色进行文本着色?

我发现字符串着色的时间取决于使用了多少不同的NSColors.在下面的代码中,如果我只对三种情况使用一种颜色,则文本着色过程比三种不同颜色用于这三种情况的情况快3倍,每种情况下每种颜色.为什么?有没有办法不减慢三种不同颜色的着色?

for i in 0..<arrayOfNSRangesForA.count
{
    textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForA[i])
}

for i in 0..<arrayOfNSRangesForT.count
{
   textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForT[i])
}

for i in 0..<arrayOfNSRangesForC.count
{
    textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForC[i])
}
Run Code Online (Sandbox Code Playgroud)

更新 我发现了一件坏事.当我改变着色时NSForegroundColorAttributeName,NSBackgroundColorAttributeName运行时间显着增加--10倍.对于20 000个字符,它是一种颜色,NSForegroundColorAttributeName- 1秒,NSBackgroundColorAttributeName- 10秒; 如果有三种颜色 - 相应的3和30秒.对我而言,Swift的功能非常糟糕!由于DNA的长度是数千个A,T,G,C字符,因此无法用DNA(ATGC序列)着色进行正常工作!

更新 在评论中,我建议仅为文本的可见部分着色.我尝试过这种方法,与标准方式相比,即使对于较短的文本也是如此.所以,我有文本的NSRange文本的可见部分,并在滚动时使用通知滚动时在飞行中着色.这是一个糟糕的方式.

arrays nsattributedstring nscolor swift

8
推荐指数
1
解决办法
280
查看次数

无法将"Range <String.Index>"类型的值(又名"Range <String.CharacterView.Index>")转换为预期的参数类型"NSRange"(又名"_NSRange")

我试图用属性String替换子字符串.以下是我的代码.

let searchText = self.searchBar.text!
let name = item.firstName ?? ""
let idNo = "Employee Id. \(item.employeeId ?? "NA")"

if let range = name.range(of: searchText, options: String.CompareOptions.caseInsensitive, range: nil, locale: nil)
{
    let attributedSubString = NSAttributedString.init(string: name.substring(with: range), attributes: [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 17)])
    let normalNameString = NSMutableAttributedString.init(string: name)
    normalNameString.mutableString.replacingCharacters(in: range, with: attributedSubString)
    cell.name.attributedText = normalNameString
}
else
{
    cell.name.text = name
}
Run Code Online (Sandbox Code Playgroud)

我收到编译时错误:

"无法将'Range'类型的值(又称'Range')转换为预期的参数类型'NSRange'(又名'_NSRange')".

我应该在这里改变什么?

nsattributedstring ios swift

8
推荐指数
2
解决办法
8651
查看次数

多种颜色的大导航栏文本

iOS 11在导航栏中引入了大文本选项.我想要一个使用多种颜色的标题.例如:

在此输入图像描述

设置标题相当容易,甚至可以更改整个标题的颜色:

[[self navigationItem] setTitle: @"Colors"];
[[[self navigationController] navigationBar] setLargeTitleTextAttributes: @{NSForegroundColorAttributeName: [UIColor colorFromHex: redColor]}];
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚的是如何改变标题的一部分.例如,一种选择这样的范围的方法 - NSRangeMake(0, 1)- 以便我可以为它应用颜色.

这一定是可能的,对吧?

uinavigationbar nsattributedstring uinavigationitem ios

8
推荐指数
1
解决办法
823
查看次数