标签: nsattributedstring

在NSAttributedString中保存自定义属性

我需要在NSTextView中为所选文本添加自定义属性.所以我可以通过获取选择的属性字符串,向其添加自定义属性,然后用我的新属性字符串替换选择来实现.

所以现在我将文本视图的属性字符串作为NSData并将其写入文件.稍后,当我打开该文件并将其恢复到文本视图时,我的自定义属性消失了!在为我的自定义属性计算出整个方案后,我发现不会为您保存自定义属性.请查看以下重要提示:http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/AttributedStrings/Tasks/RTFAndAttrStrings.html

所以我不知道如何使用此自定义属性保存和恢复我的文档.有帮助吗?

cocoa attributes objective-c nsattributedstring

10
推荐指数
2
解决办法
7133
查看次数

iPhone CoreText:查找子字符串的像素坐标

这是Twitter应用程序的屏幕截图供参考:http://screencast.com/t/YmFmYmI4M

我想要做的是在NSAttributedString中的子字符串顶部放置一个浮动弹出窗口,该字符串可以跨越多行.NSAttributedString是项目的要求.

在提供的屏幕截图中,您可以看到链接是背景突出显示的,因此它让我相信他们正在使用CoreText和NSAttributedStrings.我还找到了一个名为CTRunRef(http://developer.apple.com/library/ios/#documentation/Carbon/Reference/CTRunRef/Reference/reference.html)的东西看起来很有前景,但我很难将它们整合在一起概念.

简而言之,如果我在核心文本中有一个段落,当我点击一个单词时,如何找到该单词的边界框?

iphone substring nsattributedstring core-text ios4

10
推荐指数
1
解决办法
3968
查看次数

NSMutableAttributedString上的iOS行间距属性

我们如何在NSMutableAttributedString上设置行间距或行高.我试过了

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing:150] ;
paragraphStyle.minimumLineHeight = 150;
[self addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.length)];
Run Code Online (Sandbox Code Playgroud)

没有运气.

任何建议将不胜感激.

谢谢

nsattributedstring ios

10
推荐指数
1
解决办法
8919
查看次数

如何阻止UILabel在每次增量时调整大小?

我的应用程序中有一个秒表功能,使用居中UILabel的比例间距字体来渲染时间.每次增加标签的宽度都会发生变化,从而产生一种在快速速度下看起来特别糟糕的弹跳效果.这是一个例子.

我怎样才能解决这个问题?

iOS 9更新

它现在是一个单行:

UIFont.monospacedDigitSystemFontOfSize(17, weight: UIFontWeightRegular)
Run Code Online (Sandbox Code Playgroud)

此外,我上次尝试时,下面的解决方案不适用于iOS 9.浪费了相当多的时间调试之前在标题中遇到这个问题.

在iOS 7中使用Text Kit变得微不足道.

确保导入Core Text:

#import <CoreText/CoreText.h>
Run Code Online (Sandbox Code Playgroud)

创建一个将比例数转换为等宽数字的设置:

NSArray *monospacedSetting = @[@{UIFontFeatureTypeIdentifierKey: @(kNumberSpacingType),
                                 UIFontFeatureSelectorIdentifierKey: @(kMonospacedNumbersSelector)}];
Run Code Online (Sandbox Code Playgroud)

通过附加当前使用的字体描述符来创建新的字体描述符UILabel:

UIFontDescriptor *newDescriptor = [[timeLabel.font fontDescriptor] fontDescriptorByAddingAttributes:@{UIFontDescriptorFeatureSettingsAttribute: monospacedSetting}];
Run Code Online (Sandbox Code Playgroud)

更新标签的字体:

// Size 0 to use previously set font size
timeLabel.font = [UIFont fontWithDescriptor:newDescriptor size:0];
Run Code Online (Sandbox Code Playgroud)

objective-c nsstring nsattributedstring core-text swift

10
推荐指数
1
解决办法
1585
查看次数

如果不在主线程上,NSAttributedString initWithData和NSHTMLTextDocumentType会崩溃

调用

NSAttributedString * as = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)

除主线程外,导致崩溃

1   0x194b861fc <redacted>
2   0x19801d31c <redacted>
3   0x198010eb4 _os_once
4   0x19801aa2c pthread_once
5   0x195a0858c <redacted>
6   0x195a07c78 WebKitInitialize
7   0x18bb38918 <redacted>
8   0x10020cdf0 _dispatch_client_callout
9   0x10020dcfc dispatch_once_f
10  0x1977f8bd0 <redacted>
11  0x1978009ac <redacted>
12  0x19780bdb8 <redacted>
13  0x1940b259c _NSReadAttributedStringFromURLOrData
14  0x1940b0eb4 <redacted>
15  0x1001041a0 -[MPMPostTextBrickCell attributedTextWithHTMLString:]
16  0x100103d3c __39-[MPMPostTextBrickCell setupWithBrick:]_block_invoke
17  0x187fb3508 <redacted>
18  0x187f04c94 <redacted>
19  0x187ef461c <redacted>
20  0x187fb626c <redacted>
21  0x10020cdf0 …
Run Code Online (Sandbox Code Playgroud)

cocoa cocoa-touch nsattributedstring nsoperationqueue ios

10
推荐指数
2
解决办法
7809
查看次数

在AppKit中测量文本宽度的性能

