标签: nsattributedstring

NSRangeException Out of Bounds错误

我正在设置标签的属性文本,我收到这个奇怪的错误:由于未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray replaceObjectsInRange:withObject:length :: Out of bounds'.我之前从未见过这个错误,所以我不确定如何修复它.以下是导致崩溃的代码:

if ([cell.waveTextLabel respondsToSelector:@selector(setAttributedText:)])
{
    const CGFloat fontSize = 16.0f;

    //define attributes
    UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize];

    // Create the attributes
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];

    NSRange rangeOfIs = [cell.cellWaveObject.waveString rangeOfString:@" is"];
    NSLog(@"The range of is: {%lu, %lu}", (unsigned long)rangeOfIs.location, (unsigned long)rangeOfIs.length);
    NSRange nameRange = NSMakeRange(0, rangeOfIs.location);
    NSLog(@"The range of the name: {%lu, %lu}", (unsigned long)nameRange.location, (unsigned long)nameRange.length);

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:cell.cellWaveObject.waveString];
    [attributedString setAttributes:attrs range:nameRange];

    //set the label text
    [cell.waveTextLabel …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring uilabel nsrange nsrangeexception

19
推荐指数
1
解决办法
2182
查看次数

将NSUnderlineStyle.PatternDash添加到Swift中的NSAttributedString?

我正在尝试在我的Swift应用程序中为某些文本添加下划线.这是我目前的代码:

let text = NSMutableAttributedString(string: self.currentHome.name)

let attrs = [NSUnderlineStyleAttributeName:NSUnderlineStyle.PatternDash]

text.addAttributes(attrs, range: NSMakeRange(0, text.length))
homeLabel.attributedText = text
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误text.addAttributes:

NSString 与...不完全相同 NSObject

如何将枚举中包含的属性添加到Swift中的NSMutableAttributedString?

nsattributedstring ios nsmutableattributedstring swift

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

设置NSUnderlineStyle会导致无法识别的选择器异常

我试图用NSAttributed字符串为字符串加下划线.出于某种原因,我的行导致异常:

-[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance   
Run Code Online (Sandbox Code Playgroud)

结果应该用于UITableView中的UILabel,并根据需要创建.

这是代码:

attributedString = NSMutableAttributedString(string: message)

if let actor = event.actor {
   let attributes = [NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle]
   var attributedActorString = NSMutableAttributedString(string: actor.shirtName, attributes: attributes)
   attributedActorString.insert(NSAttributedString(string: " "), at: 0)
   attributedActorString.append(NSAttributedString(string: ". "))                               attributedActorString.append(attributedImageStringForUrl(event.actor!.portraitImageUrl, indexPath: indexPath))
   attributedString.append(attributedActorString)
}
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift swift3

19
推荐指数
1
解决办法
1508
查看次数

显示很多RichText:选择最佳选项

显示大量 RichText 的最佳方式是什么(HTML格式)

目前我正在开发一个论坛应用程序.正如您所期望的那样,网站上的帖子有很多HTML格式,列表,图像粗体文本,彩色文本等......

现在我可以想到几个选项(非理想选择,如果你想到在评论中发布的任何其他选项,请:)

  • 自定义单元格使用NSAttributedString + HTML + DTAttributedTextView为每个帖子?

    问题:我在其他地方的应用程序中使用了NSAttributedString + HTML类别并且它不太理想,创建NSAttributedString似乎相当昂贵(慢),即使是少量.对于10个以上的帖子,每个帖子可能是整篇文章的长度会很糟糕虽然DTAttributedTextView支持IMG html标签(大多数标签)它不支持远程加载图像,除非你在img中指定它们的宽度和高度标签.对于IMG标签可能是笑脸(10*10)或屏幕截图(640*960)的论坛,没有办法预测. (自从编写此NSAttributedString + HTML(现在重命名为DTCoreText)以来,已添加对<img>标记的完全支持并大大改进!)

  • 每个帖子中都有一个带有UIWebView的自定义单元格?

    这个我考虑了很长一段时间,但是在阅读这篇博文时,我发现了一个会导致问题的问题,即平滑滚动.为网站提供本机应用程序的想法是,使用简单的UIWebView查看网站移动主题更好.如果应用滞后和不畅,同时滚动这是糟糕的不是更好(也为我需要显示隐藏图像的WebView像他建议是行不通的).UIWebView也需要在主线程上创建或者它们中断.

  • 回到UIWebView? 令人讨厌的是,除了做一个可怜的作弊之外(比如在iFans应用程序中),你只显示文本,然后如果他们点击它,UIWebView加载所有漂亮的图像等......,唯一的选择似乎是做我认为的TapaTalk应用程序将整个线程视图作为UIWebView.这不是糟糕,因为它可能会有相当不错的性能,并允许我可能添加用户控制的主题等..但我发现在网站本机应用程序令人厌恶使用UIWebView的想法.

有没有人有任何创建网络应用程序的经验,比如可能是facebook客户端,论坛应用程序或新网站应用程序,它们必须显示来自网站的内容(我并不真正算一个Twitter客户端,因为它只需要处理文本和每个帖子少量链接).或者有关在iOS应用中显示RichText内容的最佳方式的任何想法?

任何想法都必须处理这个问题:

  • 多彩色文本.
  • 右,左和中心对齐的文本.
  • 图像(可变大小).
  • 粗体.
  • 不同大小和字体的文本.
  • 强调文字.
  • YouTube视频.
  • HTML表格.

以防万一实际问题不是很明确,我会总结一下:

" 为论坛客户端应用程序显示大量RichText(HTML格式)内容的最佳方式是什么? "

iphone uiwebview uitableview nsattributedstring ios

18
推荐指数
1
解决办法
3858
查看次数

使用NSAttributedString删除上限

我想在仅UILabel使用该attributedText NSAttributedString属性的情况下执行首字下沉.像这样:

http://www.interpretationbydesign.com/wp-content/uploads/2009/02/drop_caps.gif

我已经尝试将第一个字符范围的基线调整为负值,它可以将第一个字符的顶部与第一个字符的其余部分的顶部对齐.但我还没有找到任何方法让其他线条流向掉落角色的右侧.

这可以使用NSAttributedString only,或者我必须拆分字符串并使用Core Text自己渲染吗?

typography nsattributedstring uilabel core-text ios

18
推荐指数
3
解决办法
2880
查看次数

从故事板本地化归因于UITextView

我正在使用故事板,并有一个视图,我已经将一些UITextViews子类化.

我的问题是我正在使用ibtool --generate-strings-file从故事板中提取字符串进行本地化,然后ibtool -write在另一个故事板文件上使用以应用翻译的字符串.

当我使用ibtool任何具有归属文本的UITextView时,ibtool --generate-strings-file命令会忽略该命令并从结果字符串文件中省略.

是否可以从故事板中提取属性文本以进行本地化?

xcode localization nsattributedstring ios

18
推荐指数
2
解决办法
5962
查看次数

NSAttributedString initWithHTML字符编码不正确?

-[NSMutableAttributedString initWithHTML:documentAttributes:] 似乎破坏了特殊字符:

NSString *html = @"“Hello” World"; // notice the smart quotes
NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:htmlData documentAttributes:nil];
NSLog(@"%@", as);
Run Code Online (Sandbox Code Playgroud)

打印“Hello†World后跟一些RTF命令.在我的应用程序中,我将属性字符串转换为RTF并将其显示在一个NSTextView,但字符也在那里被破坏.

根据文档,默认编码是UTF-8,但我尝试显式,结果是相同的:

NSDictionary *attributes = @{NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]};
NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithHTML:htmlData documentAttributes:&attributes];
Run Code Online (Sandbox Code Playgroud)

