标签: nsattributedstring

存储NSAttributedString核心数据

我试图将NSAttributedString存储到Core Data SQL存储.

我将属性设置为"可转换",它是可选的,它不是瞬态的或索引的,并且值转换器名称设置为默认值"NSKeyedUnarchiveFromData".在.xcdatamodel中并生成在.h中具有此内容的托管对象类:

@property (nonatomic, retain) id Text; (I have tried changing id to NSAttributedString *Text)
Run Code Online (Sandbox Code Playgroud)

这在.m中:

@dynamic Text;
Run Code Online (Sandbox Code Playgroud)

我查看并将我的NSManagedObject的".text"属性设置为属性字符串,然后在完成时执行:

NSError *error = nil;
[managedObjectContext save:&error];
Run Code Online (Sandbox Code Playgroud)

此过程导致输出中出现此错误:

[NSCFType encodeWithCoder:]:发送到实例0xc04edb0的无法识别的选择器由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' * - [NSCFType encodeWithCoder:]:无法识别的选择器发送到实例0xc04edb0'

我已经检查了我存储到属性的类的类,它NSAttributedString也是我检查responsesToSelector @selector(:)并返回true,所以非常困惑,因为这与错误消息相反?

请指教.

谢谢詹姆斯

iphone core-data objective-c nsattributedstring

22
推荐指数
3
解决办法
8314
查看次数

在UILabel中对齐不同大小的文本

如何在UILabel中顶部对齐不同大小的文本?一个例子是在价格横幅中将较小尺寸的分金额与较大尺寸的金额进行顶部对齐.

示例图像

iOS6中的UILabel支持NSAttributedString允许我在同一个UILabel中使用不同大小的文本.但是它似乎没有顶部对齐文本的属性.有什么选择来实现这个?在我看来,提供自定义绘图逻辑来基于自定义属性字符串键进行顶部对齐可能是最好的,但我不知道如何去做.

nsattributedstring uilabel ios ios6

22
推荐指数
2
解决办法
9896
查看次数

真的与NSAttributedString关闭线?

我希望两行文本看起来非常靠近(小行间距)的按钮.我有以下代码:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"50 WPM"];

NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
paragrapStyle.alignment = NSTextAlignmentCenter;
paragrapStyle.lineSpacing = -10;

[string addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, string.length)];

UIFont *font1 = [UIFont systemFontOfSize:22.0];
[string addAttribute:NSFontAttributeName value:font1 range:NSMakeRange(0, string.length - 4)];

UIFont *font = [UIFont systemFontOfSize:15.0];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(string.length - 3, 3)];

[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, string.length)];

[self.button setAttributedTitle:string forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

但由于行间距不能为负,所以它并没有像我希望的那样接近.它看起来像这样:

在此输入图像描述

有没有办法让他们更接近?

objective-c uibutton nsattributedstring uilabel ios

22
推荐指数
2
解决办法
8925
查看次数

NSUnderlineStyleAttributeName下划线间距

我希望下划线低于文本,就像任何其他正常的下划线机制一样.然而,使用NSAttributed字符串,它会留下"g's"和"y"的漏洞

例: 在此输入图像描述

应该如何看待: 在此输入图像描述

如何增加下划线和标签之间的间距?

underline nsattributedstring ios

22
推荐指数
3
解决办法
6722
查看次数

使用NSAttributed String更改字符串的颜色和字体

我有一个字符串,其中最后一个单词与整个字符串中的其他单词相比总是具有不同的颜色,例如:如果字符串颜色为黑色,则语句中的最后一个单词将为红色.我正在使用NSAttributed字符串,如下所示:

NSDictionary *attrDict = [NSDictionary dictionaryWithObject:[UIFont fontWithName:Arial size:16.0] forKey:NSFontAttributeName];
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:@"This is a" attributes: attrDict];
Run Code Online (Sandbox Code Playgroud)

我会为最后一个单词制作类似的NSAttributedString,并用第一个字符串修改它.

问题:我想在字符串中添加颜色和字体.我已经使用字典处理字体了.但是我可以在同一个字典中添加颜色处理程序/代码,或者在NSAttributedString上使用"addAttribute:"是添加颜色或任何其他属性的唯一方法吗?

objective-c nsattributedstring addattribute ios

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

为什么使用HTML字符串对NSAttributedString的初始调用比后续调用长100多倍?

我需要在我的iOS应用程序中显示HTML文本.我决定使用内置方法NSAttributedString,initWithData:options:documentAttributes:error:.实际的解析工作非常好,但是,我似乎遇到了一个非常奇怪的错误,如果我附加调试器,它似乎只会表现出来.

第一次调用此方法时,在运行iOS 7.0.4的iPhone 5S上运行时间不到1秒,在iPod Touch第5代上运行约1.5秒.这个怪癖也体现在模拟器上,但由于模拟器的绝对速度,它明显不那么引人注目.

后续呼叫只需要大约10-50毫秒,这比初始呼叫要快得多.

这似乎与输入字符串的缓存无关,因为我在我的"真实"应用程序中使用多个输入字符串对其进行了测试.

