我需要在NSTextView中为所选文本添加自定义属性.所以我可以通过获取选择的属性字符串,向其添加自定义属性,然后用我的新属性字符串替换选择来实现.
所以现在我将文本视图的属性字符串作为NSData并将其写入文件.稍后,当我打开该文件并将其恢复到文本视图时,我的自定义属性消失了!在为我的自定义属性计算出整个方案后,我发现不会为您保存自定义属性.请查看以下重要提示:http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/AttributedStrings/Tasks/RTFAndAttrStrings.html
所以我不知道如何使用此自定义属性保存和恢复我的文档.有帮助吗?
这是Twitter应用程序的屏幕截图供参考:http://screencast.com/t/YmFmYmI4M
我想要做的是在NSAttributedString中的子字符串顶部放置一个浮动弹出窗口,该字符串可以跨越多行.NSAttributedString是项目的要求.
在提供的屏幕截图中,您可以看到链接是背景突出显示的,因此它让我相信他们正在使用CoreText和NSAttributedStrings.我还找到了一个名为CTRunRef(http://developer.apple.com/library/ios/#documentation/Carbon/Reference/CTRunRef/Reference/reference.html)的东西看起来很有前景,但我很难将它们整合在一起概念.
简而言之,如果我在核心文本中有一个段落,当我点击一个单词时,如何找到该单词的边界框?
我们如何在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)
没有运气.
任何建议将不胜感激.
谢谢
我的应用程序中有一个秒表功能,使用居中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) 调用
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) 在AppKit中有没有办法快速测量大量NSString对象(比如一百万)的宽度?我尝试了3种不同的方法:
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显然是要走的路,但问题在于
如果你想玩,这是github项目.
cocoa objective-c nslayoutmanager nsattributedstring nstextstorage
我有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) 我的App.在很多地方我都使用过System Bold Font,它已经成为我应用程序的标准配置.
如果我使用Attributed String默认显示的字体是"Helvetica Neue".在所有字体中,我搜索了系统粗体字体,但找不到它.
那么,在使用归属字符串时如何设置系统粗体字体?

我有一个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) 我无法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)
这会导致以下行为:

请注意左侧和顶部的边距.
我意识到可能有一些方法来对齐文本而不使其符合视图的边缘,但是使其符合视图的边缘将允许我使用自动布局等直观地使用视图.
当字符串包含前导或尾随空格时,我不关心行为.
如果无法做到这一点NSAttributedString,是否有其他方法可以获得您建议的此行为?
澄清一下,这是我想要的第一号.