cocoa nsattributedstring

18
推荐指数
3
解决办法
5952
查看次数

UILabel + AutoLayout =基线对齐错误

对齐两个标签时遇到问题.两个例子来说明问题

示例1(确定)

[leftLabel setText:@"03"];
[rightLabel setText:@"Description3"];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

例2(NOK)

[leftLabel setText:@"03"];
[rightLabel setAttributedText:[[NSAttributedString alloc] initWithString:@"Description3"]];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在这两个示例中,布局约束都是这样

[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-bigMargin-[leftLabel]-bigMargin-[rightLabel]-bigMargin-|"
                                        options:NSLayoutFormatAlignAllBaseline
                                        metrics:metrics
                                          views:views];
Run Code Online (Sandbox Code Playgroud)

问题是正确的标签,当文本是属性标签时,它被绘制在下面一点,如图中所示,并且对齐结果错误.

为什么?我能用这UIlabel两种方法解决这个问题吗?

编辑:

在GitHub上创建了一个项目,并对此进行了测试.这里的问题是即使没有NSAttributdString我也有问题!查看带有数字的标签,与说明和金额没有正确对齐.

在此输入图像描述

我在这里粘贴单元格的代码,但必须在项目中看到整个场景.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        UIView *contentView = [self contentView];

        [contentView setBackgroundColor:[UIColor clearColor]];

        dayLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
        [dayLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
        [contentView addSubview:dayLabel_];

        monthLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
        [monthLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
        [monthLabel_ setFont:[UIFont boldSystemFontOfSize:13.0f]];
        [contentView addSubview:monthLabel_];

        descriptionLabel_ = [[UILabel alloc] initWithFrame:CGRectZero];
        [descriptionLabel_ setTranslatesAutoresizingMaskIntoConstraints:NO];
        [descriptionLabel_ setFont:[UIFont …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring ios autolayout

18
推荐指数
1
解决办法
8567
查看次数

UILabel的行间距,单行文本

是否期望具有自定义lineSpacing属性的多行UILabel 包括该行间距,即使标签的文本适合一行?

这是我的标签:

let label = UILabel()
label.numberOfLines = 4
var paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 5
paragraph.lineBreakMode = .ByTruncatingTail
label.attributedText = NSAttributedString(string: "Some short text", attributes: [NSParagraphStyleAttributeName: paragraph])
Run Code Online (Sandbox Code Playgroud)

这是如何布局的.请注意文本下方的额外间距.

在此输入图像描述

为了比较:

在此输入图像描述

奇怪的是缺乏一致性.当标签延伸到第二行时,底线不再包含此额外间距:

在此输入图像描述

当有一行文本时,有没有办法删除这个行间距?或者其他一些方法来强制执行某些一致性,这样我至少可以解释它?

更新

基线计算似乎也被打破了.尝试将视图(此处为红色框)与标签的基线对齐时,会部分覆盖多行标签.

在此输入图像描述 在此输入图像描述

nsattributedstring uilabel ios autolayout

18
推荐指数
1
解决办法
3630
查看次数

Markdown到NSAttributedString库?

现在iOS 6中完全支持NSAttributedString,是否有一个库将采用带有markdown的NSString,并将其转换为NSAttributedString?

iphone markdown nsstring nsattributedstring ios

17
推荐指数
3
解决办法
6859
查看次数