标签: nsattributedstring

将NSAttributedString转换为纯文本

我有一个NSData包含NSAttributedString源自a的属性text()的实例NSTextView.我想将属性字符串转换为普通字符串(NSString)而不进行任何格式化以进行一些文本分析(在转换时我无法访问原始NSTextView及其NSTextStorage实例).

最好的方法是什么?

编辑:

出于好奇,我检查了以下结果:

[[[self textView] textStorage] words]
Run Code Online (Sandbox Code Playgroud)

这对于进行一些文本分析似乎很方便.结果数组包含NSSubTextStorage的实例(下面的单词"Eastern"):

东部{NSFont ="\"LucidaGrande 11.00 pt.P [](0x7ffcaae08330)fobj = 0x10a8472d0,spc = 3.48 \""; NSParagraphStyle ="对齐0,LineSpacing 0,ParagraphSpacing 0,ParagraphSpacingBefore 0,HeadIndent 0,TailIndent 0,FirstLineHeadIndent 0,LineHeight 0/0,LineHeightMultiple 0,LineBreakMode 0,Tabs(\n 28L,\n 56L,\n 84L,\n 112L,\n
140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n
308L,\n \n 336L \n),DefaultTabInterval 0,Blocks(null),Lists(null) ,BaseWritingDirection -1,HyphenationFactor 0,TighteningFactor 0.05,HeaderLevel 0"; }

NSSubTextStorage可能是一个私有类,因为我找不到任何文档.它还保留所有格式.

cocoa nsattributedstring

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

是否可以在UITextView和UITextField中更改单个单词的颜色

是否可以在UITextView和UITextField中更改单个单词的颜色?

如果我输入了一个带有符号前面的单词(例如:@word),它的颜色可以改变吗?

objective-c uitextfield uitextview nsattributedstring ios

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

NSAttributedString上标样式

我想在一个文本块中标注所有®字符的实例(法律免责声明,当然;))并且默认方式NSAttributedString不是很好.

如果我只是让角色成为并且仅使用未经修改的角色NSString,则其呈现与大写字母相同的大小并且大致位于基线处.如果我将superscript属性添加到NSAttributedString如下:

[attrStr setAttributes:@{(NSString *)kCTSuperscriptAttributeName : @1} range:NSMakeRange(locationOfReg, 1)];
Run Code Online (Sandbox Code Playgroud)

角色从基线抬起,其大小不变,但线间距现在受到影响,因为凸起的角色会侵入上面的线.

为了显示:

(R)的变体

我在Photoshop中创建了这个图像,通过减小角色的大小和移动基线来实现所需的位置.我知道如何更改iOS中的字体大小,但更改基线似乎更棘手.有关如何实现这一目标的任何建议?

编辑:我想我可以使用上标属性作为一种方法来改变基线.现在,找出一种获取当前字体大小并随后减少它以允许在不同大小的文本块上使用相同方法的方法会很棒.

nsattributedstring core-text ios

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

通过NSAttributedString属性循环以增加字体SIze

我只需循环遍历所有属性NSAttributedString并增加其字体大小.到目前为止,我已经达到了成功循环并操纵属性的程度,但我无法保存NSAttributedString.我评论出的这条线对我不起作用.怎么收回?

NSAttributedString *attrString = self.richTextEditor.attributedText;

[attrString enumerateAttributesInRange: NSMakeRange(0, attrString.string.length)
                               options:NSAttributedStringEnumerationReverse usingBlock:
 ^(NSDictionary *attributes, NSRange range, BOOL *stop) {

     NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];        

     UIFont *font = [mutableAttributes objectForKey:NSFontAttributeName];
     UIFont *newFont = [UIFont fontWithName:font.fontName size:font.pointSize*2];         
     [mutableAttributes setObject:newFont forKey:NSFontAttributeName];
     //Error: [self.richTextEditor.attributedText setAttributes:mutableAttributes range:range];
     //no interfacce for setAttributes:range:

 }];
Run Code Online (Sandbox Code Playgroud)

objective-c nsattributedstring ios

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

使用NSAttributedString将文本居中在UILabel中

对我正在开发的应用程序进行一些基本改进.iOS快速开发领域仍然是新手.我想我的代码中的文本行会自动居中,因为我将标签设置为居中.经过一番研究后,我发现事实并非如此.我如何将这样的代码对齐到中心:

let atrString = try NSAttributedString(
   data: assetDetails!.cardDescription.dataUsingEncoding(NSUTF8StringEncoding)!, 
   options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], 
   documentAttributes: nil)
assetDescription.attributedText = atrString
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel ios swift

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

NSAttributedString,改变字体整体但保留所有其他属性?

说我有一个NSMutableAttributedString.

该字符串具有各种格式的格式:

这是一个例子:

