标签: nsattributedstring

swift - 如何删除 NSAttributedString 中的空白(空)行?

我正在制作一个笔记应用程序。我将属性文本保存在 DetailVC 上,并在UITableViewCell 标签中显示预览。

在标签中我有这样的文字

The Bold Title

my Text
Run Code Online (Sandbox Code Playgroud)

我想改变这个

The Bold Title
my Text
Run Code Online (Sandbox Code Playgroud)

我在字符串中找到了解决方案

 let regex = try! NSRegularExpression(pattern: "\n+", options: [])
 let newString = regex.stringByReplacingMatches(in: MyString, options: [], range: NSRange(location: 0, length: MyString.characters.count), withTemplate: "\n")
Run Code Online (Sandbox Code Playgroud)

但我怎样才能在NSATTirbutedString中做到这一点?

请帮忙!

uitableview nsattributedstring ios nsregularexpression swift

3
推荐指数
1
解决办法
3208
查看次数

在一个字符串中多次使用 NSAttributedString

例如,我有一个字符串"F (R U R' U') (R U R' U') (R U R' U') F'"。我正在使用 NSAttributedString 搜索括号中的文本(R U R' U'),并将其替换为相同的文本,只是颜色不同。我正在使用的代码是

let mutableAttributedString = NSMutableAttributedString(string: algToShow) 
var searchRange = NSRange(location: 0, length: algToShow.count)
var foundRange1 = NSRange()
foundRange1 = (algToShow as NSString).range(of: "(R U R' U')", options: NSString.CompareOptions.caseInsensitive, range: searchRange)
if foundRange1.location != NSNotFound || foundRange2.location != NSNotFound || foundRange3.location != NSNotFound || foundRange4.location != NSNotFound || foundRange5.location != NSNotFound {
            // found an occurrence of the substring! …
Run Code Online (Sandbox Code Playgroud)

xcode nsattributedstring swift

3
推荐指数
1
解决办法
819
查看次数

仅更改 NSAttributedString 的字体大小

我有一个NSAttributedString从 RTF 文件加载的,所以它已经包含了不同范围的几个字体属性。现在我想根据设备的屏幕大小调整字体大小,但是当我添加具有新大小的全新字体属性时,其他字体消失了。

有没有办法更改整个字符串的字体大小?

font-size nsattributedstring ios swift

3
推荐指数
1
解决办法
964
查看次数

iOS13 中的超级/下标似乎已损坏(NSAttributedString)

尝试在 UITextView 中使用 NSAttributedString 显示超级/下标文本似乎在 iOS13 中被破坏 - 除非有人知道否则?

奇怪的是,如果我使用 UIFont systemFont 那么它可以工作 - 但如果我使用任何其他字体它就不行。

请参阅下面的代码,以在我的测试应用程序中设置 UITextView。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIFont* font = [UIFont systemFontOfSize:32];
    //UIFont* font = [UIFont fontWithName:@"Helvetica Neue" size:32];
    //UIFont* font = [UIFont fontWithName:@"Courier" size:32];
    //UIFont* font = [UIFont fontWithName:@"Arial" size:32];

    NSMutableAttributedString* as = [[NSMutableAttributedString alloc] initWithString:@"Super2Script" attributes:@{NSFontAttributeName : font}];

    [as addAttribute:(NSString*)kCTSuperscriptAttributeName value:@(1) range:NSMakeRange(5, 1)];



    UITextView* tv = [[UITextView alloc] initWithFrame:CGRectZero];
    tv.attributedText = as;
    [tv sizeToFit];

    [self.view addSubview:tv];
    tv.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring core-text ios13

3
推荐指数
1
解决办法
246
查看次数

带有 NSAttributedString 的 Cell 使 UITableView 的滚动变慢

我有一个包含多种单元格的表格视图。其中之一是带有 TextView 的单元格,在此文本视图中,我必须NSAttributedString从数据呈现一个。根据Apple 文档,这必须在主线程上完成:

不应从后台线程调用 HTML 导入器(即,选项字典包含 NSDocumentTypeDocumentAttribute,其值为 NSHTMLTextDocumentType)。它将尝试与主线程同步,失败并超时。从主线程调用它是有效的(但如果 HTML 包含对外部资源的引用,则仍然会超时,应该不惜一切代价避免这种情况)。HTML 导入机制旨在实现诸如 Markdown 之类的东西(即文本样式、颜色等),而不是用于一般的 HTML 导入。

但是以这种方式渲染会使表格视图的滚动滞后,并且还会干扰自动布局。这是我的单元格内的代码。

dispatch_async(dispatch_get_main_queue(), ^{
    NSString* htmlString = [NSString stringWithFormat:@"<div style=\"font-family:%@; font-size:%dpx; color:#08080d;\">%@</div>",fontNameBase, 16,txt];
    htmlString = [Utility replaceHtmlCodeEntities:htmlString];
    NSData* tempData = [htmlString dataUsingEncoding:NSUnicodeStringEncoding];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:tempData  options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.textViewMsg.attributedText = txt;
});
Run Code Online (Sandbox Code Playgroud)

我像这样滚动我的 tableView:

-(void)reloadAndScroll{
    [self.tableChat reloadData];

    long lastRowNumber = [_tableChat numberOfRowsInSection:0] - 1;
    if (lastRowNumber > 0) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
        [_tableChat scrollToRowAtIndexPath:indexPath …
Run Code Online (Sandbox Code Playgroud)

scroll objective-c nsattributedstring tableview ios

3
推荐指数
1
解决办法
730
查看次数

如何更改 AttributedString 中 Markdown 样式文本的外观?

WWDC 2021在 Foundation 中引入了新的 AttributedString API,包括 Markdown 支持。例如,我想更改通过 markdown 创建.stronglyEmphasized的文本的外观AttributedString,使强调文本的前景色为红色(以及粗体)。据我所知,它是这样的:

let attributedString = try! AttributedString(markdown: "This string *has* some markdown")
let transformed = attributedString.transformingAttributes(\.stronglyEmphasized) { attribute in
    attribute.replace(with: .foregroundColor, value: Color.red)
}
Run Code Online (Sandbox Code Playgroud)

但这告诉我“如果没有上下文类型,就无法解析对成员‘stronglyEmphasized’的引用”并且“无法推断通用参数‘U’”。这是毫无帮助的。

macos foundation nsattributedstring ios nsattributedstringkey

3
推荐指数
1
解决办法
1215
查看次数

将本地化字符串的一部分设置为粗体 swift

我有一个字符串,假设“我的名字是 %@,我在 %@ 班学习”,现在我想将要插入的占位符文本加粗,这样结果将如下所示:“我的名字很严厉并且我在10班学习”,我会将其显示在标签上

我已经尝试过使用 NSAttributedString 但由于字符串将被本地化,我无法使用属性字符串的范围参数将其设置为粗体。

nsattributedstring nslocalizedstring ios swift

3
推荐指数
1
解决办法
4574
查看次数

设置attributedText,NSRangeException错误

尝试通过选择设置我的UIText视图的属性文本属性.几乎是有效的.使用下面的红色字体颜色设置文本的操作.这有时会起作用,但通常会出错:

因未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray objectAtIndex:effectiveRange :: out of bounds'

即使文本视图中的字符似乎多于所选范围所指示的字符,也会发生这种情况.

- (IBAction)setText:(id)sender {

    NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithAttributedString:myTextView.attributedText];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(p1,p2)];

    myTextView.attributedText = string;

}
Run Code Online (Sandbox Code Playgroud)

p1和p2是所选文本的开头和结尾.它们是使用下面的代码生成的,它似乎按预期工作:

- (void)textViewDidChangeSelection:(UITextView *)textView {

    UITextRange *selectedRange = [myTextView selectedTextRange];

    p1 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange start]];
    p2 = [myTextView offsetFromPosition:myTextView.beginningOfDocument toPosition:[selectedRange end]];

 }
