标签: nsattributedstring

UILabel中的单字幕下划线文本(移植ObjC代码)

我想强调UILabel中的文字.我找到了以下ObjC代码:

NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @1}
myLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Test string" 
                                                     attributes:underlineAttribute];
Run Code Online (Sandbox Code Playgroud)

我正在尝试将其移植到C#,但它无法正常工作.我正在尝试以下方法:

var keys = new object[] { "NSUnderlineStyleAttributeName" };
var objects = new object[] { 1 };
NSDictionary underlineAttribute = NSDictionary.FromObjectsAndKeys(objects, keys);
label.AttributedText = new NSAttributedString(@"Test",underlineAttribute);
Run Code Online (Sandbox Code Playgroud)

而不是1,我也尝试了"1"和NSUnderlineStyle.Single,但没有任何工作

有任何想法吗?

谢谢

nsattributedstring uilabel xamarin.ios ios

4
推荐指数
1
解决办法
4050
查看次数

如何以编程方式在动态文本中提供相同标签的不同字体颜色

嗨下面是我需要实现的视图UILabel.我听说过NSAttributedString但不确定如何使用它进行动态文本加载.

在此输入图像描述

整个文本字体是Roboto-Light.但是,我必须从前两位医生的API响应中替换文本'Andrew Murphy博士,John Smith',并从API获得"23位医生"的计数,以便相应地调整此标签.您可以看到的文本颜色取决于文本是常量还是动态.我不知道如何实现它.因此,一些代码片段真的很受欢迎.

谢谢!

objective-c nsattributedstring uilabel ios

4
推荐指数
1
解决办法
1704
查看次数

NSAttributedString initWithData:选择:documentAttributes:错误:documentAttributes不会保留在ARC

我正在尝试加载文档NSData(它来自Dropbox我的应用程序中的文件,但为了简单起见,下面的示例使用.txt文件,这导致我正在尝试解决的相同问题).

问题:我实例化一个NSDictionary,并将其[NSAttributedString -initWithData:options:documentAttributes:error:]作为out参数传递给它.

但是,NSDictionary实例被取消分配,并导致-initWithData:options:documentAttributes:error:崩溃.

当我启用时NSZombie,我得到的错误是:[_ _ NSDictionaryI retain]:发送到解除分配的实例的消息

- initWithData:options:documentAttributes:error:当我将NULL传递给documentAttributes时运行正常.

这是代码:

NSError* error;
NSString* path = [[NSBundle mainBundle] pathForResource:@"Forward Thinking"
                                                 ofType:@"txt"];
NSData* data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedAlways error:&error];
if (data) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDictionary* documentAttributes = [[NSDictionary alloc] init];
        NSAttributedString* attrStr = [[NSAttributedString alloc] initWithData:data options:nil documentAttributes:&documentAttributes error:NULL];
        self.textView.attributedText = attrStr;
    });
}
Run Code Online (Sandbox Code Playgroud)

任何线索都会很棒!

nsattributedstring ios

4
推荐指数
1
解决办法
3250
查看次数

Objective C - 如何从NSAttributedString创建rtf

我可以使用以下命令从rtf字符串转换为属性字符串:

 NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)

现在我怎么能从attributionString转换回rtf字符串?

rtf objective-c nsattributedstring ios

4
推荐指数
1
解决办法
3115
查看次数

NSAttributedString第一行缩进结束

我希望第一行中的第一行NSAttributedString是UITextView从第一行的右侧缩进.

因此firstLineHeadIndentin NSParagraphStyle将从左侧缩进第一行.我想做同样的事情,但是从我的右边做UITextView.

这是我希望文本包装的截图.
在此输入图像描述

objective-c uitextview nsattributedstring ios

4
推荐指数
1
解决办法
5076
查看次数

NSAttributedString冻结UITableView

使用NSAttributedString滚动时应用程序确实冻结(当我使用NSString它工作正常),所以有我的方法:

- (void)setSubtitleForCell:(TTTableViewCell *)cell item:(TTPhotoPost *)item
{
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:
                                            [item.caption dataUsingEncoding:NSUnicodeStringEncoding]
                                                                            options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                                                 documentAttributes:nil
                                                                              error:nil];

    [cell.descriptionLabel setAttributedText:attributedString];
}
Run Code Online (Sandbox Code Playgroud)

那里有什么错误吗?或某种方式使att.string更快?

objective-c uitableview nsattributedstring ios

4
推荐指数
1
解决办法
2791
查看次数

计算UILabel中可见文本的范围

我有一个UILabel固定大小的文本,我设置的文本长度UILabel可以是 200、5 或 500 个字符。我想要做的是计算UILabel在当前UILabel大小下可以放入多少可见文本。

为什么我要这样做?因为我想在...Read more文本的末尾添加一个文本,但不是在整个文本的末尾,只是在UILabel.

提前致谢。

objective-c nsattributedstring uilabel ios

4
推荐指数
1
解决办法
8027
查看次数

如何将系统字体与属性字符串一起使用?

let attributedText: NSMutableAttributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName: UIFont(name: "San Francisco", size: 14.0)!])
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,当我运行我的应用程序时,它崩溃了,因为它找不到“旧金山”字体。但这不是iOS 9的系统字体吗?如何在属性字符串中使用系统字体?

我正在使用 Swift 2.2

fonts nsattributedstring nsmutableattributedstring swift swift2.2

4
推荐指数
1
解决办法
4370
查看次数

UITextView,NSAttributedString和自定义属性

我在堆栈溢出方面进行了很多搜索,但找不到解决方案。也许我只是误解了一些答案。

我创建了一个,UITextView并且正在使用NSAttributedStringsUITextView很好。

现在,添加自定义属性后,我陷入了困境。

在哪里可以显示我的自定义属性UITextView?有委托方法,还是我必须创建自己UITextView的方法并覆盖方法?

objective-c uitextview nsattributedstring ios

4
推荐指数
1
解决办法
1683
查看次数

对NSAttributedString使用Codable时出错

我正在尝试为包含NSAttributedString的类实现Codable,但是在编译时出现错误:

try container.encode(str, forKey: .str)
Run Code Online (Sandbox Code Playgroud)

对成员'encode(_:forKey :)的错误含糊的引用

和

str = try container.decode(NSMutableAttributedString.self, forKey: .str)
Run Code Online (Sandbox Code Playgroud)

错误:没有任何“解码”候选者产生预期的上下文类型“ NSAttributedString”

我可以通过使用字符串中的NSData来解决它,但是我认为这应该可以工作

class Text : Codable {
    var str : NSAttributedString

    enum CodingKeys: String, CodingKey {
        case str
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(str, forKey: .str). <-- error ambiguous reference to member 'encode(_:forKey:)'
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        str = try container.decode(NSMutableAttributedString.self, forKey: .str)  <-- error: No 'decode' candidates …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift codable

4
推荐指数
1
解决办法
794
查看次数