但是,当我在没有调试器的情况下运行程序时,它按预期运行,大约需要10-20ms,这是我希望HTML解析所采用的.

以下是相关的代码部分:

-(void) benchmarkMe:(id)sender {
    NSData *data = [testString dataUsingEncoding:NSUTF8StringEncoding];

    NSTimeInterval startTime = [[NSDate date] timeIntervalSinceReferenceDate];

    // So the complier doesn't keep complaining at me.
    __attribute__((unused))
    NSAttributedString *parsed = [[NSAttributedString alloc] initWithData:data
                                                                  options:@{
                                                                        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                        NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
                                                                    }
                                                       documentAttributes:nil
                                                                    error:nil];

    NSTimeInterval endTime = [[NSDate date] timeIntervalSinceReferenceDate];

    NSString *message = [NSString stringWithFormat:@"Took %lf seconds.", endTime - startTime];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Benchmark complete!"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil]; …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c nsattributedstring ios lldb

21
推荐指数
1
解决办法
4119
查看次数

如何在NSAttributedString中为行添加间距

我正在制作一个格式化剧本的应用程序,我使用NSAttributedString来格式化输入到UITextView中的文本,但是有些行太靠近了.

我想知道是否有人可以提供代码示例或如何改变这些行之间的边距的提示,以便它们之间有更多的空间.

下面是另一个桌面编剧程序的图像,它演示了我的意思,注意每个位之前有一点空间,它说的是"DOROTHY".

在此输入图像描述

nsattributedstring ios

21
推荐指数
3
解决办法
3万
查看次数

使用 HTML 字符串初始化 NSAttributedString 时 iOS 15 崩溃

我有一个将 HTML 字符串转换为NSAttributedString.

iOS 15 发布后,我看到了很多用户的崩溃情况。所有这些崩溃都发生在 iOS 15 上并且发生在主线程上。

这是来自 Crashlytics 的典型崩溃报告。

Crashed: com.apple.main-thread
0  libsystem_platform.dylib       0x1f29d60c0 _os_unfair_lock_recursive_abort + 36
1  libsystem_platform.dylib       0x1f29d0a10 _os_unfair_lock_lock_slow + 304
2  Foundation                     0x183d5e730 -[NSProcessInfo(NSProcessInfoHardwareState) isLowPowerModeEnabled] + 68
3  WebCore                        0x192011004 <redacted> + 56
4  CoreFoundation                 0x182535ee8 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 28
5  CoreFoundation                 0x1825d1b9c ___CFXRegistrationPost_block_invoke + 52
6  CoreFoundation                 0x1825a4f54 _CFXRegistrationPost + 456
7  CoreFoundation                 0x18254bd54 _CFXNotificationPost + 716
8  Foundation                     0x183d50028 -[NSNotificationCenter postNotificationName:object:userInfo:] + 96
9  Foundation                     0x183dc89d4 NSProcessInfoNotifyPowerState + 188 …
Run Code Online (Sandbox Code Playgroud)

html crash nsattributedstring swift ios15

21
推荐指数
1
解决办法
3577
查看次数

计算字体大小以适合框架 - 核心文本 - NSAttributedString - iOS

我有一些文本,我通过NSAttributedString(下面的代码)绘制到一个固定的框架.目前我正在努力将文本大小编码为16.我的问题是,有没有办法计算给定帧文本的最佳拟合大小?

- (void)drawText:(CGContextRef)contextP startX:(float)x startY:(float)
y withText:(NSString *)standString
{
    CGContextTranslateCTM(contextP, 0, (bottom-top)*2);
    CGContextScaleCTM(contextP, 1.0, -1.0);

    CGRect frameText = CGRectMake(1, 0, (right-left)*2, (bottom-top)*2);

    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:standString];
    [attrString addAttribute:NSFontAttributeName
                      value:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]
                      range:NSMakeRange(0, attrString.length)];

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attrString));
    struct CGPath * p = CGPathCreateMutable();
    CGPathAddRect(p, NULL, frameText);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), p, NULL);

    CTFrameDraw(frame, contextP);
}
Run Code Online (Sandbox Code Playgroud)

fonts objective-c nsattributedstring core-text ios

20
推荐指数
5
解决办法
2万
查看次数

无法转换'[String:AnyObject]类型的值?' 预期的参数类型'[NSAttributedStringKey:Any]?'

如何将类型的值转换'[String : AnyObject]?'为预期的参数类型'[NSAttributedStringKey : Any]?'

open class func drawText(context: CGContext, text: String, point: CGPoint, 
align: NSTextAlignment, attributes: [String : AnyObject]?)
{
    var point = point

    if align == .center
    {
        point.x -= text.size(withAttributes: attributes).width / 2.0
    }
    else if align == .right
    {
        point.x -= text.size(withAttributes: attributes).width
    }

    NSUIGraphicsPushContext(context)

    (text as NSString).draw(at: point, withAttributes: attributes)

    NSUIGraphicsPopContext()
}
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift swift4 nsattributedstringkey

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