标签: nsattributedstring

来自NSAttributedString的HTML

我需要将其转换回HTML,而不是将HTML转换为属性字符串.这可以在Mac上轻松完成,如下所示:http://www.justria.com/2011/01/18/how-to-convert-nsattributedstring-to-html-markup/

不幸的是,该方法dataFromRange:documentAttributes:仅在Mac上通过NSAttributedStringAppKit Additions提供.

我的问题是你怎么能在iOS上这样做?

html objective-c nsattributedstring ios

11
推荐指数
1
解决办法
4264
查看次数

NSMutableAttributedStrings - objectAtIndex:effectiveRange ::越界

我试图在标签上添加一些花哨的文本,但是我遇到了NSMutableAttributedString类的一些问题.我试图实现四个:1.更改字体,2.下划线范围,3.更改范围颜色,4.上标范围.

这段代码:

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
    NSMutableAttributedString* display = [[NSMutableAttributedString alloc]
                                          initWithString:@"Hello world!"];
    NSUInteger length = [[display string]length] - 1;

    NSRange wholeRange = NSMakeRange(0, length);
    NSRange helloRange = NSMakeRange(0, 4);
    NSRange worldRange = NSMakeRange(6, length);

    NSFont* monoSpaced = [NSFont fontWithName:@"Menlo" 
                                         size:22.0];

    [display addAttribute:NSFontAttributeName
                    value:monoSpaced
                    range:wholeRange];

    [display addAttribute:NSUnderlineStyleAttributeName 
                    value:[NSNumber numberWithInt:1] 
                    range:helloRange];

    [display addAttribute:NSForegroundColorAttributeName 
                    value:[NSColor greenColor]
                    range:helloRange];

    [display addAttribute:NSSuperscriptAttributeName 
                    value:[NSNumber numberWithInt:1] 
                    range:worldRange];

    //@synthesize textLabel; is in this file.
    [textLabel setAttributedStringValue:display];
}
Run Code Online (Sandbox Code Playgroud)

给我这个错误:

NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds
Run Code Online (Sandbox Code Playgroud)

此外,我尝试搞乱范围,但当我尝试时变得更加困惑NSRange worldRange = NSMakeRange(4, 5); …

macos objective-c nsattributedstring nsrange

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

NSTextView和NSAttributedString

我试图将一个属性字符串粘贴到我的NSTextView中,但它只显示为纯文本,没有属性.我创建了我的字符串:

    NSString *str = @"Parsing Directory Structure\n\n";

NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:str];
NSDictionary *attributes = @{
                             NSForegroundColorAttributeName : [NSColor blueColor],
                             NSFontAttributeName : [NSFont fontWithName:@"HelveticaNeue-Bold" size:20.f]
                             };
[attrstr setAttributes:attributes range:NSRangeFromString(str)];

[[self.textView textStorage] appendAttributedString:attrstr];
Run Code Online (Sandbox Code Playgroud)

在NSTextView上(在滚动视图内)我已经选中了"允许富文本"框,并且未选中"可编辑"框.我基本上试图像控制台输出窗口一样使用它.

cocoa nstextview nsattributedstring

11
推荐指数
1
解决办法
9553
查看次数

如何从NSMutableAttributedString中清除属性?

如何将所有属性设置NSMutableAttributedString为空?你必须通过它们进行枚举并删除它们吗?

我不想创建一个新的.我工作的textStorageNSTextView.设置新字符串会重置光标位置NSTextView并触发委托.

objective-c nsattributedstring ios swift

11
推荐指数
2
解决办法
8643
查看次数

如何从NSAttributedString的末尾修剪(删除)空格

我有一个字符串,在字符串的末尾没有空格,但当我转换为NSAttributedString并设置为UITextView它时,在结尾处看到一些空格UILabel.

为了使NSAttributedString我使用以下代码.在我的代码中expectedLabelSize给出了很高的高度.

UILabel *tempLbl = [[UILabel alloc]init];
tempLbl.font = txtView.font;
tempLbl.text = string;

NSDictionary *dictAttributes = [NSDictionary dictionaryWithObjectsAndKeys: tempLbl.font, NSFontAttributeName, aParaStyle, NSParagraphStyleAttributeName,[UIColor darkGrayColor],NSForegroundColorAttributeName, nil];