在AppKit中有没有办法快速测量大量NSString对象(比如一百万)的宽度?我尝试了3种不同的方法:

  • [NSString sizeWithAttributes:]
  • [NSAttributedString size]
  • NSLayoutManager(获取文本宽度而不是高度)

    以下是一些性能指标

    Count\Mechanism    sizeWithAttributes    NSAttributedString    NSLayoutManager
    1000               0.057                 0.031                 0.007
    10000              0.329                 0.325                 0.064
    100000             3.06                  3.14                  0.689
    1000000            29.5                  31.3                  7.06



    NSLayoutManager显然是要走的路,但问题在于

  • 由于创建了重量级的NSTextStorage对象,因此内存占用空间很大(根据分析器而超过1GB).
  • 创作时间长.所有花费的时间都是在创建上述字符串的过程中,这本身就是一个交易破坏者.(随后测量NSTextStorage对象,其中创建和布置的字形只需要大约0.0002秒).
  • 对于我想要做的事情,7秒仍然太慢.有更快的方法吗?在大约一秒钟内测量一百万个字符串?

    如果你想玩,是github项目.

  • cocoa objective-c nslayoutmanager nsattributedstring nstextstorage

    10
    推荐指数
    1
    解决办法
    763
    查看次数

    iOS - 使用归属文本自动缩小UILabel

    我有UILabel两个由新行分隔的属性字符串.第一个字符串的字体大小设置为17,第二个字符串设置为14. NSMutableAttributedString如果内容不能放在一行中,我希望我的第一个字符串调整为最小字体大小.

    那可能吗?

    UILabel通过在IB中为纯文本设置"自动缩小到最小字体大小" 来配置此类行为非常简单,但不知道如何为属性文本执行此操作.

    这是我的代码:

    NSString *eventName = @"Looong Event Name";
    NSString *placeString = @"My place";
    
    eventName = [eventName stringByAppendingString:@"\n"];        
    NSMutableAttributedString *attrName = [[NSMutableAttributedString alloc] initWithString:eventName];
    [attrName addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, [eventName length])];
    
    
     NSMutableAttributedString *attrPlace = [[NSMutableAttributedString alloc] initWithString:placeString];
     [attrPlace addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, placeString.length)];
     [attrPlace addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, placeString.length)];
    
      NSMutableAttributedString *finalString = [[NSMutableAttributedString alloc] initWithAttributedString:attrName];
      [finalString appendAttributedString:attrPlace];
    
      UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
    
      nameLabel.attributedText = finalString;
    
    Run Code Online (Sandbox Code Playgroud)

    iphone xcode cocoa-touch nsattributedstring ios

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

    找不到属性字符串iOS Xcode的系统粗体字体

    我的App.在很多地方我都使用过System Bold Font,它已经成为我应用程序的标准配置.

    如果我使用Attributed String默认显示的字体是"Helvetica Neue".在所有字体中,我搜索了系统粗体字体,但找不到它.

    那么,在使用归属字符串时如何设置系统粗体字体?

    在此输入图像描述

    nsattributedstring ios xcode5

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

    如何从NSAttributedString获取图像数据

    我有一个NSTextView.我将图像粘贴到其中并查看.当我获得文本视图的NSAttributedString的NSTextAttachment时,它的文件包装器是nil.如何获取粘贴到文本视图中的图像数据?

    我在NSAttributedString上使用类别来获取文本附件.如果可能的话,我宁愿不写入磁盘.

    - (NSArray *)allAttachments
    {
        NSError *error = NULL;
        NSMutableArray *theAttachments = [NSMutableArray array];
        NSRange theStringRange = NSMakeRange(0, [self length]);
        if (theStringRange.length > 0)
        {
            NSUInteger N = 0;
            do
            {
                NSRange theEffectiveRange;
                NSDictionary *theAttributes = [self attributesAtIndex:N longestEffectiveRange:&theEffectiveRange inRange:theStringRange];
                NSTextAttachment *theAttachment = [theAttributes objectForKey:NSAttachmentAttributeName];
                if (theAttachment != NULL){
                    NSLog(@"filewrapper: %@", theAttachment.fileWrapper);
                    [theAttachments addObject:theAttachment];
                }
                N = theEffectiveRange.location + theEffectiveRange.length;
            }
            while (N < theStringRange.length);
        }
        return(theAttachments);
    }
    
    Run Code Online (Sandbox Code Playgroud)

    cocoa nsattributedstring

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

    如何在视图中绘制没有边距的字符串?

    我无法NSAttributedString在视图中绘制一个没有边距的东西.这是我的代码:

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:72.0f]};
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Hello"
                                                                 attributes:attributes];
    [string drawWithRect:rect
                 options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics
                 context:nil];
    
    Run Code Online (Sandbox Code Playgroud)

    这会导致以下行为:

    用边距绘制的字符串

    请注意左侧和顶部的边距.

    1. 如何避免这些边距并使文本完全符合包含视图的边缘?我想要的是实际绘制的文本与视图的边缘相交,也就是说,在任何字形中绘制的最顶部像素应该在视图的顶部,并且在任何字形中绘制的最左边的像素位于视图的左侧.风景.
    2. 假设1是可能的,有没有办法可以得到绘制文本的实际宽度和高度,这样我就可以计算字体大小和字距以使文本符合底部和右边缘?

    我意识到可能有一些方法来对齐文本而不使其符合视图的边缘,但是使其符合视图的边缘将允许我使用自动布局等直观地使用视图.

    当字符串包含前导或尾随空格时,我不关心行为.

    如果无法做到这一点NSAttributedString,是否有其他方法可以获得您建议的此行为?

    澄清一下,这是我想要的第一号.

    期望的行为

    string uikit uiview nsattributedstring ios

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