这个字符串在iOS中很难改变 ,真的很糟糕.

但是,字体本身不是您想要的字体.

我想要

将每个字符更改为特定字体(例如,Avenir)

但,

在各个地方保留整个字符串中出现的其他每个属性(粗体,斜体,颜色等).

你到底怎么这样做?


注意:

如果您在整个范围内轻松添加属性"Avenir":它只是删除所有其他属性范围,您将丢失所有格式

需要明确的是:不幸的是,属性并非实际上是"附加的".

nsattributedstring swift

30
推荐指数
3
解决办法
7844
查看次数

在iOS 7中NSLineBreakMode的等效字符串绘制方法是什么?

有一种方法

- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment;
Run Code Online (Sandbox Code Playgroud)

我现在必须替换iOS 7.我得到了

NSDictionary *attributes = @{NSFontAttributeName: font};
[self drawInRect:rect withAttributes:attributes];
Run Code Online (Sandbox Code Playgroud)

但如何指定像换行一样的换行模式?我搜索了归因字符串绘制符号的文档但没有提到换行模式.这自动化总是自动换行吗?

nsstring uikit nsattributedstring ios

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

iOS 7 BUG - NSAttributedString不会出现

上周我问了一个关于模拟器错误的问题,NSAttributedString没有显示:iOS 7模拟器错误 - NSAttributedString没有出现

不幸的是,现在看来这不是模拟器错误,而是iOS 7错误.我现在已经在iPhone 5设备上重现了这个问题.

该错误似乎是使用NSUnderlineStyleAttributeName和NSParagraphStyleAttributeName作为NSAttributedString的属性的组合.

到目前为止,我只测试了两台iOS 7设备,问题只出现在其中一台设备上.即使他们已经升级到完全相同的版本:

  • 带iOS 7.0的第一部iPhone 5(11A465):不显示文字

  • 升级到7.0.2后的第一个iPhone 5(11A501):不显示文本

  • 运行iOS 7.0的第二部iPhone 5(11A4449d):正确显示文本

  • 升级到7.0.2(11A501)后的第二部iPhone 5:不显示文字

所以看来Apple在iOS 7.0(11A4449d)之后引入了这个bug.我已经向他们提交了一个错误,并会根据我得到的任何回复更新您.

重现bug的步骤

如果您运行的是iOS 7.0.2,那么您应该可以重现此错误.

在您的设备上下载并运行该项目https://github.com/rohinnz/iOS-7-BUG---NSAttributedString-does-not-appear

要么

1)在Xcode 5中创建一个新的"单视图应用程序".什么都叫它.

2)在ViewController.m中,将viewDidLoad方法替换为:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableParagraphStyle* paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentCenter;

    NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit" attributes:
                                   @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                                     NSParagraphStyleAttributeName:paragraph}];

    UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
    myLabel.backgroundColor = [UIColor greenColor];
    myLabel.attributedText = attrStr;
    [myLabel sizeToFit];

    [self.view addSubview:myLabel];
}
Run Code Online (Sandbox Code Playgroud)

3)编译并在您的设备上运行.根据您的iOS …

nsattributedstring ios7 xcode5

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

如何将NSAttributedString转换为HTML字符串?

正如标题所说,现在我可以简单地将HTML转换NSAttributedStringinitWithHTML:documentAttributes:,但我想在这里做的是反向.是否有任何第三方库来实现这一目标?

   @implementation NSAttributedString(HTML)
-(NSString *)htmlForAttributedString{
    NSArray * exclude = [NSArray arrayWithObjects:@"doctype",
                         @"html",
                         @"head",
                         @"body",
                         @"xml",
                         nil
                         ];
    NSDictionary * htmlAtt = [NSDictionary
                              dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType,
                              NSDocumentTypeDocumentAttribute,
                              exclude,
                              NSExcludedElementsDocumentAttribute,
                              nil
                              ];
    NSError * error;
    NSData * htmlData = [self dataFromRange:NSMakeRange(0, [self length])
                               documentAttributes:htmlAtt error:&error
                         ];
        //NSAttributedString * htmlString = [[NSAttributedString alloc]initWithHTML:htmlData documentAttributes:&htmlAtt];
    NSString * htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
    return htmlString;
}
@end
Run Code Online (Sandbox Code Playgroud)

html cocoa nsattributedstring

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

UIPickerView不允许通过委托的`attributionTitleForRow ...`更改字体名称和大小

我使用以下函数来更改字体颜色和字体大小,颜色有效,但字体名称和字体大小拒绝工作.

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? 

var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Arial-BoldMT", size: 45)!, NSForegroundColorAttributeName:UIColor.whiteColor()])
Run Code Online (Sandbox Code Playgroud)

任何帮助?

谢谢.

size fonts cocoa uipickerview nsattributedstring

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