CGSize expectedLabelSize = [string boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dictAttributes context: nil].size;
Run Code Online (Sandbox Code Playgroud)

whitespace objective-c nsattributedstring ios

11
推荐指数
2
解决办法
2697
查看次数

使用故事板中的属性字符串本地化UILabel

我有一个UILabel,文本在故事板中设置为"归属".当我生成Main.strings文件以翻译成另一种语言时,不会出现此标签的文本.

我试图通过复制对象id手动将一个条目添加到Main.strings文件中.我尝试设置"text"属性和"attributedText"属性,但是当我运行应用程序时,不使用已翻译的字符串.

那么,如何将UILabel集本地化为故事板上的"归属"?

localization objective-c nsattributedstring ios

11
推荐指数
1
解决办法
3587
查看次数

选择NSTextField时字体更改

我想创建一个带有属性字符串的简单标签.我是这样做的:

NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
                                      NSParagraphStyleAttributeName,
                                      [NSFont fontWithName:@"Raleway-Bold" size:30],
                                      NSFontAttributeName,
                                      _grey,
                                      NSForegroundColorAttributeName,
                                      nil];
NSMutableAttributedString *noNotificationsString =
[[NSMutableAttributedString alloc] initWithString:@"No Notifications"
                                       attributes:noNotificationsAttrs];

NSTextField* title_field = [[NSTextField alloc] initWithFrame:
                             CGRectMake(
                                        0,
                                        0,
                                        200,
                                        200
                                        )
                             ];
[title_field setWantsLayer:true];
[title_field setSelectable:YES];
[title_field setAllowsEditingTextAttributes:true];
[title_field setAttributedStringValue:noNotificationsString];
[title_field setEditable:false];
[title_field setBordered:false];
title_field.tag = 1;
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

在此输入图像描述

在此输入图像描述

不幸的是,当点击(选择)此标签时,它显示如下:

在此输入图像描述

在此输入图像描述

在角落周围有点大胆和像素化.这种情况正在发生,许多其他标签具有不同的字符串,大小和颜色.我该怎么办呢?!

请注意,这些标签嵌套在nsscrollview- > nstableview- >中viewForTableColumn


堆叠选择:

在此输入图像描述 我相信问题是NSCell在mousedown上调用了一个编辑功能.


选择的字体也不同!! 在此输入图像描述


编辑:

有趣的是,如果我wantslayer:YES从(2)父视图中删除它不会这样做.但他们都需要想要层,否则我就不能有弯角......

macos objective-c nstextfield nsattributedstring

11
推荐指数
1
解决办法
632
查看次数

iOS NSAttributedString表情符号透明度/ alpha

我正在使用UITextView,并希望文本的某些部分是透明的.

我目前正在使用NSAttributedString和NSForegroundColorAttribute,其颜色具有所需的透明度.

它适用于常规字符,但不适用于表情符号.是否可以在UITextView中将表情符号设置为透明?如果是这样,怎么样?

注意:我希望支持iOS6或更高版本.

transparency alpha nsattributedstring ios emoji

10
推荐指数
0
解决办法
996
查看次数

如何使用自定义键盘扩展插入NSAttributedString?

我想用键盘扩展,因为这些应用程序编写一些自定义的字体的文本(1,2,3,4)在做什么.我知道如何在文档代理中插入普通字符串.

    [self.textDocumentProxy insertText:mystring];
Run Code Online (Sandbox Code Playgroud)

我试图NSAttributedString使用上述方法插入,但我看不到任何方式插入NSAttributedString文档代理.

有人可以指导什么是解决这个问题的最佳方法?任何建议将不胜感激.

nsattributedstring custom-font ios ios-keyboard-extension

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

NSAttributedString无法按时渲染 - Swift

所以,我在一个项目中遇到问题,某些旧版iPhone在某些iOS版本中没有正确呈现某些字符串(特别是它不适用于带有iOS10的iPhone 5,而它适用于iOS9.3,奇怪的是).为了减少这个问题,我写了这段代码:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Long HTML, although we'll make it even larger to prove a point
        let string = "<h1>Thing 1</h1><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget …
Run Code Online (Sandbox Code Playgroud)

iphone nsattributedstring ios swift

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