Run Code Online (Sandbox Code Playgroud)

编辑:我在阅读@ borrrden的评论后解决了这个问题.而不是NSMakeRange(p1,p2)]我使用NSMakeRange(p1,p2-p1)].

uitextview nsattributedstring ios nsrange nsrangeexception

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

如何将NSAttributedString转换为NSData?

我有一个NSAttributedString我想写回到粘贴板.

一般 NSPasteboard有不同的类型,将NSAttributedString 带附件不适合NSPasteboardTypeRTFD

我知道如何写入粘贴板:

    NSData * __strong newContent = ... // how?
    NSPasteboard * __strong pboard = [NSPasteboard generalPasteboard];
    NSString * __strong type = NSPasteboardTypeRTFD;
    [pboard setData:newContent forType:type];
Run Code Online (Sandbox Code Playgroud)

但是如何从给定的NSAttributedString两个转换RTF NSData(它应该适合粘贴板可读的类型)?

objective-c nsattributedstring nspasteboard osx-mavericks

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

TextView,iOS和OSX中的彩虹文本

我正在尝试为我的应用添加一些不同的文字颜色以融合到图像中.我有很多输入,我的用户想要彩虹文字颜色和重复.例如,单词:stackoverflow看起来像这样:s=red t=orange a=yellow c=green k=blue o=purple v=pink e=red r=orange f=yellow l=green o=blue w=purple

我甚至无法开始思考如何在一个单一的环境中做到这一点UITextView 有人知道如何在用户输入时实现这一目标吗?提示?例?

关于iOS的彩虹文字,我没有看到关于SO的任何其他帖子.(如我错了请纠正我)

macos cocoa objective-c nsattributedstring

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